日期:2014-05-18  浏览次数:20817 次

session传值问题
我先在一个servlet类中.设置了一个session
                              Date   time=new   Date();
  String   uptime=new   SimpleDateFormat( "yyyyMMddhhmmss ").format(time);
  if(request.getSession().getAttribute( "uptime ")!=null){
  request.getSession().removeAttribute( "uptime ");
  }
  request.getSession().setAttribute( "uptime ",uptime);
然后在另外一个ACTION类中得到这个session中的值
String   uptime=(String)request.getSession().getAttribute( "uptime ");

发现在action中无法得到session值,而且我可以肯定的是,在servlet类中,设置session是成功的,,不知道是什么原因,在action中就得不到了.希望大家能帮忙解决下,谢谢

------解决方案--------------------
你打印下看看 兩次sessionID是否相同

------解决方案--------------------
HttpSession session;
if(request.getSession().getAttribute( "uptime ")==null){
session=request.getSession();
}
session.setAttribute( "uptime ",uptime);

我不知道你的那个作用域是不是有问题了。request

------解决方案--------------------
下面是对HttpServletRequest的getSession说明,其中有两个版本的getSession,在用户的一次登录中,在首次使用session时,采用getSession(true)这种方法,以后最好都要用getSession(false)这种方式,楼主最好看看在什么地方session实效了,那样解决的容易些。

http://java.sun.com/j2ee/1.4/docs/api/index.html

getSession
public HttpSession getSession()Returns the current session associated with this request, or if the request does not have a session, creates one.
Returns:
the HttpSession associated with this request
See Also:
getSession(boolean)

getSession
public HttpSession getSession(boolean create)Returns the current HttpSession associated with this request or, 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:
create - 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
See Also:
getSession()