【发布时间】:2016-03-25 18:10:32
【问题描述】:
我正在使用 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 文件添加了一个表单,对 url 执行 POST:
形式:
<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>
附:另一种对 RPC servlet 执行相同操作的形式(其结构类似)正在工作。
【问题讨论】:
-
您访问的是什么网址?因为我在你的错误中看到了括号。
-
请将您的问题编辑成MCVE;你已经发布了一个 HTML 的 sn-p 和一个 Servlet,那个 sn-p 的 url 是什么?你用的是什么服务器?您是否启用了日志记录?
-
我添加了有关服务器和 url 的信息并删除了 Servlet 的不必要部分。它看起来更好知道吗?
-
请在此处发布您的 web.xml 文件。