HttpServlet映射不起作用

时间:2016-03-25 18:10:33

标签: java jsp servlets post

我正在使用Apache Tomcat v8.0服务器和Java EE应用程序。基本上,我创建了一个ResponseUpload servlet。我需要通过POST请求从Web应用程序获取JSON数据。这是Servlet的代码:

@WebServlet("/RequestUpload")
public class RequestUpload extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public RequestUpload() {
    super();
    // TODO Auto-generated constructor stub
}
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    BufferedReader reader = request.getReader();
    Gson gson = new Gson();
    JSONObject json = gson.fromJson(reader, JSONObject.class);
    uplRequest.upload(json);

    PrintWriter out = response.getWriter();
    out.print(json);
}

然后测试它是否有效我向jsp文件添加了一个表单,将 POST 添加到url:

  

http://localhost:8080/webApp/RequestUpload

表格:

<form action= "RequestUpload" method = "POST">
    First Name: <input type = "text" name = "first_name">
    <br />
    Last Name: <input type = "text" name = "last_name" />
    <input type = "submit" value = "Submit" />
</form>

但我收到404错误 -

  

HTTP状态404 - / webApp / RequestUpload

有人能告诉我我的错误在哪里吗?

我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>webApp</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>

</web-app>

P.S。另一种与RPC servlet相同的形式(结构类似)正在发挥作用。

2 个答案:

答案 0 :(得分:2)

经过研究并借助之前的回答,我在构建路径中发现了问题。 The build path image

我必须允许源文件夹的输出文件夹,并将输出文件夹从构建更改为 WEB-INF / classes

答案 1 :(得分:1)

Servlet类:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;

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

import com.google.gson.Gson;
@WebServlet("/RequestUpload")
public class RequestUpload extends HttpServlet {

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

        Map<String, String> options = new LinkedHashMap<String, String>();
        options.put("first_name", request.getParameter("first_name"));
        options.put("last_name", request.getParameter("last_name"));

        String json = new Gson().toJson(options);

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(json);

    }
} 

JSP:针对home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<form action= "RequestUpload" method = "POST">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>

如果您的Web应用程序的上下文根是webApp且服务器正在端口:8080上运行。你可以看到home.jsp如下:

enter image description here

当数据在 first_name 中以 Minhajur last_name 作为 Rahman 提供时,点击提交按钮后, 以下json响应将被返回: enter image description here

注意:确保在lib文件夹中添加了所需的jar依赖项。例如: gson-1.7.1.jar 存在于 webapp / WEB-INF / lib 文件夹中。

故障排除:

  

HTTP状态404 - / webApp / RequestUpload

- &GT;检查您的webApp是否已成功编译,并且已编译的 RequestUpload.class 已存在于tomcat服务器上的 webapp / WEB-INF / classes 目录中。

如果 RequestUpload.class 不存在,那么

1)尚未成功编译。       或

2)Compiled类文件尚未移至webapp / WEB-INF / classes目录。

因此,请遵循以下步骤:

i)清理你的tomcat工作目录并再次编译运行并检查那里是否存在类文件。

ii)再次在eclipse上删除并添加Tomcat服务器。然后按照前面的步骤进行操作。

您还可以阅读以获取更多参考: How to use Servlets and Ajax?