Jetty - 无法找到Servlet

时间:2014-07-28 12:55:51

标签: java eclipse servlets jetty

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

  

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

我认为我正确配置了所有文件,无法解释为什么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>

3 个答案:

答案 0 :(得分:2)

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

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

而不是

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

答案 1 :(得分:2)

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

此外,为提交按钮指定action

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

答案 2 :(得分:1)

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

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

或servelt的完整路径,包括根上下文:

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

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

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

根据您的偏好。