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 ...
随机推荐
- Thinkphp学习笔记1-URL模式
PATHINFO模式 PATHINFO模式是系统的默认URL模式,提供了最好的SEO支持,系统内部已经做了环境的兼容处理,所以能够支持大多数的主机环境.对应上面的URL模式,PATHINFO模式下面的 ...
- 使用ReportStudio打开cube模型创建报表出现两个最细粒度名称
本人也是第一次遇到这样的问题,此问题甚是简单,也许很简短的一句话就可以解决这个问题了,看官请留神哦 cube做好发布到cognos之后使用Analysis Studio打开结构正常 于是想到要用此数据 ...
- (剑指Offer)面试题58:二叉树的下一个结点
题目: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 思路: 考虑中序遍历的过程, 如果当前结点存在右子节点, ...
- mysql安装错误总结
1.若在启动mysql服务时出现如下错误,可查看错误日志找出错误原因. Error:Starting MySQL.The server quit without updating PID file ( ...
- Arrays.asList的用法
使用工具类Arrays.asList()把数组转换成集合时,不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportOperationException异常说明: ...
- CommonClassLoader或SharedClassLoader加载的Spring如何访问并不在其加载范围内的用户程序呢
Question 引自<深入理解Java虚拟机—JVM高级特性与最佳实践>9.2.1,p235 如果有10个WEB应用程序都是用spring来进行组织管理的话,可以把Spring放到Com ...
- 点击div和某些控件之外的地方隐藏div,点击div不隐藏。对象 click和document click冲突有关问题
帮朋友解决这个问题,我发现用以往想想像的方式来实现,貌似不太可行,所以从网上找了一些解决办法,进行优化,这篇比较详细,所以拿来备忘,另一方面也希望可以帮助需要的同学! 问题背景:jQuery事件问题! ...
- Android API之android.os.AsyncTask
android.os.AsyncTask<Params, Progress, Result> AsyncTask enables proper and easy use of the UI ...
- .net mvc 站点自带简易SSL加密传输 Word报告自动生成(例如 导出数据库结构) 微信小程序:动画(Animation) SignalR 设计理念(一) ASP.NET -- WebForm -- ViewState ASP.NET -- 一般处理程序ashx 常用到的一些js方法,记录一下 CryptoJS与C#AES加解密互转
.net mvc 站点自带简易SSL加密传输 因项目需要,传输数据需要加密,因此有了一些经验,现简易抽出来分享! 请求:前端cryptojs用rsa/aes 或 rsa/des加密,后端.net ...
- python pandas groupby
转自 : https://blog.csdn.net/Leonis_v/article/details/51832916 pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对 ...