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);
}
}