在web.xml中映射servlet

Pra*_*mod 2 java xml eclipse tomcat servlets

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>
Run Code Online (Sandbox Code Playgroud)

当表单页面提交给servlet时它不起作用.我每次都会收到404错误.我一直遇到这个问题.有人请帮帮我.

Yub*_*raj 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>
Run Code Online (Sandbox Code Playgroud)

你应该action在表格上给出如下价值:

<form action="/EmployeeManagement/WebContent/Registration" method="post">

      //Some code here

</form> 
Run Code Online (Sandbox Code Playgroud)

并且还注意到以下代码中的所有值都区分大小写:

<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>
Run Code Online (Sandbox Code Playgroud)

您的servlet名称Registration在两个标记上应该相同<servlet>...</servlet>,<servlet-mapping>...</servlet-mapping>并且package名称应该与servlet类所在的名称相同.