JSP第六次作业

1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页。

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>My JSP 'login.jsp' starting page</title>
 6 </head>
 7 <body>
 8     <form action="dologin.jsp" method="post" name="login">
 9         用户名:<input type="text" name="uname" size="20"><br> 
10&emsp;码:<input type="password" name="upwd" size="20"><br> 
11         <input type="button" value="登录" onClick="validate()">
12     </form>
13     <script type="text/javascript">
14         function validate() {
15             if (login.uname.value == "") {
16                 alert("用户名不能为空!");
17                 return;
18             }
19             if (login.upwd.value == "") {
20                 alert("密码不能为空!");
21                 return;
22             }
23             login.submit();
24         }
25     </script>
26 </body>
27 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>My JSP 'dologin.jsp' starting page</title>
 6 </head>
 7 <body>
 8     <%
 9         //设置编码
10         request.setCharacterEncoding("UTF-8");
11         response.setCharacterEncoding("UTF-8");
12         //获取请求数据
13         String uname = request.getParameter("uname");
14         String upwd = request.getParameter("upwd");
15         //判断密码和用户名是否相同
16         if (uname.equals(upwd)) {
17             session.setAttribute("uname",uname);
18             request.getRequestDispatcher("main.jsp")
19                     .forward(request, response);
20         } else {
21             response.sendRedirect("no.jsp");
22         }
23     %>
24 </body>
25 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>My JSP 'main.jsp' starting page</title>
 6 </head>
 7 
 8 <body>
 9     <%
10         String uname = (String) session.getAttribute("uname");
11         //如果他是空,说明没登陆,直接访问该页面了
12         if (uname == null) {
13             response.sendRedirect("login.jsp");
14         }
15     %>
16     欢迎你<%=uname%><br>
17     <a href="loginout.jsp">退出登录</a>
18     <br>
19 
20 </body>
21 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>My JSP 'loginout.jsp' starting page</title>
 6 </head>
 7 
 8 <body>
 9     <%
10         session.invalidate();//结束会话
11         response.setHeader("refresh", "5;url=login.jsp");
12     %>
13 </body>
14 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4   <head>
 5     <title>My JSP 'no.jsp' starting page</title>
 6   </head>
 7   
 8   <body>
 9    <h1>登陆失败!!</h1>
10     <%response.setHeader("refresh", "5;url=login.jsp");%>
11   </body>
12 </html>

 

 

 

 

 

 

2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>My JSP 'main.jsp' starting page</title>
 6 </head>
 7 
 8 <body>
 9     <%
10         String uname = (String) session.getAttribute("uname");
11         //如果他是空,说明没登陆,直接访问该页面了
12         if (uname == null) {
13             response.sendRedirect("login.jsp");
14         }
15     %>
16     欢迎你<%=uname%><br>
17     <a href="loginout.jsp">退出登录</a>
18     <br>
19     <form action="buy.jsp">
20         <hr>
21         是否加入购物车?<br> <input name="item" type="checkbox" value="weilong">
22         辣条 $1<br> <input name="item" type="checkbox" value="xizhilang">
23         果冻 $2<br> <input name="item" type="checkbox" value="menglong">
24         雪糕 $3<br>
25         <hr>
26         <input type="submit" value="加入购物车">
27     </form>
28 
29 
30 </body>
31 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4 <head>
 5 <title>My JSP 'buy.jsp' starting page</title>
 6 </head>
 7 
 8 <body>
 9 
10     <h2>购物车</h2>
11     <%
12         request.setCharacterEncoding("utf-8");
13         String itemName[] = request.getParameterValues("item");
14         if (itemName == null) {
15             out.print("购物车为空");
16         } else {
17             for (int k = 0; k < itemName.length; k++) {
18                 out.print("(" + (k + 1) + ")" + itemName[k] + "<br>");
19             }
20         }
21     %>
22 
23 </body>
24 </html>

 

 

 

 

posted @ 2022-04-23 13:37  MXT16  阅读(19)  评论(0编辑  收藏  举报