在web.xml中映射servlet [英] Mapping servlet in web.xml
问题描述
xml文件位于我的项目的 WebContent / WEB-INF / web.xml
中。我正在使用Eclipse并运行Tomcat(这不是通过Eclipse安装的,我更喜欢它是一个单独的安装)。
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 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>
表单页面提交到servlet时,它不起作用。我每次都收到404错误。我一直在遇到这个问题。有人请帮助我。
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> ...< / servlet> ;
标签对映射很重要。所以使用以下内容:
You are missing <servlet>...</servlet>
tag which is important to mapping. So use following :
<?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:
<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>
您的servlet名称注册
应该相同两个标签< servlet> ...< / servlet>
和< servlet-mapping> ...< / servlet-mapping>
和包
名称应与servlet类所在的名称相同。
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.
这篇关于在web.xml中映射servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!