我想在我编写的拦截器中看到对网页的请求。我将根据传入请求中的某些值更改我的响应值。我将使用类似的东西;
String ex = request.getHeader("GET");
if(ex.contains("addHeader("a","example")"));
response.setHeader("a","null");
这是我的索引.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 框架
最佳答案
可以从ActionContext中获取
ActionContext context = ai.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
关于java - 如何在Java类中访问HTTP请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57818201/