request的生存期只限于服务器跳转
症状:
刚才想做一个实验,在a.jsp中向request添加属性(页面编码为UTF-8),在b.jsp中删除该属性(页面编码为ISO-8859-1),通过ServletRequestAttributeListener来观察是否删除成功。(目的是看页面编码会不会影响attribute name的比较。)
先在浏览器输入...a.jsp,回车,然后输入...b.jsp,回车
后来发现ServletRequestAttributeListener始终没有检测到request的属性被删除。
分析:
“先在浏览器输入...a.jsp,回车,然后输入...b.jsp,回车”
2个不同的request,访问b.jsp的时候已经是另一个request object了,注意client-side跳转与server-side跳转的区别
解决方案:
使用server-side跳转,例如<jsp:forward>
代码如下:
主要的思路就是,a.jsp提交一个参数arg1,作为attibute的key(或者说name),然后跳转(server-side跳转)到b.jsp,再输入另一个参数arg2,提交后如果arg1.equals(arg2)返回true,那么就能删除request找中由arg1作为key的attribute了
ServletRequestAttributeListener.java
public class ServletRequestAttributeListenerDemo implements ServletRequestAttributeListener {
public void attributeAdded(ServletRequestAttributeEvent srae){
System.out.println("** 增加request属性 --> 属性名称:" + srae.getName() + ",属性内容:" + srae.getValue()) ;
}
public void attributeRemoved(ServletRequestAttributeEvent srae){
System.out.println("** 删除request属性 --> 属性名称:" + srae.getName() + ",属性内容:" + srae.getValue()) ;
}
public void attributeReplaced(ServletRequestAttributeEvent srae){
System.out.println("** 替换request属性 --> 属性名称:" + srae.getName() + ",属性内容:" + srae.getValue()) ;
}
}
/*
<listener>
<listener-class>
org.foo.listenerdemo.ServletRequestAttributeListenerDemo
</listener-class>
</listener>
*/
request_attribute_remove1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 之所以要加上form来提交这个步骤,是因为页面编码无法影响到java的默认charset,所以如果直接在java代码中写上attrname的话估计起不到检验的作用 -->
<%
String attr_name1 = request.getParameter("attr_name1");
if (attr_name1!=null && !attr_name1.equals("")) {
session.setAttribute(attr_name1, "java") ;
// 只能用server-side跳转,否则request就被销毁了
request.getRequestDispatcher("request_attribute_remove2.jsp").forward(request, response);
}
%>
<form action="request_attribute_remove1.jsp" method="post">
要添加的属性名:<input type="text" name="attr_name1">
<input type="submit" value="添加属性">
</form>
</body>
</html>
request_attribute_remove2.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String attr_name2 = request.getParameter("attr_name2");
if (attr_name2!=null && !attr_name2.equals("")) {
session.removeAttribute(attr_name2) ;
}
%>
<form action="request_attribute_remove2.jsp" method="post">
Attribute name:<input type="text" name="attr_name2">
<input type="submit" value="Delete Attribute">
</form>
</body>
</html>
运行测试,Oops!又出问题了!
症状:
在a.jsp提交了一个参数"info"之后,console显示了"** 增加request属性 --> 属性名称:info,属性内容:java",然后跳转到b.jsp(server-side跳转),再提交参数"info",console没有显示任何内容!
分析:
form的action是client-side跳转,注意看浏览器的地址栏发生了变化!
解决方案:
目前我是把request换成了session,然后使用了一个HttpSessionAttributeListener
form action可以实现server-side跳转吗?
换成session之后,得出结论:只要提交的参数是ACSII字符,页面的编码即便不相同,也不会影响到内部String的比较,非ASCII字符,例如中文字符可能导致2个编码不一致的页面,即使是相同的字符串,equals的比较结果也是false(可以参考这篇文章:http://www.cnblogs.com/qrlozte/p/3516716.html)
代码如下:
HttpSessionAttributeListener
public class HttpSessionAttributeListenerDemo implements HttpSessionAttributeListener {
public void attributeAdded(HttpSessionBindingEvent se){
System.out.println(se.getSession().getId() + ",增加属性 --> 属性名称" + se.getName() + ",属性内容:" + se.getValue()) ;
}
public void attributeRemoved(HttpSessionBindingEvent se){
System.out.println(se.getSession().getId() + ",删除属性 --> 属性名称" + se.getName() + ",属性内容:" + se.getValue()) ;
}
public void attributeReplaced(HttpSessionBindingEvent se){
System.out.println(se.getSession().getId() + ",替换属性 --> 属性名称" + se.getName() + ",属性内容:" + se.getValue()) ;
}
}
/*
<listener>
<listener-class>
org.foo.listenerdemo.HttpSessionAttributeListenerDemo
</listener-class>
</listener>
*/
session_attribute_remove1.jsp --- 编码UTF-8
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 之所以要加上form来提交这个步骤,是因为页面编码无法影响到java的默认charset,所以如果直接在java代码中写上attrname的话估计起不到检验的作用 -->
<%
String attr_name1 = request.getParameter("attr_name1");
if (attr_name1!=null && !attr_name1.equals("")) {
session.setAttribute(attr_name1, "java") ;
request.getRequestDispatcher("request_attribute_remove2.jsp").forward(request, response);
}
%>
<form action="session_attribute_remove1.jsp" method="post">
要添加的属性名:<input type="text" name="attr_name1">
<input type="submit" value="添加属性">
</form>
</body>
</html>
session_attribute_remove2.jsp --- 编码ISO-8859-1
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String attr_name2 = request.getParameter("attr_name2");
if (attr_name2!=null && !attr_name2.equals("")) {
session.removeAttribute(attr_name2) ;
}
%>
<form action="session_attribute_remove2.jsp" method="post">
Attribute name:<input type="text" name="attr_name2">
<input type="submit" value="Delete Attribute">
</form>
</body>
</html>
request的生存期只限于服务器跳转的更多相关文章
- Web开发中的服务器跳转与客户端跳转
两者比较如下: 跳转类型 客户端请求次数 服务端响应次数 URL变化 站外跳转 作用域 服务器跳转 1 1 无 否 pageContext.request.session.application 客 ...
- servlet运行机制、Request内置对象和服务器端跳转
servlet运行机制: 当发送一个请求到服务器的时候,容器(Tomcat)会判断该路径属于哪一个 Servlet 进行处理,Servlet 有一个抽象父类“HttpServlet”,这个类是一个模板 ...
- jsp 表单提交,服务器跳转方法 浏览器重定向 及 servlet映射时 路径问题
在jsp页面中,等提交表单数据时,最好用觉得路径. 写法如下: <form action ="<%=request.getContextPath()%>/do_login. ...
- asp.net mvc Ajax服务器跳转
1.过滤器权限验证 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowM ...
- 为什么这个地方用重定向会报错.只能用 服务器跳转?? 为什么我加了过滤器,还是能直接登陆 servlet
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- Ext.ux.UploadDialog上传大文件 HTTP 错误 413.1 - Request Entity Too Large Web 服务器拒绝为请求提供服务,因为该请求实体过大。Web 服务器无法为请求提供服务,因为它正尝试与客户证书进行协商,但请求实体过大。
问题描述 问题:HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 原因:Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值(I ...
- jsp_属性范围_request
request属性范围表示在服务器跳转后,所有设置的内容依然会被保留下来.(服务器端跳转:页面跳转,地址栏不发生变化) 下面写个小例子测试下: (1)request_demo.jsp <%@ p ...
- JSP基础点滴
注释:<%-- 注释 --%> JSP中一共有3种Scriptlet代码.支持与HTML的代码混编. 第一种:<%%> 定义局部变量,编写语句. 第二种:<%!%> ...
- Java web每天学之Servlet工作原理详情解析
上篇文章中我们介绍了Servlet的实现方式以及Servlet的生命周期,我们这篇文章就来介绍一下常用对象. 点击回顾:<Java Web每天学之Servlet的工作原理解析>:<J ...
随机推荐
- Pycharm 2018 激活 亲测有效
下载 https://share.weiyun.com/5NVc5U3 并将 JetbrainsCrack-3.1-release-enc.jar 放置到 pycharm安装目录的\bin目录下( ...
- 为什么Domain controller上的time synchronization非常重要?
虚拟机默认情况下所拥有的资源都是不同的, 比如说CPU clock. 在一个忙碌的系统中, 虚拟机甚至可能在很短的一段时间内被拒绝分配资源给它, 这种情况还可能发生在高系统负荷, VMotion, B ...
- 更改DNS轻松访问google.com,FaceBook,Youtube等
将默认的Dns更改为42.120.21.30即可打开 https://www.google.com/ https://www.facebook.com/ https://www.youtube.com ...
- QPS、RT、PV、UV之间的关系
QPS: 每秒查询率(Query Per Second) ,每秒的响应请求数,也即是最大吞吐能力. QPS = req/sec = 请求数/秒 QPS统计方式 [一般使用 http_load 进行统计 ...
- GIL线程全局锁 协程
GIL线程全局锁 线程全局锁(Global Interpreter Lock),即Python为了保证线程安全而采取的独立线程运行的限制,说白了就是一个核只能在同一时间运行一个线程.对于io密集型任务 ...
- android中listview点击事件失效的灵异事件
首先说明一下我想实现的功能: 点击某个item之后,让其颜色发生变化.如果变化网上有很多例子,我就不班门弄斧了.Listview之所以点击没有反应是因为上图中绿色部分(自己定义的一个继承BaseAda ...
- PHP RESTful
PHP RESTful REST(英文:Representational State Transfer,简称REST) ,指的是一组架构约束条件和原则. 符合REST设计风格的Web API称为RES ...
- 算法笔记_029:约瑟夫斯问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 引用自<算法设计与分析基础>第三版: 约瑟夫斯问题,是以弗拉瓦斯.约瑟夫斯(Flavius Josephus)的名字命名的.约瑟夫斯是一 ...
- Set 和 WeakSet 数据结构
Set 和 WeakSet 数据结构是ES6新增. 它与数组非常相似,但是Set数据结构的成员都是唯一的. 特别说明:Set 中只能添加一个NaN 一.Set 数据结构: var set = new ...
- 《学习opencv》笔记——矩阵和图像操作——cvCrossProduct and cvCvtColor
矩阵和图像的操作 (1)cvCrossProduct函数 其结构 void cvCrossProdust(//计算两个三维向量的叉积 const CvArr* src1, const CvArr* s ...