1

I do an AJAX call using $.get() method and it's response is sent using the following servlet response object :

public void getStatisticsIndividualAnalysis() throws ParseException{

     HttpServletResponse response = ServletActionContext.getResponse();
     response.setContentType("text/plain");  
     List<JSONObject> jsonReturnList = new ArrayList<JSONObject>(); // LIST to be returned through RESPONSE object

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

I am using Struts2 framework. I feel using servlet response object is a bad practice. Is there a better approach to send response?? Please help by providing some code!

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101

1 Answers1

0

In my opinion, there is nothing wrong in your approach, as you want to write directly to the response. The question could be on what could be the best way to access the response object. There are couple of ways I am aware of - e.g., implementing ServletResponseAware interface, but that ties your class to servlet environment. The docs state that

using this interface makes the Action tied to a servlet environment, so it should be avoided if possible since things like unit testing will become more difficult.

The other method is obviously what you have used - using ServletActionContext.

Shailendra
  • 8,874
  • 2
  • 28
  • 37