JSP内置对象之登录页面Cookie保存并储存登录密码

#JSP内置对象之登录页面Cookie保存并储存登录密码

登录页面

尽量使用表格fform创建登陆页面,因为配合表格sumbit提交的功能比较简洁方便,
在这里插入图片描述

下面展示一些 内联代码片

如下图对登录用户名和密码设置变量,让用户名和密码储存起来
在这个变量中
下面展示一些 `内联代码片`<form action="cookieSave.jsp" method="post">
		    <h1>信息发布平台</h1>
			<table  border="1" align="center" >
				<tr>
					<td>
						用户名:
					</td>
					<td>
						<input type="text" name="username" value="<%=username%>">
					</td>
				</tr>
				<tr>
					<td>
						密码:
					</td>
					<td>
						<input type="password" name="password" value="<%=password%>">
					</td>
				</tr>
				<tr>
				    <td colspan="2">
						<input type="checkbox" name="flag">&nbsp;记住用户名
					</td>				
				</tr>
				<tr align="center">
					<td colspan="2" >
						<input type="submit" value=" 登    录 " >
					</td>
				</tr>
			</table>
		</form>

为了让登录页面信息有效有必要设置一个判断,下面是定义变量以及判断信息

<%
			String username = "";
			String password = "";
			Cookie[] cookies = request.getCookies();
			if (cookies != null && cookies.length > 0) {
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals("username")) {
						username = cookie.getValue();
					}
					if (cookie.getName().equals("password")) {
						password = cookie.getValue();

					}
					response.addCookie(cookie);
				}

			}
		%>

login登录完整页面代码为

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
	/*String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";*/
%>
    
<!DOCTYPE html>
<html>
<head>


		<title>response方法综合案例</title>
<meta charset="UTF-8">

</head>
<body style="background-image:url(../../images/bk.jpg);">
<%
			String username = "";
			String password = "";
			Cookie[] cookies = request.getCookies();
			if (cookies != null && cookies.length > 0) {
				for (Cookie cookie : cookies) {
					if (cookie.getName().equals("username")) {
						username = cookie.getValue();
					}
					if (cookie.getName().equals("password")) {
						password = cookie.getValue();

					}
					response.addCookie(cookie);
				}

			}
		%>
		<form action="cookieSave.jsp" method="post">
		    <h1>信息发布平台</h1>
			<table  border="1" align="center" >
				<tr>
					<td>
						用户名:
					</td>
					<td>
						<input type="text" name="username" value="<%=username%>">
					</td>
				</tr>
				<tr>
					<td>
						密码:
					</td>
					<td>
						<input type="password" name="password" value="<%=password%>">
					</td>
				</tr>
				<tr>
				    <td colspan="2">
						<input type="checkbox" name="flag">&nbsp;记住用户名
					</td>				
				</tr>
				<tr align="center">
					<td colspan="2" >
						<input type="submit" value=" 登    录 " >
					</td>
				</tr>
			</table>
		</form>
</body>
</html>

cookieSave页面

在这里插入图片描述

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>


<!DOCTYPE>
<html>
  <head>
    
    <title>查看是否保存了用户名</title>
	
  </head>
  
  <body>
  	<%request.setCharacterEncoding("UTF-8"); %>
    <%
    	String username=request.getParameter("username");
    	String password=request.getParameter("password");
    	String[] flag=request.getParameterValues("flag");
    	if(flag!=null&&flag.length>0){//选中了记住用户名
    		//1.新建cookie
    		Cookie cookie1=new Cookie("username",username);
    		Cookie cookie2=new Cookie("password",password);
    		//2.设置实效(1天)
    		cookie1.setMaxAge(1*24*60*60);
    		cookie2.setMaxAge(1*24*60*60);
    		//3.把cookie对象存放在response中
    		response.addCookie(cookie1);
    		response.addCookie(cookie2);
    	}else{//没有选择记住用户名
    		Cookie[] cookies=request.getCookies();
    		if(cookies!=null&&cookies.length>0){
    			for(Cookie cookie:cookies){             //foreach循环方式
    				if(cookie.getName().equals("username")){
    					cookie.setMaxAge(0);
    				}
    				if(cookie.getName().equals("password")){
    					cookie.setMaxAge(0);
    				}
    			response.addCookie(cookie);
    			}
    		}
    	}
    
     %>
     <a href="cookieQuery.jsp">查看是否保存了用户名信息</a>
  </body>
</html>

提取cookie的cookiequery页面

效果图

在这里插入图片描述

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE >
<html>
  <head>
   
    <title>cookie中的用户名密码信息</title>
	
  </head>

  <body bgcolor="#A2B5CD">
  	<%request.setCharacterEncoding("UTF-8"); %>
    <%
    	String username="";
    	String password="";
    	Cookie[] cookies=request.getCookies();
    	if(cookies!=null&&cookies.length>0){
    		for(Cookie cookie:cookies){
    			if(cookie.getName().equals("username")){
    				username=cookie.getValue();
    			}
    			if(cookie.getName().equals("password")){
    				password=cookie.getValue();
    			
    			}
    			response.addCookie(cookie);
    		}
    	
    	}
     %>
     <h2>用户名为:<%=username %></h2>
     <h2>密码为:<%=password %></h2>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值