JSP的Cookie在登录中的使用
jsp的cookie在登录中的使用
一 功能需求
实现记忆用户名和密码功能。
二 代码
1、login.jsp
<%@ 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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>my jsp 'index.jsp' starting page</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</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");
}
}
}
%>
<form name="loginform" action="dologin.jsp" method="post">
<table>
<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="isusecookie" checked="checked"/>十天内记住我的登录状态</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"/><input type="reset" value="取消"/></td>
</tr>
</table>
</form>
</body>
</html>
2、dologin.jsp
<%@ 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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>my jsp 'dologin.jsp' starting page</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</head>
<body>
<h1>登录成功</h1>
<hr>
<br>
<br>
<br>
<%
request.setcharacterencoding("utf-8");
//首先判断用户是否选择了记住登录状态
string[] isusecookies = request.getparametervalues("isusecookie");
if(isusecookies!=null&&isusecookies.length>0)
{
//把用户名和密码保存在cookie对象里面
string username = urlencoder.encode(request.getparameter("username"),"utf-8");
//使用urlencoder解决无法在cookie当中保存中文字符串问题
string password = urlencoder.encode(request.getparameter("password"),"utf-8");
cookie usernamecookie = new cookie("username",username);
cookie passwordcookie = new cookie("password",password);
usernamecookie.setmaxage(864000);
passwordcookie.setmaxage(864000);//设置最大生存期限为10天
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); //设置cookie失效
response.addcookie(c); //重新保存。
}
}
}
}
%>
<a href="users.jsp" rel="external nofollow" target="_blank">查看用户信息</a>
</body>
</html>
3、users.jsp
<%@ 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%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>my jsp 'users.jsp' starting page</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" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
-->
</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");
}
}
}
%>
<br>
<br>
<br>
用户名:<%=username %><br>
密码:<%=password %><br>
</body>
</html>
三 测试

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
- jsp+servlet实现文件上传与下载功能
- EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常
- 在JSP中使用formatNumber控制要显示的小数位数方法
- 秒杀系统Web层设计的实现方法
- 将properties文件的配置设置为整个Web应用的全局变量实现方法
- JSP使用过滤器防止Xss漏洞
- 在JSP页面中动态生成图片验证码的方法实例
- 详解JSP 内置对象request常见用法
- 使用IDEA编写jsp时EL表达式不起作用的问题及解决方法
- jsp实现局部刷新页面、异步加载页面的方法
- Jsp中request的3个基础实践
- JavaServlet的文件上传和下载实现方法
- JSP页面的静态包含和动态包含使用方法


