java netbeans 中最简单的 servlet 和 web.xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10412823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:57:03  来源:igfitidea点击:

Simplest possible servlet and web.xml in netbeans

javaservletsnetbeansglassfishweb.xml

提问by vascowhite

I am trying to understand how java servlets work and I'm using netbeans 7.1.1 as my IDE. This is hosted on my dev machine which is an Ubuntu VM running on a windows 7 host OS.

我试图了解 java servlets 是如何工作的,我使用 netbeans 7.1.1 作为我的 IDE。它托管在我的开发机器上,这是一个在 Windows 7 主机操作系统上运行的 Ubuntu VM。

I used the netbeans wizard to set up my simple app, however there must be something I'm not understanding as going to the url http://localhost:8080/hssdatabase/results in the following error message:-

我使用 netbeans 向导来设置我的简单应用程序,但是一定有一些我不明白的地方,因为转到 url 会http://localhost:8080/hssdatabase/导致以下错误消息:-

javax.servlet.ServletException: PWC1397: Wrapper cannot find servlet class hss.index or a class it depends on

javax.servlet.ServletException: PWC1397: Wrapper 找不到 servlet 类 hss.index 或它依赖的类

Here is my project folder tree:-

这是我的项目文件夹树:-

Netbeans project

网豆项目

Here is my web.xml:-

这是我的 web.xml:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>index</servlet-name>
        <servlet-class>hss.index</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

I also tried:-

我也试过:-

<url-pattern>/index</url-pattern>

and going to http://localhost:8080/hssdatabase/index, but that had the same result.

并要去http://localhost:8080/hssdatabase/index,但结果相同。

To deploy the app I used the new project wizard to set up a basic project, then deleted the jsp that was generated. Then deleted everything in web.xml between the tags and then used the new servlet wizard to create the servlet and fill in web.xml. I'm interested in understanding why this doesn't work rather than inspecting the steps I took to get here. I'm trying to understand the web.xml and it's relationship with the servlet at the moment. Could it be the xmlns declarations that are wrong?

为了部署应用程序,我使用新项目向导设置了一个基本项目,然后删除了生成的 jsp。然后删除 web.xml 中标记之间的所有内容,然后使用新的 servlet 向导创建 servlet 并填写 web.xml。我有兴趣了解为什么这不起作用,而不是检查我到达这里所采取的步骤。我正在尝试了解 web.xml 以及它目前与 servlet 的关系。可能是 xmlns 声明错误吗?

And my index.java is just the bog standard default servlet generated by netbeans, so I don't think it's worth posting it here.

而我的 index.java 只是 netbeans 生成的 bog 标准默认 servlet,所以我认为不值得在这里发布它。

My question is: Is there something missing or wrong in the web.xml?

我的问题是:web.xml 中是否缺少某些内容或错误?

I have read everything I can find about it, but can't see anything wrong with it.

我已经阅读了所有我能找到的关于它的内容,但看不出它有什么问题。

采纳答案by vkraemer

The message indicates that your hss/index.java file did not compile OR one of the files that it depends on is not in the classpath. You may want to look at the ant output associated with 'Run' or 'Deploy' action's execution. The output should look something like this:

该消息表明您的 hss/index.java 文件未编译或它所依赖的文件之一不在类路径中。您可能想查看与“运行”或“部署”操作的执行相关联的 ant 输出。输出应如下所示:

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/web/WEB-INF/classes
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/web/META-INF
Copying 1 file to /Users/vkraemer/NetBeansProjects/WebApplication37/build/web/META-INF
Copying 2 files to /Users/vkraemer/NetBeansProjects/WebApplication37/build/web
library-inclusion-in-archive:
library-inclusion-in-manifest:
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/empty
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/generated-sources/ap-source-output
compile:
compile-jsps:
In-place deployment at /Users/vkraemer/NetBeansProjects/WebApplication37/build/web
Initializing...
run-deploy:
Browsing: http://localhost:8080/WebApplication37
run-display-browser:
run:
BUILD SUCCESSFUL (total time: 1 second)

You should also verify that the index.class file is in the $projectroot/build/web/WEB-INF/classes/hss directory. The best way to check that out is to use the Files explorer.

您还应该验证 index.class 文件是否在 $projectroot/build/web/WEB-INF/classes/hss 目录中。检查这一点的最佳方法是使用文件资源管理器。

回答by Bindu

Make sure that in index.java, before all the imports, you declare the package:

确保在 index.java 中,在所有导入之前,声明包:

package Interfaz;

Otherwise it won't be able to find that servlet under that package even though web.xml is fine.

否则,即使 web.xml 很好,它也无法在该包下找到该 servlet。