标签:highlight src 验证 str write ddc strong 系统 log
<%@ 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(int i=0; i<cks.length; i++)
{
if(cks[i].getName().equals("name"))
name = cks[i].getValue();
}
}
%>
<form action="ShoppCheck.jsp" mothod="post">
用户名:<input type="text"name="name" value="<% if(name != null) out.println(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
String cairnumber = session.getAttribute("name").toString();
out.print("name="+cairnumber);
%>
<a href="success.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>
测试是否登录显示的页面
<br>
<%
Object obj =session.getAttribute("name");//判断session是否存在
if(obj==null)
{
out.print("您没有登入") ;
response.setHeader("refresh", "2;URL=shoppMain.jsp");//若2秒钟无操作自动退回主界面
}
else
{
out.print("name="+obj.toString());
//销毁所有session
//session.invalidate();
//移除某个属性
session.removeAttribute("name");
//response.sendRedirect("tuichu.jsp");
%>
<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>
系统将退出。。。。。
</body>
</html>





如果两秒钟未任何操作,就会被退出,并自动返回主界面,防止利用URL访问网站。
注意:在JSP中,所有的架包要放在WEB_INF下的lib 文件夹下,如图:

使用cookie记录登录名,下次登录时能够记得上次的登录名,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能
标签:highlight src 验证 str write ddc strong 系统 log
原文地址:http://www.cnblogs.com/liuyanzeng/p/6013057.html