Login
<%@ 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>
<%
//提取cookie保存的卡号
Cookie[] cc=request.getCookies();
String cardid="";
if(cc!=null)
{
//遍历
for(Cookie c:cc)
{
if(c.getName().equals("cardid"))
{
cardid=c.getValue();
}
}
}
%>
<form action="check.jsp" method="post">
卡号:<input type="text" name ="cardid" value="<%=cardid%>">
<br>
密码:<input type="password" name="password">
<br>
<input type="submit" value="登陆">
</form>
</body>
</html>
check
<%@ 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 cardid =request.getParameter("cardid");
String password =request.getParameter("password");
if(cardid == null || password ==null || cardid.equals("") || password.equals(""))
{
out.print("成功");
response.setHeader("refresh", "3;url=Login.jsp");
}
else
{ //验证
if(cardid.equals("cardid")&&password.equals("password"))
{
//用cookie记住卡号
Cookie cid= new Cookie("cardid",cardid);
cid.setMaxAge(60*60); //设置cookie存在的时间
response.addCookie(cid);
//保持登录状态
session.setAttribute("cardid", cardid);
response.sendRedirect("Main.jsp");
}
else
{
out.print("错误");
response.setHeader("refresh", "3;url=Login.jsp");
}
}
%>
</body>
</html>
main
<%@ 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("cardid");
if(obj==null)
{
out.print("会话超时或未登录");
response.setHeader("regfresh","3;url=Login.jsp");
}
else
{
out.print(obj+"登录成功");
}
%>
<br>
<br>
<a href="Logout.jsp">退出登录</a>
</body>
</html>
Logout
<%@ 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
session.invalidate();
out.print("退出成功");
response.setHeader("refresh", "2;url=Login.jsp");
%>
</body>
</html>