首页 > 解决方案 > 如何在 Java 类中访问 HTTP 请求

问题描述

我想在我编写的拦截器中查看对网页的请求。我将根据传入请求中的一些值更改我的响应值。我将使用类似的东西;

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

这是我的 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>

这是我的 TestInterceptor.java 类的一部分;

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;
}

使用该功能的解决方案或其他方式是什么。感谢您的帮助。注意:我正在使用 Struts 框架

标签: javastruts2interceptorstruts2-interceptors

解决方案


你可以从 ActionContext 得到它

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

推荐阅读