症状:

  刚才想做一个实验,在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. iOS:切换视图的第三种方式:UITabBarController标签栏控制器

    UITabBarController:标签栏控制器 •通过设置viewControllers属性或者addChildViewController方法可以添加子控制器 –NSArray *viewCon ...

  2. SQL Server快速部署作业到多台服务器

    问题: 需要在很多的SQL Server服务器上创建相同的作业.我们可以一台一台的运行相同的脚本创建作业,但是有没有什么简便的做法呢? 解决方法: 可能很多人都没有注意到可以用多服务器环境管理SQL ...

  3. EasyUI-EasyUI框架入门学习

    前言 新项目的开发前端技术打算采用EasyUI框架(基于EasyUI较为丰富的UI组件库),项目组长将前端EasyUI这块的任务分配给了我.在进行开发之前,需要我这菜鸟对EasyUI框架进行一些基础的 ...

  4. 使用python读取word

    使用python读取word 官网:https://python-docx.readthedocs.io/en/latest/ 示例:https://blog.csdn.net/u010911997/ ...

  5. 用CSS下划线距离

    但在我在CSS中新加了TEXT-DECORATION: underline; 后发现下划线离文本太近了,很难看. 代码一: a { text-decoration: none; background: ...

  6. C#应用视频教程1.2 Socket通信客户端实现

    接下来我们尝试实现最简单的Socket客户端,为了确保只可能你的代码有问题,服务器要先用别人成熟的代码测试(这也是编程的一个技巧,先不要用自己写的客户端测试自己写的服务器,这样出了问题你也不知道谁有问 ...

  7. UVA11402 - Ahoy, Pirates!(线段树)

    UVA11402 - Ahoy, Pirates!(线段树) option=com_onlinejudge&Itemid=8&category=24&page=show_pro ...

  8. java.lang.IllegalArgumentException: taglib definition not consistent with specification version

    web.xml报错 taglib标签错误,3.0要用jsp-config <jsp-config>    <taglib>        <taglib-uri>& ...

  9. scribe 搭建遇到的问题

    1.如果安装了多个boost版本或boost路径不是scribe脚本指定的话,会出现问题: checking whether the Boost::System library is availabl ...

  10. 38、各Set实现类的性能分析

    HashSet和TreeSet是Set的两个典型实现,到底如何选择HashSet和TreeSet呢?HashSet的性能总是比TreeSet好(特别是最常用的添加.查询元素等操作),因为TreeSet ...