JSP中使用Cookies实现登陆信息的保存

首先,此案例需要准备三个JSP页面:

    1. login.jsp:负责登陆页面的显示(表单提交给dologin.jsp页面处理)

    2. dologin.jsp:负责判断用户是否登陆成功、用户是否勾选记住账号密码(勾选则使用Cookies保存用户信息)、查看用户信息

    3. user.jsp :显示用户信息页面(只有勾选了十天内保存用户信息才会显示用户信息)

不添加任何业务逻辑的三个页面的代码如下:

login.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>用户登陆界面</title>
</head>
<body>
	<h1>用户登陆</h1>
	<hr>
	<form name="loginForm" action="dologin.jsp" method="post">
		<table>
			<tr>
				<td>UserName:</td>
				<td><input type="text"  name="username" value=""/></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="password" name="password" value=""/></td>
			</tr>
			<tr>
				<td colspan="2"><input type="checkbox" name="isUseCookies" checked="checked"/>十天内记住我的登陆状态</td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" value="登陆"/><input type="reset" value="取消" /></td>
			</tr>
		</table>
	</form>
</body>
</html>

运行效果:



dologin.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>登陆成功</title>
</head>
<body>
	<h1>登陆成功</h1>
	<hr>
	<a href="user.jsp">查看用户信息</a>
</body>
</html>

运行效果



user.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>登陆成功</title>
</head>
<body>
	<h1>用户资料</h1>
    <hr>
          用户名:<br>
          密码:<br>
</body>
</html>

运行效果



接下来添加业务逻辑代码。

login.jsp中要判断是否保存了该用户的登陆信息,保存了的信息直接赋值,不需要用户再次手动输入;dologin.jsp中判断是否需要保存用户信息,没保存的判断是否需要保存(使用Cookies保存);user.jsp中获取dologin.jsp处保存的Cookies信息。

1.dologin.jsp中的业务逻辑代码(判断是否需要保存用户信息并进行相应处理)

	<%
		request.setCharacterEncoding("utf-8"); //防止中文乱码
		//首先判断用户是否选择保存登陆状态
		String[] isUseCookies = request.getParameterValues("isUseCookies"); 
		//选择保存:不为空且大于0
		if(isUseCookies != null && isUseCookies.length>0){
			String username = URLEncoder.encode(request.getParameter("username"),"utf-8");  
			//使用URLEncoder解决 无法在Cookies对象中无法保存中文字符的问题
			String password = URLEncoder.encode(request.getParameter("password"), "utf-8");
			//使用Cookies对象保存字符串
			Cookie usernemeCookie = new Cookie("username",username);
			Cookie passwordCookie = new Cookie("password",password);
<%
    	String username = ""; //用户名
    	String password = ""; //密码
    	
    	Cookie[] cookies = request.getCookies();
    	//保存有cookie对象
    	if(cookies != null && cookies.length > 0){
    		for(Cookie c: cookies){
    			if(c.getName().equals("username")){
    				username = c.getValue();
    			}
    			if(c.getName().equals("passwird")){
    				password = c.getValue();
    			}
    		}
    	}
    %>
          用户名:<%=username %><br>
          密码:<%=password %><br>


//设置Cookie对象的最长生存期限(单位为秒):十天usernemeCookie.setMaxAge(864000);passwordCookie.setMaxAge(864000);//使用response对象 保存Cookie对象response.addCookie(usernemeCookie);response.addCookie(passwordCookie);}else{ //选择不保存Cookie[] cookies = request.getCookies();//如果已经保存了cookie对象 则需要将它的生存期限设为0if(cookies != null && cookies.length >0){for(Cookie c : cookies){if(c.getName().equals("username") || c.getName().equals("password")){c.setMaxAge(0); //设置cookies失效response.addCookie(c); //重新保存 不然不会起作用}}}}%>

2. user.jsp中的业务逻辑代码(只需获取cookies值,即接收用户信息)

 <%
    	request.setCharacterEncoding("utf-8");	
    
    	String username = ""; //用户名
    	String password = ""; //密码
    	
    	Cookie[] cookies = request.getCookies();
    	//保存有cookie对象
    	if(cookies != null && cookies.length > 0){
    		for(Cookie c: cookies){
    			if(c.getName().equals("username")){
    				username = URLDecoder.decode(c.getValue(),"utf-8");
    			}
    			if(c.getName().equals("password")){
    				password = URLDecoder.decode(c.getValue(),"utf-8");
    			}
    		}
    	}
    %>
          用户名:<%=username %><br>
          密码:<%=password %><br>

3. login.jsp业务逻辑(判断是否保存过用户信息)

此处仍需要接收用户信息,与user.jsp中的代码相似

<%
	request.setCharacterEncoding("utf-8");
	
    	String username = ""; //用户名
    	String password = ""; //密码
    	
    	Cookie[] cookies = request.getCookies();
    	//保存有cookie对象
    	if(cookies != null && cookies.length > 0){
    		for(Cookie c: cookies){
    			if(c.getName().equals("username")){
    				username = URLDecoder.decode(c.getValue(),"utf-8");
    			}
    			if(c.getName().equals("password")){
    				password = URLDecoder.decode(c.getValue(),"utf-8");
    			}
    		}
    	}
    %>

<tr>
				<td>UserName:</td>
				<td><input type="text"  name="username" value="<%=username %>"/></td>
			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="password" name="password"/ value="<%=password %>"></td>
			</tr>

下面是运行过程中出现的问题:

1.login.jsp与user.jsp中password单词乒协错误,造成输入的密码无法保存

2.只注意dologin.jsp中使用URLEncoder.encode()方法进行编码,却忘记在user.jsp\login.jsp中使用URLDecoder.decode()方法进行解码,而造成中文乱码问题

3. dologin.jsp中设置了request.setCharacterEncoding("utf-8"); 却在user.jsp\login.jsp中忘记设置


猜你喜欢

转载自blog.csdn.net/qq_36834445/article/details/79922821
今日推荐