jsp如何实现短期内免除重复输账号密码
Admin 2023-04-14 群英技术资讯 925 次浏览
这篇文章给大家介绍了“jsp如何实现短期内免除重复输账号密码”的相关知识,讲解详细,步骤过程清晰,有一定的借鉴学习价值,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。本文实例为大家分享了jsp实现简单用户7天内免登录的具体代码,供大家参考,具体内容如下
(1)登陆页面:login.jsp
<%@ 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>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="username" value="${cookie.cook_name.value }"/><br/>
<input type="checkbox" name="chkCookie" value="7"/>记住我,7天内免登录<br/>
<input type="submit" value="登录"/><br/>
<a href="sessionUser.jsp" rel="external nofollow" >验证session</a>
<a href="cookieUser.jsp" rel="external nofollow" >验证cookie</a>
</form>
</body>
</html>
(2)跳转页面:cookieUser.jsp
<%@ 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>
<%
//从浏览器获取cookie
Cookie[] cookies = request.getCookies();
String uname = null;
//判断语句条件小技巧
if(null != cookies){
for(Cookie cookie:cookies){
//遍历本地浏览器中的所有cookie
if(cookie.getName().equals("cook_name")){
//把cookie中的用户名取出
uname = cookie.getValue();
}
//out.print("<p>name: " + cookie.getName()+"</p>");
//out.print("<p>value: " + cookie.getValue()+"</p>");
}
}
//判断cookie有没有失效
if(null != uname){
out.print("<p>用户名:"+uname +"</p>");
}
else{
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
(3)Servlet文件:LoginServlet类
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//获取用户登录名
String username = request.getParameter("username");
//获取7天内是否免登录
String day7 = request.getParameter("chkCookie");
int cookie_day = 1;
if(null != day7 && day7.equals("")){
cookie_day = Integer.valueOf(day7);
}
//创建cookie
Cookie cookie = new Cookie("cook_name",username);
//设置Cookie有限期
//cookie.setMaxAge(6);//有效期6秒
cookie.setMaxAge(3600 * 24 * cookie_day);//设置7天免登录
//写入cookie到浏览器
response.addCookie(cookie);
response.sendRedirect("cookieUser.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要为大家详细介绍了Jsp+Servlet实现文件上传下载功能的第二部分,文件列表展示,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了JSP实现文件上传功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要为大家详细介绍了JSP实现弹出登陆框以及阴影效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这篇文章主要介绍了使用JSP技术实现一个简单的在线测试系统的实例详解,文章通过示例代码和代码运行结果图相结合展示介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍是动态实现web网页登陆和注册功能的示例代码,文中代码讲解详细,对我们学习JSP有一定的帮助,感兴趣的小伙伴可以跟随小编一起学习一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008