xml文件位于WebContent/WEB- INF/web.xml我的项目中。我正在使用Eclipse并运行Tomcat(它不是通过Eclipse安装的。我希望它是单独的安装)。
WebContent/WEB- INF/web.xml
<?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错误。我已经遇到这个问题一段时间了。有人请帮助我。
您缺少<servlet>...</servlet>标记,这对于映射很重要。因此,使用以下命令:
<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像下面这样在表单上赋予价值:
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类所在的位置相同。
Registration
<servlet- mapping>...</servlet-mapping>
package