javaweb中servlet处理cookies+json

    
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" );
         // 得到jsp传过来的书本的id
         int  bookId = Integer.parseInt((String) request.getParameter( "count" ));
         // 得到cookies数组
         request.setCharacterEncoding( "UTF-8" );
         Cookie[] cookie = request.getCookies();
         // 定义变量用来接受书本保存的json的字符串表现形式
         String bookList =  null ;
         //定义一个标志位判断书籍的json你时候有该书籍
         boolean  isIn =  false ;
         boolean  isNew =  true ;
         if  (cookie !=  null ) {
             // 遍历cookie数组
             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" );
                     //bookList = new String(bookList.getBytes("ISO-8859-1"), "gbk");
                 }
             }
             System.out.println( "bookList:" +bookList);
             if  (bookList !=  null ) {
                 // 将字符串转换为json对象
                 JSONObject jsonBookList = JSONObject.fromObject(bookList);
                 // 获取json对象的迭代器
                 Iterator iterator = jsonBookList.keySet().iterator();
                 // 遍历迭代器,查询json中是否存在当前用户购买的书籍
                 while  (iterator.hasNext()) {
                     // 得到用户购买书籍的id
                     String key = iterator.next().toString();
                         // 判断是否已购买
                     if  (key.equals(bookId+ "" )) {
                         isIn =  true ;
                         break ;
                     }
                 }
                 if  (!isIn) {
                     //如果书籍没有被购买,初始化书籍的数量,再添加书籍到json
                     int  bookSize =  1 ;
                     //添加书籍到json
                     addBook(bookId, jsonBookList,response,bookSize);
                 } else  { //json存在该书籍,则给給书籍的数量加1
                     //已经被购买的书籍的json
                     JSONObject jsonBook = JSONObject.fromObject(jsonBookList.get(bookId+ "" ).toString());
                     int  bookSize = Integer.parseInt(jsonBook.get( "bookSize" ).toString())+ 1 ;
                     //添加书籍到json
                     addBook(bookId, jsonBookList,response,bookSize);
                 }
             } else  {
                 isNew =  false ;
             }
         } else  {
             isNew =  false ;
         }
         if  (!isNew) {
             //生成一个JSONObject对象
             String data =  "{}" ;
             JSONObject jsonBookList = JSONObject.fromObject(data);  
             //添加书籍到json
             int  bookSize =  1 ;
             addBook(bookId, jsonBookList,response,bookSize);
         }
         //跳转到处理cookies的servlet
         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>();
         // 根据书籍的id得到书籍实体对象
         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()+ "" );
         //将该书籍的信息的集合添加到json对象中
         jsonBookList.put(bookId, map);
         //将json的字符串形式添加到cookies中
         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);
         //cookie.setMaxAge(300);
         response.addCookie(cookie);
     }
     
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
javaweb是一种以Java为基础的web开发技术,它包括了servlet、jsp等相关技术。servlet是JavaEE规范的一种组件,它主要用于处理用户发送的HTTP请求和返回HTTP响应。servlet可以接收前端页面传来的数据,然后对数据进行处理并返回给前端页面。mysql是一种关系型数据库管理系统,它能够存储大量的数据并提供有效的数据管理和查询功能。在javaweb开发,我们可以通过servlet来连接mysql数据库,从而实现数据的持久化存储和操作。 jsp是一种基于Java的动态网页开发技术,它可以将Java代码嵌入到HTML页面,从而实现动态页面的生成和展示。jsp可以使用Java代码来获取数据库的数据并将其显示在前端页面上,实现了前后端的数据交互。 service是一种用于实现业务逻辑的组件或类,它负责处理具体的业务功能。在javaweb开发,我们可以将业务逻辑封装在service,然后在servlet调用相应的service方法,实现对数据的处理和操作。 考试无框架要求表示不可以使用现成的框架或库来完成开发任务,需要手动编写相关代码。在考试,可以考察学生对javaweb技术的理解和应用能力,要求学生能够熟练地使用servlet、jsp等相关技术,并能够编写出符合需求的代码。此外,对于数据库操作,学生还需要了解mysql的基本操作和相关的jdbc编程技巧。 总而言之,javaweb servlet mysql jsp service的考试是考察学生对javaweb开发技术的掌握程度和应用能力的一种考试形式,学生需要熟练地使用这些技术,并能够灵活运用到实际的开发场景
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值