JSP第八周作业

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

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML>
 3 <html>
 4 <head>
 5 <meta charset="utf-8">
 6 <title></title>
 7 </head>
 8 <body>
 9     <%
10         if (session.getAttribute("uname") == null) {
11             response.sendRedirect("login.jsp");
12         } else {
13             String uname = (String) session.getAttribute("uname");
14             out.print("欢迎" + uname + "</br>");
15     %>
16     <a href="logout.jsp">退出登录</a>
17     <%
18         }
19     %>
20 </body>
21 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML>
 3 <html>
 4 <head>
 5 <meta charset="utf-8">
 6 <title></title>
 7 </head>
 8 <body>
 9     <form name="formName" action="dologin.jsp" method="post">
10     
11         <input name="uname"><br>
12         <input type="password" name="password"><br>
13         <input type="button" value="登录" onclick="login()"> 
14     </form>
15     <script type="text/javascript">
16         function login(){
17             if(formName.uname.value==""||formName.password.value==""){
18                 alert("账号或密码不能为空!");
19                 return;
20             }
21             formName.submit();
22         }
23     </script>
24 </body>
25 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3     request.setCharacterEncoding("utf-8");
 4 %>
 5 <!DOCTYPE HTML>
 6 <html>
 7 <head>
 8 <title>•</title>
 9 </head>
10 <body>
11     <%
12         String uname = request.getParameter("uname");
13         String password = request.getParameter("password");
14         if (uname.equals(password)) {
15             session.setAttribute("uname", uname);
16             request.getRequestDispatcher("main.jsp").forward(request,
17                     response);
18         } else {
19     %>
20     <script type="text/javascript">
21         alert("登录失败");
22     </script>
23     <%
24         request.getRequestDispatcher("login.jsp").forward(request,
25                     response);
26         }
27     %>
28 </body>
29 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML>
 3 <html>
 4   <head>
 5     <meta charset="utf-8">
 6     <title></title>
 7   </head>
 8   <body>
 9     <%session.invalidate(); %>
10     <script type="text/javascript">
11         window.location.href="main.jsp";
12     </script>
13   </body>
14 </html>

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

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML>
 3 <html>
 4 <head>
 5 <meta charset="utf-8">
 6 <title></title>
 7 </head>
 8 <body>
 9     <%
10         String uname = (String) session.getAttribute("uname");
11         //如果他是空,说明没登陆,直接访问该页面了
12         if (uname == null)
13             response.sendRedirect("../login/login.jsp");
14     %>
15     欢迎你<%=uname%><br>
16     <br>
17     <a href="../login/logout.jsp">退出登录</a>
18     <hr>
19     商品展示:
20     <br> 欢迎进入水果商城:
21     <br>
22     <hr>
23     <form action="/demo4/cart/show.jsp" method="post">
24         请选择要加入购物车的商品:<br> <input type="checkbox" name="list" value="荔枝">
25         荔枝 9.99/kg<br> <input type="checkbox" name="list" value="桃子">
26         桃子 6.88/kg<br> <input type="checkbox" name="list" value="西瓜">
27         西瓜 7.99/kg<br>
28         <hr>
29         <input type="submit" value="加入购物车">
30 
31     </form>
32 </body>
33 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML>
 3 <html>
 4 <head>
 5 <meta charset="utf-8">
 6 <title></title>
 7 </head>
 8 <body>
 9     <form name="formName" action="dologin.jsp" method="post">
10     
11         <input name="uname"><br>
12         <input type="password" name="password"><br>
13         <input type="button" value="登录" onclick="login()"> 
14     </form>
15     <script type="text/javascript">
16         function login(){
17             if(formName.uname.value==""||formName.password.value==""){
18                 alert("账号或密码不能为空!");
19                 return;
20             }
21             formName.submit();
22         }
23     </script>
24 </body>
25 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3     request.setCharacterEncoding("utf-8");
 4 %>
 5 <!DOCTYPE HTML>
 6 <html>
 7 <head>
 8 <title>•</title>
 9 </head>
10 <body>
11     <%
12         String uname = request.getParameter("uname");
13         String password = request.getParameter("password");
14         if (uname.equals(password)) {
15             session.setAttribute("uname", uname);
16             request.getRequestDispatcher("../cart/main.jsp").forward(request,
17                     response);
18         } else {
19     %>
20     <script type="text/javascript">
21         alert("登录失败");
22     </script>
23     <%
24         request.getRequestDispatcher("login.jsp").forward(request,
25                     response);
26         }
27     %>
28 </body>
29 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML>
 3 <html>
 4   <head>
 5     <meta charset="utf-8">
 6     <title></title>
 7   </head>
 8   <body>
 9     <%session.invalidate(); %>
10     <script type="text/javascript">
11         window.location.href="main.jsp";
12     </script>
13   </body>
14 </html>
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML>
 3 <html>
 4 <head>
 5 <meta charset="utf-8">
 6 <title></title>
 7 </head>
 8 <body>
 9     <h2>购物车添加的商品有:</h2>
10     <%
11         request.setCharacterEncoding("utf-8");
12         String listName[] = request.getParameterValues("list");
13         if (listName == null) {
14             out.print("购物车为空!");
15         } else {
16             for (int i = 0; i < listName.length; i++) {
17                 out.print("(" + (i + 1) + ")" + listName[i] + "<br>");
18             }
19         }
20     %>
21 </body>
22 </html>

 

posted @ 2022-04-24 13:54  Lwk36  阅读(9)  评论(0编辑  收藏  举报