import
java.io.*;
import
java.util.*;
import
javax.servlet.*;
import
javax.servlet.http.*;
import
net.sf.json.JSONObject;
import
com.masque.beans.BookBean;
import
com.masque.daoimpl.BookDaoImpl;
import
com.sun.xml.internal.bind.v2.runtime.Name;
import
com.sun.xml.internal.bind.v2.runtime.output.Encoded;
/**
* 此类描述的是对存储书籍的cookies的处理,再返回数据到jsp
*/
public
class
BuyOneBook
extends
HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType(
"text/html,charset=UTF-8"
);
int
bookId = Integer.parseInt((String) request.getParameter(
"count"
));
request.setCharacterEncoding(
"UTF-8"
);
Cookie[] cookie = request.getCookies();
String bookList =
null
;
boolean
isIn =
false
;
boolean
isNew =
true
;
if
(cookie !=
null
) {
for
(
int
i =
0
; i < cookie.length; i++) {
if
(cookie[i].getName().equals(
"jsonBookList"
)) {
bookList = cookie[i].getValue();
bookList = java.net.URLDecoder.decode(bookList,
"UTF-8"
);
}
}
System.out.println(
"bookList:"
+bookList);
if
(bookList !=
null
) {
JSONObject jsonBookList = JSONObject.fromObject(bookList);
Iterator iterator = jsonBookList.keySet().iterator();
while
(iterator.hasNext()) {
String key = iterator.next().toString();
if
(key.equals(bookId+
""
)) {
isIn =
true
;
break
;
}
}
if
(!isIn) {
int
bookSize =
1
;
addBook(bookId, jsonBookList,response,bookSize);
}
else
{
JSONObject jsonBook = JSONObject.fromObject(jsonBookList.get(bookId+
""
).toString());
int
bookSize = Integer.parseInt(jsonBook.get(
"bookSize"
).toString())+
1
;
addBook(bookId, jsonBookList,response,bookSize);
}
}
else
{
isNew =
false
;
}
}
else
{
isNew =
false
;
}
if
(!isNew) {
String data =
"{}"
;
JSONObject jsonBookList = JSONObject.fromObject(data);
int
bookSize =
1
;
addBook(bookId, jsonBookList,response,bookSize);
}
response.sendRedirect(
"readallbook.do?load=1"
);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public
void
doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
doGet(request, response);
}
private
void
addBook(
int
bookId, JSONObject jsonBookList,HttpServletResponse response,
int
bookSize) {
Map<String, String> map =
new
HashMap<String, String>();
BookBean bookBean = (
new
BookDaoImpl()).SelectOneBook(bookId);
map.put(
"bookId"
, bookId +
""
);
map.put(
"title"
, bookBean.getTitle());
map.put(
"price"
, bookBean.getPrice() +
""
);
map.put(
"bookSize"
, bookSize+
""
);
map.put(
"priceAll"
, bookBean.getPriceAll()+
""
);
jsonBookList.put(bookId, map);
String str=
null
;
try
{
str = java.net.URLEncoder.encode(jsonBookList.toString().replace(
"\""
,
"\'"
),
"UTF-8"
);
}
catch
(UnsupportedEncodingException e1) {
e1.printStackTrace();
}
Cookie cookie =
new
Cookie(
"jsonBookList"
,str);
response.addCookie(cookie);
}
}