0

I am new to Spring MVC and I had a doubt about accessing the HttpServletResponse object in a servlet.

Basically my code is something like this:

@RequestMapping("/part of requesturl")
@ResponseBody String methodName(HttpServletRequest request)
{
    //All the computations which build up a JSon

    return json;
}

Right now this json file is being displayed on the browser as a response. I need to force it to be downloaded to the disk of the user irrespective of the browser. By looking at the other posts, I figured I should do someting like this:

response.setContentType("application/force-download");

How do I get the response object here? Also, if someone could shed some more light on forcing download onto the browser that would be great. Thanks in advance!

Prakash K
  • 11,537
  • 5
  • 49
  • 107
Prasad K
  • 153
  • 3
  • 8

1 Answers1

3

How do I get the response object here ?

Very intuitive:

@ResponseBody String methodName(
        HttpServletRequest request, 
        HttpServletResponse response
) {
    response.setContentType("application/force-download");
        //...
}

Also, if someone could shed some more light on forcing download onto the browser that would be great.

See:

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 311,858
  • 65
  • 665
  • 652