Java 在 web.xml 中映射 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18889681/
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
Mapping servlet in web.xml
提问by Pramod
The xml file is located in WebContent/WEB-INF/web.xml
of my project. I am using Eclipse and running Tomcat (which is not installed via Eclipse. I prefer it to be a separate installation).
xml 文件位于WebContent/WEB-INF/web.xml
我的项目中。我正在使用 Eclipse 并运行 Tomcat(它不是通过 Eclipse 安装的。我更喜欢它是一个单独的安装)。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>
It doesnt work when the form page submits to the servlet. I am getting a 404 error everytime. I have been encountering this problem for a while. Somebody please help me.
当表单页面提交给 servlet 时它不起作用。我每次都收到 404 错误。我遇到这个问题已经有一段时间了。有人请帮助我。
采纳答案by Yubaraj
You are missing <servlet>...</servlet>
tag which is important to mapping. So use following :
您缺少<servlet>...</servlet>
对映射很重要的标签。所以使用以下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>EmployeeManagement</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>name</param-name>
<param-value>Pramod</param-value>
</context-param>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
</web-app>
and you should give action
value on your form like following:
你应该action
在你的表格上给出如下价值:
<form action="/EmployeeManagement/WebContent/Registration" method="post">
//Some code here
</form>
and also note it down all values are case sensitive on the following code:
并记下以下代码中的所有值都区分大小写:
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.yourPackageName.yourServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
Your servlet name Registration
should be same on both tags <servlet>...</servlet>
and <servlet-mapping>...</servlet-mapping>
and also package
name should be same where your servlet class is located.
您的 servlet 名称Registration
应该在两个标签上相同<servlet>...</servlet>
,<servlet-mapping>...</servlet-mapping>
并且package
名称也应该与您的 servlet 类所在的位置相同。
回答by Mite Mitreski
Check your form action. Is the path there
检查您的表单操作。路径在那里
/EmployeeManagement/WebContent/Registration
or
或者
YOURAPPCONTEXT/EmployeeManagement/WebContent/Registration
or
或者
YOURAPPNAME/EmployeeManagement/WebContent/Registration
回答by upog
you have not mapped servlet name to servlet class, It should be like given below,
您尚未将 servlet 名称映射到 servlet 类,它应该如下所示,
In <servlet-class>
give the path of your servlet
在 <servlet-class>
给出你的servlet的路径
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.Registration<servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/EmployeeManagement/WebContent/Registration</url-pattern>
</servlet-mapping>
回答by M. Abbas
You forgot a vital part of the configuration. You should add this to your web.xml
before servlet-mapping
tag:
你忘记了配置的一个重要部分。您应该将其添加到您的web.xml
beforeservlet-mapping
标签中:
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.name.of.your.servlet.class</servlet-class>
</servlet>
回答by A4L
You have specifyed a servlet-mapping
and used the name Registration
in servlet-name
without defining it before.
您已经指定了 aservlet-mapping
并使用了名称Registration
inservlet-name
之前没有定义它。
You need to define the servlet before using it in a servlet mapping
您需要在 servlet 映射中使用它之前定义 servlet
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>[fully qualifyied name of your servlet]</servlet-class>
</servlet>
回答by D_G
You are missing another part to define the servlet in the web.xml
您缺少在 web.xml 中定义 servlet 的另一部分
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>
package.path.to.RegistrationServlet
</servlet-class>
</servlet>