Alireza Fattahi
Alireza Fattahi

Reputation: 45465

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

Upvotes: 7

Views: 12247

Answers (4)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

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);
    // ...
}

Upvotes: 9

Siddheshwaar Patil
Siddheshwaar Patil

Reputation: 33

use

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

it worked for me

Upvotes: 1

Roman C
Roman C

Reputation: 1

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();

Upvotes: 6

Pankaj Sharma
Pankaj Sharma

Reputation: 1853

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

or

use

ServletActionContext.getRequest()

Upvotes: 0

Related Questions