10-30----作业,实现购物车,退出登陆,登陆记住用户名密码,检查用户是否登陆4个功能

 

 



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 name = ""; 12 String password = ""; 13 Cookie[] cookies = request.getCookies(); 14 if(cookies!=null&&cookies.length>0){ 15 for(Cookie cookie:cookies){ 16 if("name".equals(cookie.getName())){ 17 name = cookie.getValue(); 18 }else if("password".equals(cookie.getName())){ 19 password = cookie.getValue(); 20 } 21 } 22 } 23 %> 24 <form action="setcookiesession.jsp" method="post"> 25 账号:<input type="text" name="name" value="<%= name %>"/> 26 密码:<input type="password" name="password" value="<%= password %>"/> 27 <input type="submit" value="提交"/> 28 </form> 29 </body> 30 </html>
 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 name = request.getParameter("name");
12     String password = request.getParameter("password");
13     Cookie cookie1 = new Cookie("name",name);
14     Cookie cookie2 = new Cookie("password",password);
15     cookie1.setMaxAge(60*60*60);
16     cookie2.setMaxAge(60*60*60);
17     response.addCookie(cookie1);
18     response.addCookie(cookie2);
19      
20        session.setAttribute("name", name);
21     session.setMaxInactiveInterval(60*60); 
22     
23     response.sendRedirect("gouwu.jsp");
24 %>
25 </body>
26 </html>
 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     Object sessionname = session.getAttribute("name");
12     if(sessionname==null)
13         response.sendRedirect("login.jsp");      
14 %>
15     <form action="gouwuresult.jsp" method="post">
16         请输入您想要购买的商品名称:<input type="text" name="namesp" /><br/>
17         请输入您想要购买的商品数量:<input type="text" name="number" /><br/>
18         <input type="submit" value="提交" /><br/>
19     </form>
20     <br/>
21     <a href="gouwuchexianshi.jsp">查看购物车中的商品</a><br/>
22     <a href="logout.jsp">退出登陆</a>
23 </body>
24 </html>
 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 Object sessionname = session.getAttribute("name");
12 if(sessionname==null)   
13     response.sendRedirect("login.jsp");    
14 String namesp = request.getParameter("namesp");
15 String number = request.getParameter("number");
16 Cookie cookie1 = new Cookie("namesp"+Math.random(),namesp);
17 Cookie cookie2 = new Cookie("number"+Math.random(),number);
18 cookie1.setMaxAge(60*60*60);
19 cookie2.setMaxAge(60*60*60);
20 response.addCookie(cookie1);
21 response.addCookie(cookie2);
22 
23 out.print("该商品已经添加到购物车");
24 %>
25 <br/>
26 <a href="gouwu.jsp">回到购物页面</a>
27 <a href="gouwuchexianshi.jsp">查看购物车中商品</a>
28 </body>
29 </html>
 
 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     session.invalidate();
12     out.print("您已经退出登陆");
13 %>
14 <br/>
15 <a href="gouwu.jsp">试试回到购物页面以检验是否成功消除session</a>
16 </body>
17 </html>

 

 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     Object sessionname = session.getAttribute("name");
12     if(sessionname==null)
13         response.sendRedirect("login.jsp");    
14     
15     out.print("购物车中商品如下:<br/>");
16     Cookie[] cookies = request.getCookies();
17     if(cookies!=null&&cookies.length>0){
18         
19         for(int i =0,ii=0 ;i<cookies.length;i++,ii++){
20             
21             if(!cookies[i].getName().equals("JSESSIONID")&&!cookies[i].getName().equals("name")&&!cookies[i].getName().equals("password")){
22                 if(ii%2!=0)
23                         out.print("商品名称: " + cookies[i].getValue()); 
24                 else
25                     out.print("  商品数量:" + cookies[i].getValue() + "<br/>");
26             }
27             
28         }
29     }else{
30         out.print("您的购物车没有商品<br/>");
31     }
32 %>
33 <a href="gouwu.jsp">返回购物页面</a>
34 </body>
35 </html>

 

posted @ 2016-10-30 22:18  火山林风  阅读(360)  评论(0编辑  收藏  举报