7

To get the HttpServletRequest in an interceptor I used below code:

HttpServletRequest request =(HttpServletRequest) ActionContext.getContext().get(HTTP_REQUEST);

I tried to implement ServletRequestAware in the interceptor but it did not worked.

Are there any better ways to get HttpServletRequest in an Interceptor ?!

BalusC
  • 1,040,783
  • 362
  • 3,548
  • 3,513
Alireza Fattahi
  • 38,349
  • 12
  • 107
  • 156
  • Why do you need `HttpServletRequest` in interceptor? – Aleksandr M Oct 08 '13 at 07:29
  • I want to develop and interceptor which prevents Ajax request from calling directly by url. Please see http://stackoverflow.com/questions/14621539/how-to-determine-whether-a-request-is-ajax-or-normal. This interceptor will be in interceptor stack which will prevent these requests. – Alireza Fattahi Oct 08 '13 at 07:56

4 Answers4

9

You need to use ActionInvocation#getInvocationContext() to retrieve your request.

public String intercept(ActionInvocation invocation) throws Exception {
    ActionContext context = invocation.getInvocationContext();
    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
    // ...
}
Alireza Fattahi
  • 38,349
  • 12
  • 107
  • 156
Ravi K Thapliyal
  • 49,621
  • 9
  • 73
  • 89
5

The servlet stuff you could get referencing servletConfig interceptor. After this interceptor is invoked you could get servlet stuff from ServletActionContext.

HttpServletRequest request = ServletActionContext.getRequest();
Roman C
  • 48,723
  • 33
  • 63
  • 158
1

use

final HttpServletRequest request = (HttpServletRequest) ActionContext.getContext()
                                               .get(ServletActionContext.HTTP_REQUEST);

it worked for me

Aleksandr M
  • 23,988
  • 12
  • 67
  • 136
0

you will get ActionInvoction try getInvocationContext() it will return instance of "ActionContext" try .get(HTTP_REQUEST); on this.

or

use

ServletActionContext.getRequest()
Pankaj Sharma
  • 1,795
  • 1
  • 16
  • 22