1

I'm using Apache Tomcat v8.0 server and in Java EE application. Basically, I created an ResponseUpload servlet. I need to get JSON data from the web app through POST request. Here it is the code of the 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);
}

Then to test if it works I added a form to jsp file, doing POST to url:

http://localhost:8080/webApp/RequestUpload

The form:

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

But I got 404 error -

HTTP Status 404 - /webApp/RequestUpload

Can somebody show me where is my mistake?

My web.xml file:

<?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. Another form doing the same with RPC servlet (which structure is similar) is working.

2 Answers2

2

After a research and with help of previous answer I found the problem in build path. The build path image

I had to allow output folders for source folder and change the output folder from build to WEB-INF/classes.

1

Servlet Class:

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>

if your context root of the web app is webApp and server is running on port:8080. you can see the home.jsp as follows:

enter image description here

When data are given in first_name as Minhajur and last_name as Rahman, After clicking the submit button, following json response will be returned: enter image description here

Note: make sure that you have added the required jar dependencies in lib folder. For example: gson-1.7.1.jar exists in webapp/WEB-INF/lib folder.

Trouble Shooting:

HTTP Status 404 - /webApp/RequestUpload

-> Check whether your webApp has been compiled successfuly and your compiled RequestUpload.class has been existed on your webapp/WEB-INF/classes directory on tomcat server.

if RequestUpload.class does not exist, then

1) It has not been compiled successfully. or

2) the Compiled class files has not been moved to webapp/WEB-INF/classes directory.

So, follows the following steps:

i) Clean your tomcat working directory and compile run again and check the class file is existed there.

ii) Delete and Add Tomcat server on eclipse again. then follow the previous steps.

You can also read for further references: How to use Servlets and Ajax?

Community
  • 1
  • 1
CrawlingKid
  • 959
  • 5
  • 15
  • The problem is that there is a 404 error accessing "/RequestUpload". I didn't test the body of the POST. Anyway, thank you for your response – Igor Shulgan Mar 25 '16 at 20:08
  • Did you check whether the jsp is being displayed or not? if you are using eclipse, you could also check the context root of the project by checking Project-> Property->WebProjectSettings. Check the port number of tomcat. For example, if the port number is 8081 and context root of the project is jsondemo. Jsp file name is index.jsp stored under webapp directory. check whether you can see form window when accessing the url: http://localhost:8081/jsondemo. – CrawlingKid Mar 25 '16 at 20:22
  • Yes, since I have another form that is working. Moreover, if I just change the 'RequestUpload' to 'RPC', the form will work. So, it's very strange situation for me. – Igor Shulgan Mar 25 '16 at 20:26
  • if you change @WebServlet("/RequestUpload") to @WebServlet("/RPC"), but not the classname and jsp form action to action= "RPC", is it working? – CrawlingKid Mar 25 '16 at 20:31