0

I have copied some code (the example is taken from http://hmkcode.com/java-servlet-send-receive-json-using-jquery-ajax/

it works but there is something I do not understand: I cannot understand how the "articles" retains provious values Is it not reinitialized with each call of the servlet ?

public class JSONServlet extends HttpServlet {

   private static final long serialVersionUID = 1L;

  // This will store all received articles
  List<Article> articles = new LinkedList<Article>();


protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{

    // 1. get received JSON data from request
    BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
    String jsonString = "";
    if (br != null){
        jsonString = br.readLine();
    }
    ObjectMapper mapper = new ObjectMapper();

    Article article = mapper.readValue(jsonString, Article.class);

    response.setContentType("application/json");

    articles.add(article);

    mapper.writeValue(response.getOutputStream(), articles);


 }
}
Makoto
  • 765
  • 2
  • 17
  • 45

3 Answers3

1

The application server usually maintains a single instance of a servlet, so declaring the List<Article> articles as attribute in your servlet class will work as a container for the entire application for your articles.

Note that this approach should be for testing purposes only. An real world application designed like this will fail because a servlet is accessed by multiple threads at the same time and several requests on the same URL attended by your servlet that try to add the data into this unsynchronized list will raise ConcurrentModificationException.

In case you want/need to store data per client (browser), use session scope. In case you want/need to store data per application (available to all clients of your application), use application scope.

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
1

Only one servlet instance is created at any given time in a web app. Because the articles field is stored as a member variable, (not local to the method), it is shared among all requests (and all threads). This is typically a very bad practice as you could run into concurrency issues with multiple threads attempting to access the same data structure at the same time and security issues with users able to access data they perhaps shouldn't have access to.

The correct way to do this would be to use session to store data that should be private to each user, or use something like Spring's SessionScopedProxy support.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
1

I am not an expert on Java servlets, but once the servlet is initialized (a.k.a your JSONServlet class), the articles List is initialized, and subsequent calls of doPost via clients are appending to the articles list, it is not being re-initialized every time a POST is called. It will only be deleted and re-initialized when you restart your servlet.