症状:

  刚才想做一个实验,在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的生存期只限于服务器跳转的更多相关文章

  1. Web开发中的服务器跳转与客户端跳转

    两者比较如下: 跳转类型  客户端请求次数 服务端响应次数 URL变化 站外跳转 作用域 服务器跳转 1 1 无 否 pageContext.request.session.application 客 ...

  2. servlet运行机制、Request内置对象和服务器端跳转

    servlet运行机制: 当发送一个请求到服务器的时候,容器(Tomcat)会判断该路径属于哪一个 Servlet 进行处理,Servlet 有一个抽象父类“HttpServlet”,这个类是一个模板 ...

  3. jsp 表单提交,服务器跳转方法 浏览器重定向 及 servlet映射时 路径问题

    在jsp页面中,等提交表单数据时,最好用觉得路径. 写法如下: <form action ="<%=request.getContextPath()%>/do_login. ...

  4. asp.net mvc Ajax服务器跳转

    1.过滤器权限验证 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowM ...

  5. 为什么这个地方用重定向会报错.只能用 服务器跳转?? 为什么我加了过滤器,还是能直接登陆 servlet

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

  6. Ext.ux.UploadDialog上传大文件 HTTP 错误 413.1 - Request Entity Too Large Web 服务器拒绝为请求提供服务,因为该请求实体过大。Web 服务器无法为请求提供服务,因为它正尝试与客户证书进行协商,但请求实体过大。

    问题描述 问题:HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 原因:Web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值(I ...

  7. jsp_属性范围_request

    request属性范围表示在服务器跳转后,所有设置的内容依然会被保留下来.(服务器端跳转:页面跳转,地址栏不发生变化) 下面写个小例子测试下: (1)request_demo.jsp <%@ p ...

  8. JSP基础点滴

    注释:<%-- 注释 --%> JSP中一共有3种Scriptlet代码.支持与HTML的代码混编. 第一种:<%%>  定义局部变量,编写语句. 第二种:<%!%> ...

  9. Java web每天学之Servlet工作原理详情解析

    上篇文章中我们介绍了Servlet的实现方式以及Servlet的生命周期,我们这篇文章就来介绍一下常用对象. 点击回顾:<Java Web每天学之Servlet的工作原理解析>:<J ...

随机推荐

  1. 数据库迁移利器:Migrator.Net

    几年前在做项目中第一次接触到了Migrator.Net,就深深被吸引住了,至此以后在新的大项目中,我都会使用Migrator.Net来创建或者更新数据库架构.曾经在项目中也发现了小bug并提交给了作者 ...

  2. Ubuntu 所需要的中文字体美化操作步骤

    中文字体美化是个很讨厌的事情,无数初学者在这里面浪费了无数时间,做了无数没有意义的事情.但这也是不得不做的,我把 Debian/Ubuntu 所需要的中文字体美化操作步骤详细记录在这里,希望能节约大家 ...

  3. eclipse 添加 Courier New 字体

    下载了eclipse3.7,发现这个版本默认的字体不是Courier New,后来通过eclipse->windows->preferences->general->apper ...

  4. Oracle spatial抽稀函数(SDO_UTIL.SIMPLIFY)

    在使用Oracle spatial做空间查询和展示时,经常会遇到展示或者查询过慢,这时候我可以通过空间数据抽稀来优化查询展示效率. 在Oracle spatial中的抽稀函数为:SDO_UTIL.SI ...

  5. Discuz常见小问题-如何设置QQ邮箱注册验证

    开启POP3/SMTP服务器,需要发送短信,然后点击我已发送,得到一个指定的密码   开启之后得到的授权码   开启IMAP/SMTP还需要发送一次短信,得到一个另外的授权码   然后在后台设置为下面 ...

  6. WCF 之 消息契约(MessageContract)

    对于SOAP来说主要由两部分构成Header和Body,他们两个共同构成了SOAP的信封,通常来说Body保存具体的数据内容,Header保存一些上下文信息或关键信息. 比如:在一些情况下,具有这样的 ...

  7. 转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes)

    转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl ...

  8. 使用PyQt4 designer时无法启动uic解决方案

    1.自己调用命令行 pyuic4 -o codeFile.py -x yourUIfile.ui 2.写一个批处理,每次双击就行了,跟你的源文件放在同一文件夹下 import os for root, ...

  9. div浏览器兼容问题

    1. 默认的内外边距不同 问题: 各个浏览器默认的内外边距不同 解决: *{margin:0;padding:0;} 2. 水平居中的问题 问题: 设置 text-align: center ie6- ...

  10. &lt;LeetCode OJ&gt; 78 / 90 Subsets (I / II)

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...