2

I've written a controller class and an action method like the

@Controller
public class Controller
{
    @RequestParam("/test.htm")
    public String action(Model model)
    {
         //Something
    }
}

How to get HttpServletRequest object in action's method body?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 1
    you might want to take a look at here: http://stackoverflow.com/questions/8504258/spring-3-mvc-accessing-httprequest-from-controller – tt_emrah Jul 21 '14 at 07:55

2 Answers2

4

You can get the instance of HttpServletRequest by passing a reference in your action method:

@Controller
public class Controller
{
    @RequestParam("/test.htm")
    public String action(Model model, HttpServletRequest request)
    {
        //Something
        try {
            request.getParameter("user_name"); //write in try-catch block
        } catch(ServletException ex) {
        }
    }
}
Pool
  • 11,999
  • 14
  • 68
  • 78
one
  • 108
  • 5
1

Spring MVC readily avails HttpServletRequest , HttpServletResponse and many other classes for processing:

Directly add HttpServletRequest in your method argument as below :

@RequestParam("/test.htm")
public String action(Model model ,  HttpServletRequest request)
{
     //Something
}
Pramod S. Nikam
  • 4,271
  • 4
  • 38
  • 62