`
jiangzhenwei6
  • 浏览: 34212 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

jsp 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

    博客分类:
  • jsp
阅读更多
在网上经常看到有人对request.getSession(false)提出疑问,我第一次也很迷惑,看了一下J2EE1.3 API,看一下官网是怎么解释的。
【官方解释】
getSession
public HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
If create is false and the request has no valid HttpSession, this method returns null.
To make sure the session is properly maintained, you must call this method before the response is committed. If the container is using cookies to maintain session integrity and is asked to create a new session when the response is committed, an IllegalStateException is thrown.
Parameters: true - to create a new session for this request if necessary; false to return null if there's no current session
Returns: the HttpSession associated with this request or null if create is false and the request has no valid session
译:
getSession(boolean create)意思是返回当前reqeust中的HttpSession ,如果当前reqeust中的HttpSession 为null,当create为true,就创建一个新的Session,否则返回null;
简而言之:
HttpServletRequest.getSession(ture) 等同于 HttpServletRequest.getSession()
HttpServletRequest.getSession(false) 等同于 如果当前Session没有就为null;
【问题和bug】:
我周围很多同事是这样写的;
复制代码 代码如下:

HttpSession session = request.getSession(); // a new session created if no session exists, 哈哈!完蛋啦!如果session不存在的话你又创建了一个!
String user_name = session.getAttribute("user_name");

需要注意的地方是request.getSession() 等同于 request.getSession(true),除非我们确认session一定存在或者sesson不存在时明确有创建session的需要,否则尽量使用request.getSession(false)。在使用request.getSession()函数,通常在action中检查是否有某个变量/标记存放在session中。这个场景中可能出现没有session存在的情况,正常的判断应该是这样:
复制代码 代码如下:

HttpSession session = request.getSession(false);
if (session != null) {
String user_name = session.getAttribute("user_name");
}

【投机取巧】:

如果项目中用到了Spring(其实只要是Java的稍大的项目,Spring是一个很好的选择),对session的操作就方便多了。如果需要在 Session中取值,可以用WebUtils工具(org.springframework.web.util.WebUtils)的 getSessionAttribute(HttpServletRequest request, String name)方法,看看高手写的源码吧:哈哈。。
复制代码 代码如下:

/**
* Check the given request for a session attribute of the given name.
* Returns null if there is no session or if the session has no such attribute.
* Does not create a new session if none has existed before!
* @param request current HTTP request
* @param name the name of the session attribute
* @return the value of the session attribute, or <code>null</code> if not found
*/
public static Object getSessionAttribute(HttpServletRequest request, String name) {
Assert.notNull(request, "Request must not be null");
HttpSession session = request.getSession(false);
return (session != null ? session.getAttribute(name) : null);
}

注:Assert是Spring工具包中的一个工具,用来判断一些验证操作,本例中用来判断reqeust是否为空,若为空就抛异常。
上面的代码又可以简洁一下啦,看吧:
复制代码 代码如下:

HttpSession session = request.getSession(false);
String user_name = WebUtils.getSessionAttribute(reqeust, "user_name");
分享到:
评论

相关推荐

    request.getSession().doc

    Request.getSession() 方法是 HttpServletRequest 对象中的一个方法,用于获取当前 HTTP 请求关联的 HttpSession 对象。如果当前会话不存在,可以通过 create 参数控制是否创建一个新的会话对象。 参数解释 * ...

    java 中 request.getSession(true、false、null)的区别

    本文将深入探讨`request.getSession(true)`、`request.getSession(false)`以及`request.getSession(null)`的区别,并提供最佳实践建议。 首先,让我们了解`getSession()`方法的基本行为。根据Servlet官方文档,`...

    request.setAttribute 语句前总显示红色感叹号解决办法 HTTP Status 500 -

    这种问题通常是由类型不匹配导致的,比如尝试将一个非Object类型的值传递给`setAttribute`方法。 #### 问题背景 根据题目中的描述,问题的核心在于: - **异常报告类型**:Exception report - **异常消息**:The ...

    数据库测试test.sql

    if(count2 == null){ //第一个用户 count2 = 1; }else{ count2++; } //3.再存放到application作用域中 context.setAttribute("count",count2); //...

    基于servlet的购物车

    //得到书号和书本对象 int bookid =Integer.parseInt(request.getParameter("id")); Map, Book&gt; books = (Map, Book&gt;)request.... request.getRequestDispatcher("listcart.jsp").forward(request, response);

    request.getParameter()取值为null的解决方法

    id属性用于在同一个页面内唯一标识该元素,便于JavaScript或CSS进行引用和样式控制;name属性则是提交到服务器的数据中的一部分,用于在服务器端通过`request.getParameter()`方法获取数据。 当在JavaScript中使用...

    jsp中request中的使用获取参数

    关于会话管理,`request`对象有一个`getSession()`方法,用于创建或获取与当前请求关联的`HttpSession`。这使得可以在多个页面之间共享数据: ```jsp HttpSession session = request.getSession(); session....

    JSP的Request对象练习源代码

    在Java服务器页面(JSP)技术中,`Request`对象是Servlet API的一部分,它代表了客户端发起的一个HTTP请求。这个对象是由服务器创建的,并在每次HTTP请求时传递给JSP页面,以便开发者能够访问和处理请求相关的数据。...

    jsp页经典面试题java程序员面试经常考到的面试题总结.pdf

    jsp页经典面试题java程序员面试经常考到的面试题总结 jsp是一种基于Java技术的服务器端编程语言,广泛应用于Web开发中。以下是jsp页经典面试题总结,涵盖了jsp的基本概念、jsp与ssi的混合使用、线程安全、html表单...

    JSPJSP.doc

    使用`getSession(true)`更为安全,因为它会确保始终有一个可用的`session`对象。 - `session.isNew()`:返回一个`Boolean`值,表示`session`是否为新创建的。如果是新创建的,则返回`true`。 - `request....

    java中相对路径,绝对路径问题总结.doc

    - 文件的绝对路径:`request.getSession().getServletContext().getRealPath(request.getRequestURI())` - Web应用的绝对路径:`servletConfig.getServletContext().getRealPath("/")` 总的来说,正确理解和使用...

    jsp代码 request

    通过`request.getSession()`方法,我们可以创建或获取一个`HttpSession`对象,用于存储跨多个请求的用户状态信息。 ### 五、安全注意事项 处理`request`对象时,应始终注意安全性。不要信任用户提交的数据,进行...

    jsp获取action传来的session和session清空以及判断.docx

    jsp获取action传来的session是jsp开发中的一种常见需求,Session是Web应用程序中的一种机制,用于存储用户的状态信息。jsp可以通过多种方式获取action传来的session,下面将详细介绍几种方法。 一、使用Struts标签...

    jsp登陆例子.。。。

    本示例是一个基本的JSP登录系统,旨在帮助初学者理解JSP如何处理用户输入、验证身份并管理会话。 ### 1. JSP基本结构 在JSP中,我们可以直接写HTML,同时插入Java代码片段(`&lt;%...%&gt;`)、Java表达式(`${...}`)和...

    于笑扬java综合知识点总结-必背(吐血推荐).doc

    * request.getSession(true):获取当前会话,如果不存在则创建一个新的会话 十一、Page和PageContext的区别 Page和PageContext都是JSP中的隐式对象,但它们有不同的特点: * Page:表示当前JSP页面,提供了页面的...

    Java-Web程序设计案例教程第四章-JSP内置对象.ppt

    它是一个输出流,可以用来输出各种类型的数据,包括基本数据类型、字符串、日期等。在案例中,我们看到了如何通过out.print()方法将不同类型的变量值输出到网页上。此外,out对象还可以用于调试,通过输出中间状态...

    jsp9大内置对象

    - `PrintWriter getWriter()`:获取一个`PrintWriter`对象,用于向客户端输出字符。 - `void setContentLength(int len)`:设置响应体的长度。 - `void setContentType(String type)`:设置响应的内容类型。 - `...

    jsp源码-网络交易系统

    jsp源码实例-网络交易系统 import="java.util.*,domain.*,struts.page.*" pageEncoding="GB2312" errorPage="error.jsp"%&gt; &lt;!DOCTYPE ...

Global site tag (gtag.js) - Google Analytics