这篇文章总字数为:5867 字,有 5 张图存于本站服务器
问题已经解决!借鉴请谨慎雷同,最近查的严,尽量改出自己的特色!!!
前言:
这是一次作业,教学视频里有提供代码,没什么好说的,做个记录,方便将来查看。
效果图:
完整代码:
login.jsp文件(位置:webroot根目录下)
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>17180074平帅</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username ="";
String password ="";
Cookie[] cookies = request.getCookies();
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");
}
}
}
%>
<form name="loginForm" action="error.jsp" method="POST">
<table align="center">
<caption>
<h1>用户登录</h1>
</caption>
<tr>
<th></th>
<th></th>
</tr>
<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" checked="checked" name="isUseCookie">记住账号</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
<%=(String)request.getAttribute("usertxt") %>
</body>
</html>
error.jsp文件(位置:webroot根目录下)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>17180074平帅</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals(password)){
if(username.equals("")){
request.setAttribute("usertxt", "输入错误!请重新输入!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else{
request.getRequestDispatcher("dologin.jsp").forward(request, response);
}
}
else{
request.setAttribute("usertxt", "输入错误!请重新输入!");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
%>
</body>
</html>
dologin.jsp文件(位置:webroot根目录下)
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>17180074平帅</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>登录成功!</h1>
<%
request.setCharacterEncoding("utf-8");
String[] isUseCookies = request.getParameterValues("isUseCookie");
if(isUseCookies != null && isUseCookies.length>0) {
String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
String password = URLEncoder.encode(request.getParameter("password"), "utf-8");
Cookie usernameCookie = new Cookie("username",username);
Cookie passwordCookie = new Cookie("password",password);
usernameCookie.setMaxAge(10*24*3600);
passwordCookie.setMaxAge(10*24*3600);
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
}
else
{
Cookie[] cookies = request.getCookies();
if(cookies != null && cookies.length>0)
{
for(Cookie c:cookies)
{
if(c.getName().equals("username")||c.getName().equals("password"))
{
c.setMaxAge(0);
response.addCookie(c);
}
}
}
}
%>
<a href="users.jsp" target="_blank">查看用户信息</a>
</body>
</html>
users.jsp文件(位置:webroot根目录下)
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html;charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>17180074平帅</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>用户信息</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
String username ="";
String password ="";
Cookie[] cookies = request.getCookies();
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>
</body>
</html>
附件:jsp实现cookie的应用登录功能李老师作业8-1
文件大小:18.8k
更新时间:2020-5-19
本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有,若为付费资源,请在下载后24小时之内自觉删除,若作商业用途,请到原网站购买,由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
评论