在某网站的登录页面登录时如果选择“记住用户名”,登录成功后会跳转到一个中间层(页面代码将登录的用户名和密码存在cookie),中间页面中存在一个超链接,单击超链接可以链接到第三个页面查看信息。若选择“

2023-05-22,,

Response实现登录并记录用户名和密码信息

在某网站的登录页面登录时如果选择“记住用户名”,登录成功后会跳转到一个中间层(页面代码将登录的用户名和密码存在cookie),中间页面中存在一个超链接,单击超链接可以链接到第三个页面查看信息。若选择“不记住用户名”,则中间页面会删除,单击超链接则无法显示登录的用户名和密码。

登录界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<%
String username = "";
String pwd = "";
Cookie[] cookies = request.getCookies();//cookie用来保存客户端用户信息,登录时提供一个“是否记住用户名”的选项
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username = cookie.getValue();
}
if (cookie.getName().equals("pwd")) {
pwd = cookie.getValue();
}
response.addCookie(cookie);
} }
%> <form action="cookieSave.jsp" method="post">
<h1 align="center">注册界面</h1>
<table align="center" border="1">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="<%=username%>"></td>
</tr> <tr>
<td>密码:</td>
<td><input type="password" name="pwd" value="<%=pwd%>"></td>
</tr> <tr>
<td colspan="2"><input type="checkbox" name="flag">&nbsp;记住用户名
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"> <input
type="reset" value="取消"></td>
</tr>
</table> </form>
</body>
</html>

中间层保存:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>中间层</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
String[] flag = request.getParameterValues("flag"); if (flag != null && flag.length > 0) {//选中了记住用户名
//新建cookie
Cookie cookie1 = new Cookie("username", username);
Cookie cookie2 = new Cookie("pwd", pwd);
//设置实效
cookie1.setMaxAge(1 * 24 * 60 * 60);
cookie2.setMaxAge(1 * 24 * 60 * 60);
//把cookie对象存放在response中
response.addCookie(cookie1);
response.addCookie(cookie2); } else {//没有选择记住用户
Cookie[] cookies = request.getCookies();
if (cookies != null && cookies.length > 0) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
cookie.setMaxAge(0);
}
if (cookie.getName().equals("pwd")) {
cookie.setMaxAge(0);
}
response.addCookie(cookie);
}
} }
%>
<a href="look.jsp">查看保存信息</a>
</body>
</html>

信息查看层

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
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("pwd")) {
pwd = cookie.getValue();
}
response.addCookie(cookie);
}
}
%>
<h3>
用户名:<%=username%></h3>
<br>
<h3>
密码:<%=pwd%></h3>
</body>
</html>

运行结果:

在某网站的登录页面登录时如果选择“记住用户名”,登录成功后会跳转到一个中间层(页面代码将登录的用户名和密码存在cookie),中间页面中存在一个超链接,单击超链接可以链接到第三个页面查看信息。若选择“的相关教程结束。

《在某网站的登录页面登录时如果选择“记住用户名”,登录成功后会跳转到一个中间层(页面代码将登录的用户名和密码存在cookie),中间页面中存在一个超链接,单击超链接可以链接到第三个页面查看信息。若选择“.doc》

下载本文的Word格式文档,以方便收藏与打印。