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 ?!

Roman C
  • 49,761
  • 33
  • 66
  • 176
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
  • 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
  • 42,517
  • 14
  • 123
  • 173
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
6

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
  • 49,761
  • 33
  • 66
  • 176
1

use

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

it worked for me

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
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,833
  • 1
  • 17
  • 22