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

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

 

String namesp = request.getParameter("namesp");String number = request.getParameter("number");Cookie cookie1 = new Cookie("namesp"+1,namesp);Cookie cookie2 = new Cookie("number"+1,number);response.addCookie(cookie1);response.addCookie(cookie2);

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