Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get HttpServletRequest in Struts 2 interceptor

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

like image 289
Alireza Fattahi Avatar asked Oct 08 '13 07:10

Alireza Fattahi


3 Answers

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);
    // ...
}
like image 170
Ravi K Thapliyal Avatar answered Oct 19 '22 14:10

Ravi K Thapliyal


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();
like image 30
Roman C Avatar answered Oct 19 '22 15:10

Roman C


use

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

it worked for me

like image 1
Siddheshwaar Patil Avatar answered Oct 19 '22 16:10

Siddheshwaar Patil