一尘不染

在web.xml中映射servlet

tomcat

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错误。我已经遇到这个问题一段时间了。有人请帮助我。


阅读 327

收藏
2020-06-16

共1个答案

一尘不染

您缺少<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类所在的位置相同。

2020-06-16