`

session.setAttribute()之问题

阅读更多

HTTPSession在setAttribute时,保存的对象是否需要序列化?

查看StandardSession源码中,在setAttribute()中有如下代码

 if ((manager != null) && manager.getDistributable() &&
          !isAttributeDistributable(name, value))
            throw new IllegalArgumentException
                (sm.getString("standardSession.setAttribute.iae", name));

 protected boolean isAttributeDistributable(String name, Object value) {
        return value instanceof Serializable;
 }

 manager接口:
	
    /**
     * Return the distributable flag for the sessions supported by
     * this Manager.
     */
    public boolean getDistributable();
	
    /**
     * Set the distributable flag for the sessions supported by this
     * Manager.  If this flag is set, all user data objects added to
     * sessions associated with this manager must implement Serializable.
     *
     * @param distributable The new distributable flag
     */
    public void setDistributable(boolean distributable);

 

在web.xml配置<distributable/>后, manager中的distributable变量为true,在setAttribute时会检查对象是否序列化。

 因此,session中的对象需不需要实现序列化,取决于是否支持分布式session。

分享到:
评论

相关推荐

    struts乱码问题

    HttpServletRequest request=... session.setAttribute("bookName", this.bookName); session.setAttribute("bookPrice", this.bookPrice); session.setAttribute("bookPress", this.bookPress); return SUCCESS;

    在JSP中使用Session制作简单的登录模块

    &lt;br&gt;if (Name.equals("mike") && Password.equals("1234")) { session.setAttribute("Login", "OK"); response.sendRedirect("Member.jsp"); } else { out.println("登录错误,请输入正确名称...

    很经典的登录回选。

    // session.setAttribute("redirectUrl", request.getRequestURL().append("?").append(request.getQueryString()).toString()); String url = request.getParameter("url"); URLEncoder.encode(URLDecoder....

    java小项目

    基于MVC的小项目 ... session.setAttribute("userName", name); session.setAttribute("pwd", pwd); session.setAttribute("msgList", msgList); response.sendRedirect("jspPages/index.jsp"); } } }

    猜数题,JSP,全是JSP页面,没有SERVLET和BEAN

    session.setAttribute("count", new Integer(0)); session.setAttribute("save", new Integer(num)); session.setAttribute("max",new Integer(100)); session.setAttribute("min",new ...

    登陆jsp数据库验证的代码

    session.setAttribute("upassword",rs.getString(2)); session.setAttribute("userjb",rs.getString(3)); response.sendRedirect("sgly/gg-index.jsp"); rs.close(); stmt.close(); conn.close(); } ...

    Servlet解耦

    public class NoloCAction ... session.setAttribute("bookName", this.bookName); session.setAttribute("bookPrice", this.bookPrice); session.setAttribute("bookPress", this.bookPress); return SUCCESS; } }

    Java类写的随机验证码

    session.setAttribute("random", RS);//在servlets.RandomServlet中。 3、验证验证码 //取出提交的验证码(用户写的) String code = request.getParameter("code"); //取出生成的验证码(系统生成的) HttpSession ...

    jsp基础精华详解

    session.setAttribute("name", "zhangsan"); session.setAttribute("pwd", "aaa"); String name = (String) session.getAttribute("name"); 2.cookie: //创建Cookie Cookie cookie = new Cookie("name", "zhangsan...

    JavaScript中setAttribute用法介绍

    我们经常需要在JavaScript中给Element动态添加各种属性,这可以通过使用setAttribute()来实现,这就涉及到了浏览器的兼容性问题

    可以查看上线人数 监听器 servlet Ajax聊天系统

    session.setAttribute("list",ut);//把ut放入session中 list.addUserInfo(ut.getUser());//把用户名放入用户对象数组中 session.setMaxInactiveInterval(100);//设置session时间 Vector vector=list.getList(); //...

    Springmvc框架代码

    public String method3(@RequestParam(value="username",defaultValue="",... session.setAttribute("username", username); } System.out.println(username); System.out.println(1111); return "hello";

    jsp-.rar_flag提交

    这样才可以让用户主动的刷新jsp而从新生成一个session.setAttribute("flag").如果不这样flag值不会更新,返回以后的操作数据是不会更新到数据库的。 (页面过期的机制还不完全了解,只知道一种实现方法但觉得不怎么...

    SSH_appp.zip

    session.setAttribute(Constants.DEV_USER_SESSION, user); //页面跳转(main.jsp) return "redirect:/dev/flatform/main"; }else{ //页面跳转(login.jsp)带出提示信息--转发 request.setAttribute("error...

    基于JSP的Web聊天室的设计与实现

    session.setAttribute("password",code); byte[] temp = name.getBytes("iso8859-1"); name= new String(temp); //get parameter try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection c=...

    java 实现的数字验证

    用与java web 开发,java 实现的数字验证码的生成 ...session.setAttribute("rand",image.sRand); // 输出图象到页面 ImageIO.write(image.creatImage(), "JPEG", response.getOutputStream()); %&gt;

    用户管理系统(ums)

    用户管理系统。... session.setAttribute("userList", list); //跳转到显示的页面,格式(得到当前页面的+要跳转的页面) response.sendRedirect(request.getContextPath()+"/manager.jsp"); }

    比较简单的添加购物车,不过只有一个小程序

    session.setAttribute("cart", cart); } String id=request.getParameter("id"); List&lt;CartItem&gt; items=(List)this.getServletContext().getAttribute("items"); CartItem item=null; for(CartItem i ...

    struts2 session 解读

    HttpSession &lt; session = ServletActionContext.getRequest().getSession(); 例: public String findAll(){ HttpSession session = ... session.setAttribute(“list”, list); return this.SUCCESS;

    一个简单java验证码

    session.setAttribute("number", number); 只要获得该session就可以生成随机验证码, 简单易用,需要的朋友可用看看

Global site tag (gtag.js) - Google Analytics