Since you have an instance of javax.servlet.http.HttpServletResponse
, you can write the text directly. e.g.:
public class HelloWorldAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("We are send text plain");
return null;
}
}
With this way, you can send JSON, XML or binaries.
If you are combining traditional requests and Ajax (e.g., with jQuery.ajax
), may be you want to check for send an full HTML page or a fragment, JSON, and so on, with:
private final boolean isAjaxRequest(final HttpServletRequest request) {
final String header = request.getHeader("X-Requested-With");
return header != null && header.equalsIgnoreCase("XMLHttpRequest");
}