#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"> 记住用户名
</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"> 记住用户名
</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>