模拟淘宝登录,购物车

使用cookie记录登录名,下次登录时能够记得上次的登录名,使用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>

<% 
//提取checkTaobao.jsp中,cookie保存的淘宝名
Cookie[] cookies=request.getCookies();

String taobaoname="";

    if(cookies!=null)
    {
    //遍历
    for(Cookie c: cookies)
    {
        if(c.getName().equals("taobaoname"))
        {
            taobaoname=c.getValue();
            
        }
    }    
    }

%>

<form action="CheckTaobao.jsp" method="post">
用户名:<input type="text" name="taobaoname" value="<%=taobaoname%>"><br>
密码:<input type="password" name="taobaopassword"><br>
<input type="submit" value="登陆淘宝"><br>

</form>
<br>
<br>


</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>

<% 
String taobaoname=request.getParameter("taobaoname");
String taobaopassword=request.getParameter("taobaopassword");

if(taobaoname==null||taobaopassword==null
||taobaoname.equals("")||taobaopassword.equals(""))
{
    out.print("请输入正确的用户名密码");
    response.setHeader("refresh", "3;URL=LoginTaobao.jsp");
}
else
{
    //连数据库
    //验证
    if(taobaoname.equals("1234")&&taobaopassword.equals("123456"))
    {
        //用cookie记住淘宝名
        Cookie cid=new Cookie("taobaoname",taobaoname);
        cid.setMaxAge(60*60);
        response.addCookie(cid);
        
        //保持登录状态
        session.setAttribute("taobaoname",taobaoname);//session是存浏览器的
        
        
        response.sendRedirect("MainTaobao.jsp");
        
    }
    else
    {
        out.write("用户名密码错误");
        response.setHeader("refresh", "3;URL=LoginTaobao.jsp");
    }
}

%>
</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>

淘宝首页
<%
    Object obj=session.getAttribute("taobaoname");
    if(obj==null)
    {
        out.write("会话超时或未登录,请重新输入");
        response.setHeader("refresh","6;URL=LoginTaobao.jsp");
    }
    else
    {
        out.write(obj+",欢迎登录!");

%>
<br>
<br>
<a href="LogoutTaobao.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
<%

session.invalidate();
out.write("退出成功");
response.setHeader("refresh","3;URL=LoginTaobao.jsp");

%>

</body>
</html>

 

posted @ 2016-10-31 14:00  hanruyue  阅读(2607)  评论(0编辑  收藏  举报