- /*
- *这里将请求转发过来的信息处理之后,给浏览器,有显示所有和单一商品
- */
- 类:ShowAll
- public class ShowAll extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html;charset=utf-8");
- PrintWriter pw = response.getWriter();
- /*
- * 构建一个表格
- */
- pw.print("<div align=center>商品信息汇总表</div><hr width=300 align=center/>");
- pw.print("<table border=1 align=center>");
- pw.print("<tr>");
- pw.print("<td>编号</td>");
- pw.print("<td>名称</td>");
- pw.print("<td>价格</td>");
- pw.print("<td>数量</td>");
- pw.print("<td>查看</td>");
- pw.print("</tr>");
-
- Map<String, Product> pros = (Map<String, Product>) request.getAttribute("pros");
- if(pros!=null){
- for ( Entry<String, Product> entry : pros.entrySet()) {
- Product pt = entry.getValue();
- pw.print("<tr>");
- pw.write("<td>"+pt.getId()+"</td>");
- pw.write("<td>"+pt.getName()+"</td>");
- pw.write("<td>"+pt.getPrice()+"</td>");
- pw.write("<td>"+pt.getNumb()+"</td>");
- pw.write("<td><a href='/HE02/findOne?id="+pt.getId()+"'>详情</a></td>");
- pw.print("</tr>");
- }
- }
- pw.print("</table><br/><br/><br/>");
-
- pw.write("<div align=center>商品浏览记录<hr width=300 align=center />");
- Cookie[] cookies = request.getCookies();
- if(cookies !=null){
- for (Cookie cookie : cookies) {
- if("ids".equals(cookie.getName())){
- String[] ids = cookie.getValue().split(",");
- for (int i=0;i<ids.length;i++) {
- Product pt = DB.findById(ids[i]);
- pw.write((i+1)+":<a href=/HE02/findOne?id="+pt.getId()+">"+pt.getName()+"</a><br/>");
- }
- }
- }
- }
- pw.write("</div>");
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- }
- 类:ShowOne
- public class ShowOne extends HttpServlet {
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html;charset=utf-8");
- //得到需要显示的商品
- Product pt = (Product) request.getAttribute("pro");
- //输出给浏览器
- show(response, pt);
- StringBuilder sb = new StringBuilder();
- sb.append(pt.getId());
-
- //更新cookies信息
- Cookie cookie = updateCookie(request, pt, sb);
- response.addCookie(cookie);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- public void show(HttpServletResponse response, Product pt)
- throws IOException {
- PrintWriter pw = response.getWriter();
- pw.write("<div align=center>");
- pw.write("商品详情 <hr width=300 align=center/>");
- pw.write("商品:"+pt.getName()+" 甩卖:"+pt.getPrice());
- pw.write("<a href='/HE02/findAll'><br/>返回商品列表</a>");
- pw.write("</div>");
- }
-
- public Cookie updateCookie(HttpServletRequest request, Product pt,
- StringBuilder sb) {
- Cookie[] cookies = request.getCookies();
- if(cookies!=null){
- for (Cookie ck : cookies) {
- if(ck.getName().equals("ids")){
- String[] cookie = ck.getValue().split(",");
- for (String id : cookie) {
- if(!id.equals(pt.getId())){
- sb.append(","+id);
- }
- }
- }
- }
- }
- Cookie cookie = new Cookie("ids", sb.toString());
- cookie.setMaxAge(60);
- cookie.setPath("/");
- return cookie;
- }
- }
复制代码
|