【问题标题】:Jetty - Servlet cannot be found码头 - 找不到 Servlet
【发布时间】:2014-09-19 16:07:30
【问题描述】:

我是 Servlet 的新手,想通过使用 Eclipse 的 Jetty 插件来调用一个简单的 Servlet。我可以调用 index.html,但是在尝试访问 Servlet 时,我得到了:

HTTP 错误:404
访问 /ProjectServlet 时出现问题。原因:
未找到

我认为我正确配置了所有文件,无法解释 Jetty 为何返回此错误。

谢谢你帮助我!


index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>TEST</title>
</head>
<body>
    <h1>Servlet Call Test:</h1>
    <form action="/ProjectServlet" method="GET">
        <input type="text" name="name" maxlength="20" value="Name" onfocus="this.select()"/><br>
        <input type="submit" name="callservlet" value="Call Servlet."/>
    </form>
</body>
</html>

ProjectServlet.java

package servlets;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProjectServlet
 */
@WebServlet(description = "Servlet to return JSON response with project list", urlPatterns = { "/ProjectServlet" })
public class ProjectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ProjectServlet() {
        super();
    }

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

        //Call Servlet Button
        if (request.getParameter("callservlet") != null) {

            response.setContentType("application/json");

            String name = request.getParameter("name"); 
            String jsonexample = "hi " + name; 

            response.getWriter().write(jsonexample);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

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>Test</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>

  <servlet>
    <servlet-name>ProjectServlet</servlet-name>
    <servlet-class>servlets.ProjectServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ProjectServlet</servlet-name>
    <url-pattern>/ProjectServlet</url-pattern>
  </servlet-mapping>
</web-app>

【问题讨论】:

  • 您正在使用 WebServlet 注释并在 web.xml 中定义 servlet。你只需要一个。不过,我不知道这是否是错误。从 web.xml 中删除 servlet 定义和 servlet 映射,或者删除 WebServlet 注释。

标签: java eclipse servlets jetty


【解决方案1】:

这应该如下图在Servlet url之前添加上下文路径

<form action="${pageContext.servletContext.contextPath}/ProjectServlet" 
         method="GET">

而不是

<form action="/ProjectServlet" method="GET">

【讨论】:

  • 我已经尝试过了,但仍然得到:HTTP ERROR 404 Problem访问/Test/$%7BpageContext.servletContext.contextPath%7D/ProjectServlet。原因:NOT_FOUND
  • 不知道你是怎么做的?它应该工作。为什么没有按照上述异常替换 contextPath。
  • 将welcome-file-list条目移到web.xml的最后
  • 这绝对是错误的根源。但我不知道如何定义 servlet 的正确路径。
  • 让我在最后尝试测试一下。
【解决方案2】:

使用web.xml 声明或使用您同时使用的注释,这就是发生冲突情况并返回 404 的原因。因此请从 xml 文件中删除注释或映射。

另外为提交按钮指定action

 <input type="submit" name="callservlet" value="Call Servlet." onClick="action"/>
                                                               ^^^^^^^^^^^^^^^^^

【讨论】:

  • 我之前也试过这个。删除注释和删除映射都不起作用。我仍然遇到同样的错误。
【解决方案3】:

您要么需要用户相对路径(不带'/'):

<form action="ProjectServlet" method="GET">

或包含根上下文的服务器的完整路径:

<form action="/yourRootContext/ProjectServlet" method="GET">

如果您不打算更改根上下文,您可以在页面中对其进行硬编码,否则使用以下任一方法获取:

Scrplet: action="<%= application.getContextPath() %>/ProjectServlet"
EL: action="${pageContext.servletContext.contextPath}/ProjectServlet"

根据您的喜好。

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2020-08-19
    • 2012-04-24
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多