jsp实现用户自动登录功能
理解并掌握cookie的作用以及利用cookie实现用户的自动登录功能,实现下图效果

当服务器判断出该用户是首次登录的时候,会自动跳转到登录界面等待用户登录,并填入相关信息。通过设置cookie的有效期限来保存用户的信息,关闭浏览器后,验证是否能够自动登录,若能登录,则打印欢迎信息;否则跳转到登录页面。
login.jsp
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<%request.setcharacterencoding("gb2312"); %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" >
<title>my jsp 'login.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" >
-->
<script type="text/javascript">
window.onload = function(){
//获取submit
var submit = document.getelementbyid("submit");
var name = document.getelementbyid("name");
//为submit绑定单击响应函数
submit.onclick = function(){
times = document.getelementsbyname("time");
var count=0;
for(var i=0;i<times.length;i++){
if(times[i].checked == true){
count++;
}
}
if(count>=2){
alert("只能选择一个选项");
return false;
}
};
};
</script>
</head>
<body>
<!-- 设置html页面 -->
<form action="sucess.jsp" method="post">
用户名:<input name="username" /><br/>
<input type="checkbox" name="time" value="notsave" />不保存
<input type="checkbox" name="time" value="aday" />一天
<input type="checkbox" name="time" value="aweek" />一周
<input type="checkbox" name="time" value="forever" />永久
<br/><br/>
<input type="submit" name="submit" id="submit" value="登录"/>
</form>
<%
//读取session值
string val= (string)session.getattribute("name");
//如果session不存在
if(val==null){
val ="不存在";
}
out.print("当前\""+val+"\"用户可自动登录");
%>
</body>
</html>
sucess.jsp
%@ page language="java" import="java.util.*" pageencoding="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" >
<title>my jsp 'show.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" >
-->
</head>
<body>
<%
//获取username
string name = request.getparameter("username");
//判断用户名是否存在
if(name != null && !name.trim().equals("")){
string[] time = request.getparametervalues("time");
//设置session值,便于login页面读取
session.setattribute("name", name);
//设置cookie
cookie cookie = new cookie("name",name);
//根据提交选项设置cookie保存时间
if(time != null){
for(int i=0;i<time.length;i++){
//不保存cookie
if(time[i].equals("notsave")){
cookie.setmaxage(0);
}
//保存一天cookie
if(time[i].equals("aday")){
cookie.setmaxage(60*60*24);
}
//保存一周cookie
if(time[i].equals("aweek")){
cookie.setmaxage(60*60*24*7);
}
//永久保存cookie,设置为100年
if(time[i].equals("forever")){
cookie.setmaxage(60*60*24*365*100);
}
}
}
//在客户端保存cookie
response.addcookie(cookie);
}
else{%>
<%--用户名不存在则进行判断是否已有cookie --%>
<%
//获取cookie
cookie[] cookies = request.getcookies();
//cookie存在
if(cookies != null && cookies.length > 0){
for(cookie cookie:cookies){
//获取cookie的名字
string cookiename = cookie.getname();
//判断是否与name相等
if(cookiename.equals("name")){
//获取cookie的值
string value = cookie.getvalue();
name = value;
}
}
}
}
if(name != null && !name.trim().equals("")){
out.print("您好: " + name+"欢迎登录");
}
else{//否则重定向到登录界面
out.print("您还没有注册,2秒后转到注册界面!");
response.setheader("refresh","2;url=login.jsp");
%>
如果没有自动跳转,请点击<a href="login.jsp" rel="external nofollow" >此处</a>进行跳转
<%
//response.sendredirect("login.jsp");
}
%>
</body>
</html>
实现效果:
1.

2.

3.

4.

5.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
相关文章
- 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页面的静态包含和动态包含使用方法


