(备忘录)response.sendRedirect() 的session丢失问题
来源:互联网 发布:阿里云系统机顶盒root 编辑:程序博客网 时间:2024/05/29 21:31
问题描述:login.jsp是登陆页面,login_action.jsp是处理登陆数据的页面,welcome.jsp是登陆成功后的欢迎界面。
如果login传过来的用户名和密码已经存在,在welcome.jsp中就用到这个用户名,于是写了如下代码:
session.setAttribute("username",username); response.sendRedirect("welcome.jsp");
结果在welcome.jsp中无法通过(String)session.getAttribute("username")取到值。
解决方法:
用request.getRequestDispatcher("welcome.jsp").forward(request, response);替换
response.sendRedirect("welcome.jsp");
原理:
使用时response.sendRedirect(),如果是跨域跳转,则session会丢失,否则不会。
例如:在本地机上有项目名问web1和web2的两个项目同时运行,从web1跳转到web2当然是跨域跳转,session丢失。但如果是从地址http://localhost:8080/web1跳转到http://127.0.0.1:8080/web2也同样会丢失session。
在web1的servlet中执行response.sendRedirect("/web1/page2.jsp")是域内跳转,session不会丢失,但保存在request中的属性值在page2.jsp中无法取到,也就是说在page2.jsp中通过request.getAttribute()的方法无法取得值,所以只能通过把参数写到url中的方式传递参数,例如:response.sendRedirect("/web1/page2.jsp?action=select").
因为forward()就是转发。sendRedirect就是重定向。两者最大的区别就是一个是:
forward()执行后还是在同一个requestrequest范围,而sendRedirect执行之后就不在同一个request范围里面。
RequestDispatcher.forward()是在服务器端起作用,当使用forward()时,Servletengine传递HTTP请求从当前的Servlet or JSP到另外一个Servlet,JSP 或普通HTML文件,也即你的form提交至a.jsp,在a.jsp用到了forward()重定向至b.jsp,此时form提交的所有信息在 b.jsp都可以获得,参数自动传递.
response.sendRedirect()是在用户的浏览器端工作,sendRedirect()可以带参数传递,比如servlet?name=frank传至下个页面,同时它可以重定向至不同的主机上,且在浏览器地址栏上会出现重定向页面的URL.
response.sendRedirect() 的session丢失问题
http://blog.163.com/jackie_howe/blog/static/199491347201262673810183/
response.sendRedirect() 的session丢失问题
http://blog.csdn.net/zsy_gemini/article/details/2840665
- (备忘录)response.sendRedirect() 的session丢失问题
- response.sendRedirect() 的session丢失问题
- response.sendRedirect() 的session丢失问题
- ASP.NET Response.Redirect 丢失 Session的问题
- 解决response.sendRedirect引出的中文问题
- 使用response.sendRedirect()时的中文问题
- 有关jsp中的response.sendRedirect()的问题
- 有关jsp中的response.sendRedirect()的问题???
- JSP的response.sendRedirect()中文参数问题
- iframe和response.sendRedirect使用的问题
- iframe和response.sendRedirect使用的问题
- iframe和response.sendRedirect使用的问题
- response.sendRedirect 传递参数的问题
- iframe和response.sendRedirect使用的问题
- response.sendRedirect()中session与request的异同
- Response.Redirect导致session丢失的解决办法
- Session丢失的问题!
- response.sendRedirect的注意事项
- pthread_mutex_lock的实现!!
- java 之 泛型
- OpenStack python库: from oslo.config import cfg ImportError: No module named config
- VC++-标准模板库STL(Standard Template Library)
- Python 基础语法
- (备忘录)response.sendRedirect() 的session丢失问题
- iOS开发之静态库
- POJ3984 迷宫问题【BFS】
- oracle服务器配置监听,供本地或远程操作数据库
- MYSQL-Access denied for user 'root'@'localhost' (using password:NO) 解决记录
- JS 对输入框进行限制(常用的都有)
- Linux的运行级别和chkconfig用法
- Eclipse中server启动超时的解决方法
- hadoop集群删除节点