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>
当表单页面提交给servlet时,它不起作用。我每次都会收到404错误。我一直遇到这个问题。有人请帮助我。
答案 0 :(得分:8)
您缺少对映射很重要的<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>
并且您应该在表单上给出action
值,如下所示:
<form action="/EmployeeManagement/WebContent/Registration" method="post">
//Some code here
</form>
并注意到以下所有值都区分大小写:
<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名称Registration
在<servlet>...</servlet>
和<servlet-mapping>...</servlet-mapping>
标记上应该相同,并且package
名称应与servlet类所在的名称相同。
答案 1 :(得分:2)
你没有将servlet名称映射到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>
答案 2 :(得分:0)
检查您的表单操作。 那条路是
/EmployeeManagement/WebContent/Registration
或
YOURAPPCONTEXT/EmployeeManagement/WebContent/Registration
或
YOURAPPNAME/EmployeeManagement/WebContent/Registration
答案 3 :(得分:0)
您忘记了配置的重要部分。您应该在web.xml
代码前添加servlet-mapping
:
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>com.name.of.your.servlet.class</servlet-class>
</servlet>
答案 4 :(得分:0)
您已指定servlet-mapping
并在Registration
中使用了名称servlet-name
,但未在之前定义。
在servlet映射中使用它之前,需要定义servlet
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>[fully qualifyied name of your servlet]</servlet-class>
</servlet>
答案 5 :(得分:0)
您缺少在web.xml中定义servlet的另一部分
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>
package.path.to.RegistrationServlet
</servlet-class>
</servlet>