Cookie禁用的问题和Cookie的应用

Cookie禁用


# 前言 如果用户禁用了cookie,我们在用浏览器(每个浏览器就相当于一个客户端)访问服务器时,就会发生session失效,为什么会失效呢?失效了怎么办?还有JSESSIONID是什么?

一、session为什么会失效呢?失效了怎么办?

首先简单说一下session,它就是客户端和服务器端建立的一次会话。
打开浏览器输入地址栏向服务器发送请求的时候,session创建成功(关闭浏览器或者会话到期了,或者人为的销毁session【session.invalidate()】, 会话结束),这时服务器会向每一个客户端保存一个jessionid,后面客户端每次向服务器端发送请求都会将jsessionid传给服务器,服务器获取该值后确定到底是哪个客户端。
补充:jessionid传递给客户端的方法有两种,一是URL重写机制,二是使用cookie传递。
如果cookie禁用了,而且没有重写URL,那么就会出现session失效的问题,由此可见此时解决session失效的办法就是重写URL了。

重写URL

response.encodeURL(request.getContextPath()+"/你要转到的jsp页面");
//request.getContextPath():项目部署到tomcat访问的路径值

JSESSIONID是什么?
JSESSIONID就是用来判断当前客户端对应于哪个session,换句话说服务器识别客户端的方法是通过JSESSIONID来告诉服务器该客户端的session在内存的什么地方。
事实上jsessionid==request.getSession().getId()

二、cookie的应用

我们经常可以再网页中见到记住用户名和密码这样的操作,那这是怎么实现的呢,今天我们来模拟实现一下仅供参考
remember.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
	Cookie[] cookies = request.getCookies();
	boolean f = false;
	if(cookies != null){
		for(Cookie c:cookies){
			if("username".equals(c.getName())){
				f = true;
				session.setAttribute("user", c.getValue());
			}
		}
	}
	if(f){
		response.sendRedirect("welcome2.jsp");
	}
	%>
	<form action="login2" method="post">
		 用户名:<input type="text" name="username"/>
		 密码:<input type="password" name="pwd"/>
		 <label><input type="checkbox" name="rememberme"/>记住我</label>
		 <input type="submit" value="提交"/>
	</form>	
</body>
</html>

Servlet LoginAction2.java

package com.openlab.action;

import java.io.IOException;
import java.io.Serializable;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginAction2
 */
@WebServlet("/login2")
public class LoginAction2 extends HttpServlet implements Serializable{
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String remember = request.getParameter("rememberme");
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");
		request.getSession().setAttribute("user", username);
		if("f".equals(remember)){
			Cookie c1 = new Cookie("user",username);
			Cookie c2 = new Cookie("pwd",pwd);
			response.addCookie(c1);
			response.addCookie(c2);
		}
		response.sendRedirect("welcome2.jsp");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

welcom2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		Cookie[] cookies = request.getCookies();
		boolean f = false;
		if(cookies != null){
			for(Cookie c:cookies){
				if("username".equals(c.getName())){
					f = true;
					session.setAttribute("user", c.getValue());
				}
			}
		}
		if(f || session.getAttribute("user") != null){
			out.print("欢迎您:" + session.getAttribute("user"));
		}else{
			response.sendRedirect("rememberMe.jsp");
		}
	%>			
</body>
</html>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值