java - 如何在 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 框架
解决方案
你可以从 ActionContext 得到它
ActionContext context = ai.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
推荐阅读
- python - Python - 球坐标有Z轴偏差
- apache - 将 phpmyadmin 安装到 VPS Ubuntu 16.04 时出现问题
- python - python - 登录页面并获取cookies
- python - Python在对列表中的数字进行排序时忽略零
- python - 如何从数据框中删除负时间?
- javascript - 简单的 Javascript:在数组上运行函数
- c# - 为什么我的应用程序随着时间的推移变得反应迟钝?
- javascript - 如何在 refFromURL() 中插入数组中的链接 - 存储 firebase
- c - 试图理解c中的死锁
- sql - 选择语句无法找到拼写错误的参数名称