在 web.xml 中映射 servlet [英] Mapping servlet in web.xml

查看:25
本文介绍了在 web.xml 中映射 servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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> 标记.所以使用以下:

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>

你应该在你的表单上给出 action 值,如下所示:

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 名称 Registration<servlet>...</servlet> 标签上应该相同...</servlet-mapping>package 名称应该与您的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆