使用cookie记录登录名,下次登录时能够记得上次的登录名,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能
Posted 琢磨先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用cookie记录登录名,下次登录时能够记得上次的登录名,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能相关的知识,希望对你有一定的参考价值。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name=null; Cookie[] cks = request.getCookies() ;//获得cookie的集合 if(cks != null) {
// 如果已经设置了cookie , 则得到它的值,将该值放在登入名文本框的value中
//遍历
for(Cookie c:cks)
{
if(c.getName().equals("name"))
name = c.getValue();
}
} %> <form action="ShoppCheck.jsp" method="post"> 用户名:<input type="text"name="name" value="<%=name%>"><br> 密码:    <input type= "password"name="password"><br> <input type="submit" value="登入"> </form> </body> </html>
<%@page import="com.hanqi.web.ShoppDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//设置不缓存页面
//response.setHeader("Cache_Control", "no-cache");
//接受数据
String name =request.getParameter("name");
String password =request.getParameter("password");
if(name==null || password == null||name.equals("")||password.equals(""))
{
//排除用户名和密码为null或 为空字符串的情况
out.write("请正确登录系统");
}
else
{
//检查登录信息
ShoppDAO sd= new ShoppDAO ();//导包
if(sd.checkLogin(name, password))
{
//out.write("登录成功");
//无缓存的直接发送
//response.getWriter().write("登入成功");
//创建cookie
Cookie ck= new Cookie("name", name);
//设置过期时间
ck.setMaxAge(10*24*60*60);
//发送
response.addCookie(ck);
//创建session
session.setAttribute("name", name);
//页面跳转
response.sendRedirect("xtMain.jsp");
}
else
{
out.write("登入失败");
//2秒后跳回登入页面
response.setHeader("refresh", "2;URL=shoppMain.jsp");
}
}
%>
</body>
</html>
<%@page import="java.net.URLDecoder"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 系统主页面 <br> <% //检查cookie //获得cookie集合 Cookie[] cks= request.getCookies(); for(Cookie ck: cks) { //解码 out.write(ck.getName()+"="+URLDecoder.decode(ck.getValue()) +"<br>"); } //判断session
Object yonghuming = session.getAttribute("name");
if(yonghuming==null)
{
out.print("回话超时或为登入");
response.setHeader("refresh", "2;URL=shoppMain.jsp");
}
else
{
//out.print("name="+yonghuming);
out.print(yonghuming+",欢迎登入");
%>
<br>
<a href="tuichu.jsp">退出登入</a>
<%
}
%>
</body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 系统将退出。。。。。
<%
//销毁
session.invalidate();
out.print("退出成功");
response.setHeader("refresh", "6;URL=shoppMain.jsp");
%>
</body> </html>
如果两秒钟未任何操作,就会被退出,并自动返回主界面,防止利用URL访问网站。
注意:在JSP中,所有的架包要放在WEB_INF下的lib 文件夹下,如图:
以上是关于使用cookie记录登录名,下次登录时能够记得上次的登录名,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能的主要内容,如果未能解决你的问题,请参考以下文章
模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登