创建Cookie,简单模拟登录,记录登录名,购物车记录先前添加内容,session控制登录

 工作任务:模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站,并实现退出登录功能 
 
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <%
11 String username = null;
12 Cookie[] ck = request.getCookies();        //获取cookie集合
13 for(Cookie temp:ck){
14     if(temp.getName().equals("username")){
15         username = temp.getValue();
16     }
17 }
18 %>
19 <form action="Check001.jsp" method="post">
20 卡号:<input type="text" name="username" value="<% if(username != null)out.print(username);%>"><br>
21 密码:<input type="password" name="password"><br>
22 <input type="submit" value="登录">
23 </form>
24 </body>
25 </html>
 1 <%@page import="com.hq.test.CarDao"%>
 2 <%@ page language="java" contentType="text/html; charset=UTF-8"
 3     pageEncoding="UTF-8"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11 <%
12 String username = request.getParameter("username");
13 String password = request.getParameter("password");
14 if(username == null || password == null || username.equals("") || password.equals("")){
15     out.write("请正确登录!");
16     response.setHeader("refresh", "2;url=Login001.jsp");
17 }else{
18     //检查登录信息
19     CarDao cd = new CarDao();
20     if(cd.checkLogin(username, password)){
21         out.write("登录成功!");
22         session.setAttribute("username",username);
23         session.setMaxInactiveInterval(10);
24         Cookie ck = new Cookie("username",username);
25         ck.setMaxAge(10*24*60*60);
26         response.addCookie(ck);
27     }else{
28         out.write("登录失败!");
29         response.setHeader("refresh", "2;url=Login001.jsp");
30     }
31 }
32 %>
33 <form action="ShopCar.jsp" method="post">
34 购物车:<input type="text" name="shop" ><br>
35 <input type="submit" value="加入购物车">
36 </form>
37 </body>
38 </html>
 1 <%@page import="java.net.URLDecoder"%>
 2 <%@page import="java.net.URLEncoder"%>
 3 <%@ page language="java" contentType="text/html; charset=UTF-8"
 4     pageEncoding="UTF-8"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12 <%
13 String shop =request.getParameter("shop");
14 String shopcar= null;
15 Cookie[] cks = request.getCookies();        //获取cookie集合
16 for(Cookie temp:cks){
17     if(temp.getName().equals("shop")){
18         shopcar=URLDecoder.decode(temp.getValue())+" "+shop;
19     }
20 }
21 Cookie ck = new Cookie("shop",URLEncoder.encode(shopcar));
22 ck.setMaxAge(10*24*60*60);
23 response.addCookie(ck);
24 %>
25 <%=shopcar %>
26 </body>
27 </html>

 

posted @ 2016-10-30 21:04  荆小八  阅读(346)  评论(0编辑  收藏  举报