模擬淘寶登錄,購物車


使用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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM