javaweb开发技术-第五章实验
2022-09-18 19:23:54 # javaweb

javaweb开发技术教程(第二版)第五章实验

将用户的会话信息存储在session中

goods.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/18
Time: 17:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
<title>Title</title>
<style>
* {
padding: 0;
margin: 0;
}
h3 {
text-align: center;
}
.tb {
margin: 40px auto;
width: 300px;
height: 250px;
}
table {
width: 100%;
height: 80%;
}
button {
display: block;
width: 80px;
height: 30px;
margin: 10px auto;
}
</style>
</head>
<body>
<h3>商品列表</h3>
<form action="goods_ok.jsp" method="post" class="tb">
<table border="1px">
<tr>
<td>商品标号</td>
<td>商品名称</td>
<td>商品价格</td>
</tr>
<tr>
<td><input type="checkbox" name="goods" value="图书">1</td>
<td>图书</td>
<td>20</td>
</tr>
<tr>
<td><input type="checkbox" name="goods" value="CD">2</td>
<td>CD</td>
<td>10</td>
</tr>
<tr>
<td><input type="checkbox" name="goods" value="衣服">3</td>
<td>衣服</td>
<td>390</td>
</tr>
<tr>
<td><input type="checkbox" name="goods" value="玩具">4</td>
<td>玩具</td>
<td>5</td>
</tr>
<tr>
<td><input type="checkbox" name="goods" value="手机">5</td>
<td>手机</td>
<td>2090</td>
</tr>
</table>
<button type="submit">购买</button>
</form>
</body>
</html>

goods_ok.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.lang.reflect.Array" %>
<%@ page import="java.util.Arrays" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/18
Time: 17:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=GBK" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
String[] goods = request.getParameterValues("goods");
if(goods == null){
out.println("您没有选中的物品");
out.println("<a href='goods.jsp'>返回商店</a>");
}
else{
out.println("您确定要购买:");
for(String t:goods){
out.print(t+' ');
}
out.println('?');
ArrayList<String> carts = (ArrayList<String>)session.getAttribute("carts");
if(carts==null){
carts = new ArrayList<String>();
}
ArrayList<String> carts_add = (ArrayList<String>)session.getAttribute("carts_add");
if(carts_add==null){
carts_add = new ArrayList<String>();
}
for(String t:goods){
if(!carts.contains(t)){
carts_add.add(t);
}
}
session.setAttribute("carts_add",carts_add);
%>
<a href="carts_yes.jsp">是</a>
<a href="carts_no.jsp">否</a>
<%
}
%>
</body>
</html>

carts_yes.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/18
Time: 18:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
ArrayList<String> carts = (ArrayList<String>)session.getAttribute("carts");
ArrayList<String> carts_add = (ArrayList<String>)session.getAttribute("carts_add");
if(carts==null){
carts = new ArrayList<String>();
}
if(carts_add == null){
carts_add = new ArrayList<String>();
}
for(String t:carts_add){
carts.add(t);
}
session.setAttribute("carts",carts);
response.sendRedirect("carts.jsp");
%>
</body>
</html>

carts_no.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/18
Time: 18:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%

session.setAttribute("carts_add",null);
response.sendRedirect("goods.jsp");
%>
</body>
</html>

carts.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: DELL
Date: 2022/9/18
Time: 18:22
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
ArrayList<String> carts = (ArrayList<String>)session.getAttribute("carts");
ArrayList<String> carts_add = (ArrayList<String>)session.getAttribute("carts_add");
out.print("本次新增购买的商品: ");
for(String t:carts_add){
out.print(t+' ');
}
out.println();
out.print("您的购物车里共有: ");
for(String t:carts){
out.print(t+' ');
}
out.println("<a href='goods.jsp'>返回商店</a>");
%>
</body>
</html>