java httpsession读写_JavaWeb学习笔记-使用HttpSession对象跟踪会话

使用HttpSession接口开发的步骤:

1.获取HttpSession对象

2.对HttpSession对象进行读写

3.手工终止HttpSession,或者自动终止

常用方法:

getId():返回包含分配给这个会话的唯一表示的字符串。在使用URL改写以及标识会话时比较有用

setAttribute():使用指定的名称将对象绑定到这个会话。(setValue()方法)

getAttribution():返回绑定到此会话的对象。(getValue()方法)

invalide():终止当前会话

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

//获取会话对象

HttpSession session = request.getSession(true);

response.setContentType("text/html;charset=GB2312");

response.setCharacterEncoding("gb2312");//确保参数信息以汉字编码方式提取

PrintWriter out = response.getWriter();

//从会话中获取属性

int count =1;

Integer i = (Integer)session.getAttribute(COUNTER_KEY);

//如果存在以前的数值

if(i!=null){

count=i.intValue()+1;

}

//将属性信息存入会话

session.setAttribute(COUNTER_KEY, new Integer(count));

Date lastAccessed = new Date(session.getLastAccessedTime());

Date sessionCreated = new Date(session.getCreationTime());

DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);

//输出对话信息

out.println("");

out.println("

");

out.println("

会话计数器");

out.println("");

out.println("

");

out.println("你的会话ID:"+session.getId()+"
");

out.println("会话创建时间:"+formatter.format(sessionCreated)+"
");

out.println("会话上次访问时间:"+formatter.format(lastAccessed)+"
");

out.println("会话期间你向页面发起"+count+"次请求");

out.println("

");

out.println("");

out.println("

");

out.println("");

out.println("");

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request,response);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaWeb应用中,为了实现用户登录认证,通常会使用Session和Cookie。 Session是在服务端保存用户状态的一种机制,每个用户在访问服务器时都会被分配一个唯一的Session ID,通过该ID可以在服务端存储和获取与该用户相关的信息。在用户登录后,可以将用户信息存储到Session中,供之后的页面访问和使用。 Cookie是在客户端保存用户状态的一种机制,通过在服务端设置Cookie,在客户端保存一个唯一的标识符,带着该标识符可以在客户端和服务端之间传递数据。在用户登录后,可以将用户信息存储到Cookie中,供之后的页面访问和使用使用Session和Cookie实现登录认证的基本流程如下: 1. 用户在登录页面输入用户名和密码。 2. 服务器接收到请求后,验证用户名和密码是否正确。 3. 如果验证通过,生成一个唯一的Session ID,并将用户信息存储到Session中。 4. 将Session ID 存储到Cookie中,并设置Cookie的有效期。 5. 用户访问其他页面时,将Cookie中的Session ID 发送到服务器,服务器根据Session ID 获取用户信息,判断用户是否登录。 6. 如果用户已经登录,返回需要访问的页面内容;如果用户未登录,跳转到登录页面。 示例代码如下: ```java // 生成Session ID String sessionId = UUID.randomUUID().toString(); // 将用户信息存储到Session中 HttpSession session = request.getSession(); session.setAttribute("username", username); // 将Session ID 存储到Cookie中,并设置有效期为1天 Cookie cookie = new Cookie("sessionId", sessionId); cookie.setMaxAge(24 * 60 * 60); response.addCookie(cookie); // 获取Cookie中的Session ID,并根据Session ID 获取用户信息 Cookie[] cookies = request.getCookies(); String sessionId = null; if (cookies != null) { for (Cookie c : cookies) { if ("sessionId".equals(c.getName())) { sessionId = c.getValue(); break; } } } if (sessionId != null) { HttpSession session = request.getSession(false); if (session != null) { String username = (String) session.getAttribute("username"); if (username != null) { // 用户已经登录,返回需要访问的页面内容 } } } // 用户未登录,跳转到登录页面 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值