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>
相關文章
相關標籤/搜索