haipeng01 发表于 2010-7-12 16:42

【求助】关于session问题

求助各位大侠,我想把查询出来的数据经过处理之后放到session中,程序上应该怎样实现,如何把数据放到session中

baifenghan 发表于 2010-7-12 21:09

如果是在使用servlet,那么可以使用HttpServletRequest这个类的方法:
HttpSession getSession()
          Returns the current session associated with this request, or if the request does not have a session, creates one.
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.

如果是使用JSP,那么有一个内置对象,就是session.

hsh_1987 发表于 2010-7-12 22:08

简单的说:
1、Servlet
HttpSession session = request.getSession();
session.setAttribute("自定义名字", 需要放到session的数据);

2、Jsp(session是Jsp的一个内置对象,直接使用)
session.setAttribute("自定义名字", 需要放到session的数据);

lampeter123 发表于 2010-7-13 08:21

简单来说就是得到Session对象后,用setAttribute()方法存放数据,用getAttribute()方法取出数据

panghaichao 发表于 2010-8-9 11:44

1、Servlet
HttpSession session = request.getSession();
session.setAttribute("自定义名字", 需要放到session的数据);

2、Jsp(session是Jsp的一个内置对象,直接使用)
session.setAttribute("自定义名字", 需要放到session的数据);  

3、取的时候
session.getAttribute("自定义名字");  

sunkaidong 发表于 2010-8-9 12:42

可以使用bean的。用类做封装。

zheng840 发表于 2010-8-31 12:45

支持上面的,,

ablikim 发表于 2010-8-31 14:23

[quote][b]以下是引用[u]panghaichao[/u]在2010-8-9 11:44:06的发言:[/b]

1、Servlet
HttpSession session = request.getSession();
session.setAttribute("自定义名字", 需要放到session的数据);

2、Jsp(session是Jsp的一个内置对象,直接使用)
session.setAttribute("自定义名字", 需要放到session的数据);  

3、取的时候
session.getAttribute("自定义名字");  
[/quote]同意这个看法,好比把“自定义的名字”换成你要存的数据就可以了,比如说username,password。

wei0000 发表于 2010-9-15 17:30

[tk19]很好

页: [1]

编程论坛