javastruts2interceptorstruts2-interceptors

How to reach HTTP Request in Java class


I want to see the request to the web page in an Interceptor I wrote. I will change my response value according to some values in the incoming request.I'm gonna use something like that ;

String ex = request.getHeader("GET");
if(ex.contains("addHeader("a","example")"));
     response.setHeader("a","null");

Here is my index.ft:

Your name: <@s.url value="${name}"/>
Enter your name here:<br/>
<form action="" method="get">
<input type="text" name="name" value="" />
<input type="submit" value="Submit" />
</form>

Here is part of my TestInterceptor.java class;

public class TestInterceptor implements Interceptor {
....
@Override
public String intercept(ActionInvocation ai) throws Exception {
    System.out.println("before");

    //the area where I want to write the codes I want to use above
    // I can't to reach request.getHeader(...) function in here
    String result = ai.invoke();
    System.out.println("after");
    return result;
}

What's the solituon or another way to use that functions. Thanks for helping. Note : I'm using Struts framework


Solution

  • You can get it from ActionContext

    ActionContext context = ai.getInvocationContext();
    HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);