模擬淘寶購物,運用cookie,記錄登錄賬號信息,並且記住購物車內所選的商品


1、登錄界面

<%@ 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="";
//獲取所有cookie
Cookie[] ck=request.getCookies();
if(ck!=null)
{
	//遍歷
	for(Cookie co:ck)
	{
		if(co.getName().equals("name"))
		{
			name=co.getValue();
		}
	}
}
%>
請登錄購物賬號

<form action="Test.jsp" method="post">

賬戶:<input type="text" name="name" value="<%=name %>">
密碼:<input type="password" name="password">

<input type="submit" value="點擊登錄">

</form>
</body>
</html>

2,驗證登錄

<%@ 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(""))
{
	
		 out.write("請正確登錄!");
	     response.setHeader("refresh", "2;url=Denglu.jsp");
}
else{
	//驗證
	if(name.equals("1234")&&password.equals("123456"))
	{
		//用cookie記住賬號
		//創建cookie
		Cookie coo=new Cookie("name",name);
		coo.setMaxAge(20*24*3600);
		response.addCookie(coo);
		
		//保持登錄狀態
		//創建session
		session.setAttribute("name", name);
		session.setMaxInactiveInterval(20*60);
		
		response.sendRedirect("Gouwuche.jsp");
	}
	else
	{
		out.write("用戶名或密碼錯誤");
	}
	
}
%>



</body>
</html>

3.購物界面

<%@page import="java.net.URLEncoder"%>
<%@ 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");

if(obj==null)
{
	out.write("回話超時或未登錄");
	response.setHeader("refresh", "2;url=Denglu.jsp");
}
else
{
	out.write(obj+",歡迎登錄");

}
%>

<form action="Daoru.jsp" method="post">
       請輸入您想要購買的商品名稱:<input type="text" name="namesp" /><br/>
                		<input type="submit" value="加入購物車" /><br/>
 </form>
 <br>
 <a href="logout.jsp">退出</a>
</body>
</html>

4.退出界面

<%@ 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=Denglu.jsp");


%>
</body>
</html>

5.創建cookie商品

<%@page import="java.net.URLEncoder"%>
<%@ 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 namesp=request.getParameter("namesp");
namesp=new String(namesp.getBytes("ISO-8859-1"),"UTF-8");
//設置cookie
Cookie coo = new Cookie("namesp", URLEncoder.encode(namesp) );//利用cookie記住購物車內的物品
//設置cookie的生命周期為10分鍾
coo.setMaxAge(600);
//發送cookie
response.addCookie(coo);
//跳轉頁面到菜單選擇
response.sendRedirect("ShangPin.jsp");


 %>



</body>
</html>

6.購物車內商品

<%@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>
<%
Object obj=session.getAttribute("name");
if(obj!=null)
{
	Cookie[] ck=request.getCookies();
	if(ck!=null)
	{
			for(int i =0 ;i<ck.length;i++){
	             if(ck[i].getName().equals("namesp")){
	              	 out.print(ck[i].getName() + "= " + URLDecoder.decode(ck[i].getValue()) + "<br/>");        
	             }
	        }
	}
	else
	{
		out.write("購物車沒有商品")	;
	}
}
else
{
	out.write("未登錄");	
}
%>
<a href="Gouwuche.jsp">返回進行購物</a>
</body>
</html>

  

  

  

  


免責聲明!

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



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