Login
网站首页 > 文章中心 > 其它

session setattribute_怎么向session中追加数据

作者:小编 更新时间:2023-08-02 14:09:06 浏览量:194人看过

HttpSession session = ServletActionContext.getRequest().getSession();

session.setAttribute(CURRENT_USER_KEY, theUser);

这是写在sevlet(服务器端)的有关session的代码答逗,用来兄穗统计网站被访问的次羡举卜数,希望对你有用

public class UserTest extends HttpServlet

{

public UserTest()

}

public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException

doPost(request,response);

public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException

int count=0;

HttpSession s = request.getSession();

if(s.getAttribute("cc") !=null)

count = Integer.parseInt(s.getAttribute("cc").toString());

count++;

s.setAttribute("cc",count);

response.setCharacterEncoding("gbk");

PrintWriter out = response.getWriter();

out.println("");

out.println("用户是第"+count+"次访问该程序");

存入到session范围内的数据会在一次回话结束时消失,把握好这点就好说了.

我给你模拟一下,

ArrayList al=new ArrayList();

Student stu=new Student();//比如我们模拟一张学生表,里世吵面有三列:学生ID、姓名、住址.

stu.setStuId(1);

stu.setStuName("张某");

stu.setStuAddress("地球中国");

al.add(stu);//将stu对象写入集合中.

到这,假如我想要将学生表中的数据展示在jsp界面上,则需要用到setAttribute将al集合传过去,用什么方法呢?请看代码:

request.getSession().setAttribute("al", al);这一步,就是将上面的al集合放到session范围内,只要当前会话不结束,al中的数据就会一直在session中.

什么是session本站上有很多,直接搜就可以,此时此刻呢说getAttribute的用法,扮蠢此时此刻呢是jsp页面的内容.

<%

List al=(List)request.getAttribute("al");

说白了,setAttribute 与getAttribute是相互依存的关系,setAttribute设置属性,getAttribute获得setAttribute中的内容.

就是这样,哪儿不懂请追问.

session.setAttribute("ID",rb.getReaderID());

session.setAttribute("name",rb.getReaderName());

是可以传多个值的啊,你说的是一个Attribute里传多个值吗?如果是这样:

你可以封装成list 或者建立一个实体,再或者传一个裂渣map过去也好啊.

使用Map:

Map map = new HashMap();

map.put("id", rb.getReaderID());

map.put("name", rb.getReaderName());

session.setAttribute("map",map);

另一端获取就直接session.map.name ,以悉森及session.map.id 就可以获取了

"

以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章