【问题标题】:Changing web.xml <servlet-mapping> in Eclipse has no effect (Eclipse Juno, Tomcat 6)在 Eclipse 中更改 web.xml <servlet-mapping> 无效(Eclipse Juno,Tomcat 6)
【发布时间】:2012-11-15 14:36:09
【问题描述】:

我对 Web 编程非常陌生,我正在尝试编写一个简单的应用程序来从输入表单中收集数据并通过 HTTPServletRequest 将其传递给 servlet。我知道每个 servlet 都需要映射到 web.xml 内的特定 URL。我的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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>InputFormTest</display-name>
  <welcome-file-list>
    <welcome-file>InputForm.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>InputFormTest</display-name>
    <servlet-name>InputFormTest</servlet-name>
    <servlet-class>com.jsp.core.InputFormTest</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>InputFormTest</servlet-name>
    <url-pattern>/InputFormTest</url-pattern>
  </servlet-mapping>
</web-app>

InputForm.jsp 显示正确。我已经使用 Eclipse 的上下文菜单添加了 InputFormTest servlet,即 New -> Servlet,它具有以下代码:

package jsp.core;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class InputFormTest
 */
public class InputFormTest extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>InputFormTest</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>User inserted: " + request.getParameter("id") + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

我有两个问题:

  1. 首先,在 Eclipse 中运行应用程序后,我收到以下错误

    HTTP 状态 404 - /InputFormTest


    输入状态报告

    消息/InputFormTest

    说明请求的资源不可用。


    Apache Tomcat/6.0.36

  2. 其次,在web.xml 中更改&lt;url-pattern&gt;/InputFormTest&lt;/url-pattern&gt; 不会影响上述输出,这很奇怪,因为我认为message /InputFormTest 应该反映web.xml 中的设置

    李>

任何提示表示赞赏。

【问题讨论】:

    标签: eclipse jsp tomcat servlets


    【解决方案1】:

    好吧,我终于想通了。可以猜到,答案很简单:Eclipse 内部浏览器缓存页面。

    这意味着,在使用“Run as -> Run on server”运行新更改的项目后,Eclipse 内部浏览器中显示的页面是一个旧的缓存页面。之后需要手动刷新。在 Web 开发项目的情况下有一种奇怪的行为。除此之外,一切正常 - 案件已结案。

    【讨论】:

      【解决方案2】:

      右键单击服务器和服务器资源管理器,然后单击清理。 再次尝试运行应用程序。我认为它会起作用。

      希望这会有所帮助。

      【讨论】:

      • 您好,OP 本身已经回答了这个问题。你没看到吗?
      【解决方案3】:

      只是想补充上面的答案:

      如果您已经清除了服务器的缓存并刷新了您的项目,但仍然无法正常工作,请尝试清除您所在的 Web 浏览器的缓存。这是我必须执行的最后一步,才能使更新的 Web xml 正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-24
        • 1970-01-01
        • 2013-05-04
        • 2012-10-15
        • 2013-01-29
        • 1970-01-01
        • 2012-08-22
        • 1970-01-01
        相关资源
        最近更新 更多