本文實例講述了Java Web開發(fā)之基于Session的購物商店實現(xiàn)方法。分享給大家供大家參考,具體如下:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
package cn.com.shopping; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; //完成購買 public class BuyServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id=request.getParameter( "id" ); Book book=(Book)Db.getAll().get(id); //再加上那個關閉Cookie時session的剞劂方案 //阻止session的時候解決方案 HttpSession session=request.getSession( false ); //從session中得到用戶的保存所有書的集合(購物車) List list=(List)session.getAttribute( "list" ); if (list== null ) { list= new ArrayList(); session.setAttribute( "list" , list); } list.add(book); String url=response.encodeRedirectURL( "/Session/SessionCountDemo" ); response.sendRedirect(url); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } } package cn.com.shopping; import java.io.IOException; import java.io.PrintWriter; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; //顯示書 public class ListBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding( "UTF-8" ); response.setContentType( "text/html;charset=UTF-8" ); PrintWriter out=response.getWriter(); HttpSession session=request.getSession(); out.print( "本店有如下的商品:<br/>" ); Map<String ,Book > map=Db.getAll(); for (Map.Entry<String, Book> entry:map.entrySet()) { Book book=entry.getValue(); String url=response.encodeURL( "/Session/BuyServlet?id=" +book.getId()); out.print(book.getName()+ "<a href='" +url+ "' target='_blank' >購買</a><br/>" ); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } } //Db作為數(shù)據(jù)庫 class Db { private static Map<String ,Book> map= new LinkedHashMap(); static { map.put( "1" , new Book( "1" , "Java WEB開發(fā)" , "WY" , "好書" )); map.put( "2" , new Book( "2" , "WEB開發(fā)" , "zt" , "一般" )); map.put( "3" , new Book( "3" , "程序設計" , "df" , "較好書" )); map.put( "4" , new Book( "4" , "計算機組成" , "as" , "一般好書" )); map.put( "5" , new Book( "5" , "編譯原理" , "ty" , "很好書" )); map.put( "6" , new Book( "6" , "網(wǎng)絡維護" , "hj" , "非常好書" )); } public static Map getAll() { return map; } } //書 class Book { private String id; private String name; private String author; private String description; public Book() { super (); // TODO Auto-generated constructor stub } public Book(String id, String name, String author, String description) { super (); this .id = id; this .name = name; this .author = author; this .description = description; } public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this .author = author; } public String getDescription() { return description; } public void setDescription(String description) { this .description = description; } } package cn.com.shopping; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class SessionCountDemo extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding( "UTF-8" ); response.setContentType( "text/html;charset=UTF-8" ); PrintWriter out=response.getWriter(); HttpSession session=request.getSession(); if (session== null ) { out.write( "您沒買任何的商品!" ); return ; } out.write( "您購買了如下的商品:" ); List<Book> list=(List) session.getAttribute( "list" ); for (Book book:list) { out.write(book.getName()); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } } |
希望本文所述對大家Java web程序設計有所幫助。