1.session.setAttribute();If the object being stored in the session object implements HttpSessionBindingListener, then the container calls HttpSessionBindingListener.valueBound. If you have an invalidated session then this function throws an IllegalStateException. 2.request.setAttribute();
跟session的方法不同,你只能使用在一个request里面
要想使用里面保存的对象,只能在include或者forward的页面使用
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);request.setAttribute("name", "test");dispatcher.forward(request, response);
//那么第二个页面也能使用request里面的对象要是该request不是从dispatcher传递过去的
里面不会包含先前设置的对象

解决方案 »

  1.   

    我是用的dispatcher传递request。
    我想问的是
    如果我要把login的user信息一直带到后面的话,是不是一直只用一个request,这样forward来forward去?
      

  2.   

    另外我想问一下,
    session和request到底有没有包含关系?
    我看到request可以得到或者创建一个新的session,
    如果我在jsp叶面不是用userbean方式来使用我的bean,而是在servlet中将bean实例存入request中,再dispatcher到页面,这样作对不对?
    要是能这样做的话,再次提交jsp的时候,我刚才存在request中的bean实例会不会还在?还是说,每次submit之后都要生成一个新的request?
    谢谢了。实在感激!
      

  3.   

    在servlet中将bean实例存入request中,再dispatcher到页面是对的
    Http是一个无状态的连接,新的request不会保存上个request的数据,
    唯有通过传递request对象的方法。
    你的情况最好还是使用session吧
      

  4.   

    唯有通过传递request对象的方法。这句话不太明白,
    怎样传递呢?
    比如在新的request要提交的时候,也就是每次submit()的时候,怎样把上次的request给传递到servlet中呢。还是在jsp的submit()之前,写....forward(request,response)吗?
      

  5.   

    "唯有通过传递request对象的方法。"
    就是只有通过RequestDispatcher将当前的
    request传递到下一个页面