后台接口向数据库录入汉字时乱码以及自动过滤文字经验总结

本人第一次写博客,而且是一个菜鸟,如果有什么不完善的地方请前辈们多多指教。

 2017-06-01 21:56:05

因为公司功能的需求,需要写一个溯源信息的接口,下面的代码是前台的一个用户评论的界面。所谓溯源就是追溯源头,有点类似于淘宝的物流。通过溯源信息可以查询到商品的产地、质量、商标等等一系列跟产品有关的信息。

productid:溯源产品的对应的编号,由前一个页面自动传递过来,通过这个属性将客户评论的内容与溯源产品相绑定。

username:评论者的名字。

telephone:评论者的联系方式。

rating:评论的等级,就是我们订餐时给商家评论是几颗星一样。星星的标志由前台来处理,这里只是获取从前台传递过来的值。

commentcontent:评论的内容,要求不能显示脏话、骂人话等不雅的词语,如果在评论的内容中存在这样的语言,代码将自动将其过滤掉替换为"*",并存入数据库中。

下面是具体的实现接口。

  1 public void addCommentMessage(HttpServletRequest request,
  2             HttpServletResponse response) throws UnsupportedEncodingException {
  3         JSONObject jsonObject = new JSONObject();
  4         // 敏感词汇集合
  5         String[] discuss = { "孙子", "草", "你妈", "儿子", "爸爸", "傻逼", "sb", "尼玛",
  6                 "你大爷", "混蛋", "滚", "靠", "完犊子" };
  7         String productid = request.getParamter("productid");
  8 
  9     // 修改乱码格式;这个主要是针对中文汉字来说的,这样处理在前台输入的中文汉字将不会以乱码形式存放在数据库中了。下面的代码同样的道理
 10         productid = new String(productid.getBytes("ISO-8859-1"), "utf-8");
 11 
 12     //从前台获取客户评论填写的名字
 13         String username = request.getParameter("username");
 14 
 15    //修改乱码格式
 16         username = new String(username.getBytes("ISO-8859-1"), "utf-8");
 17 
 18     //从前台获取客户评论填写的联系方式
 19         String telephone = request.getParameter("telephone");
 20 
 21     //修改乱码格式
 22         telephone = new String(telephone.getBytes("ISO-8859-1"), "utf-8");
 23 
 24     //从前台获取客户评论填写的评价等级
 25         String rating = request.getParameter("rating");
 26 
 27     //修改乱码格式
 28         rating = new String(rating.getBytes("ISO-8859-1"), "utf-8");
 29 
 30      //从前台获取客户评论填写的评论内容
 31         String commentcontent = request.getParameter("commentcontent");
 32 
 33     //修改乱码格式
 34         commentcontent = new String(commentcontent.getBytes("ISO-8859-1"),"utf-8");
 35         request.setCharacterEncoding("utf-8");
 36         try {
 37 
 38     //CommentEntity为评论的实体类,在这里直接引用的。向数据库中射值得set方法就在其中
 39             CommentEntity commentEntity = new CommentEntity();
 40 
 41     //判断传过来的productid是否为空字符串或null或"null"
 42             if (!productid.equals("") && !productid.equals(null)
 43                     && !productid.equals("null")) {
 44 
 45       //通过commentEntity实体类的set方法将信息存入到数据库中
 46                 commentEntity.setProductid(productid);
 47             }
 48             if (!username.equals("") && !username.equals(null)
 49                     && !username.equals("null")) {
 50                 commentEntity.setUsername(username);
 51             }
 52             if (!telephone.equals("") && !telephone.equals(null)
 53                     && !telephone.equals("null")) {
 54                 commentEntity.setTelephone(Integer.valueOf(telephone));
 55             }
 56             if (!rating.equals("") && !rating.equals(null)
 57                     && !rating.equals("null")) {
 58                 commentEntity.setRating(rating);
 59             }
 60             if (!commentcontent.equals("") && !commentcontent.equals(null)
 61                     && !commentcontent.equals("null")) {
 62                 // 判断评论信息是否包含敏感词汇,如果包含则将敏感部分以*的形式录入数据库
 63                 for (String s : discuss) {
 64                     if (commentcontent.contains(s)){
 65                         String str = commentcontent.replaceAll(s, "***");
 66                         commentcontent=str;
 67                         commentEntity.setCommentcontent(commentcontent);
 68                     }else if (!commentcontent.contains(s)) {
 69                         commentEntity.setCommentcontent(commentcontent);
 70                     }
 71                 }
 72 
 73             }
 74             Serializable save = this.commentService.save(commentEntity);
 75             if (!save.toString().equals("")) {
 76                 // 成功,jsonObject 赋值
 77                 jsonObject.put("code", "1");
 78                 jsonObject.put("message", "添加评论成功");
 79                 jsonObject.put("data", "");
 80                 
 81             } else {
 82                 // 失败,为jsonObject 赋值
 83                 jsonObject.put("code", "0");
 84                 jsonObject.put("message", "添加评论失败");
 85                 jsonObject.put("data", "");
 86             }
 87         } catch (NumberFormatException e) {
 88             // TODO Auto-generated catch block
 89             e.printStackTrace();
 90         }
 91         try {
 92             // 把json传到页面callback + "(" + json + ")"
 93             String callback = request.getParameter("callback");
 94             String jsonp = callback + "(" + jsonObject.toString() + ")";
 95             // 输出jsonObject
 96             PrintWriter writer = response.getWriter();
 97             response.setCharacterEncoding("utf-8");
 98             response.setContentType("text/html;charset=utf-8");
 99             writer.println(jsonp);
100         } catch (Exception e) {
101             // TODO: handle exception
102         }
103     }
原文地址:https://www.cnblogs.com/1925yiyi/p/6930734.html