zoukankan      html  css  js  c++  java
  • Java中的中文乱码问题

     客户端向服务器发送请求时,有两种方式post和get请求,当客户端提交的内容有中文时。服务器要进行设置才能获得中文,否则获得的是乱码。那么怎么设置呢:在servlet中进行设置,有两种情况:1:请求时服务器获得客户提交的中文,

                            2:响应时客户端浏览器得到响应的中文

       1:服务器获得客户端提交的内容,两种情况get和post请求

        1)链接、Js等get请求中文乱码处理方式

    String username=new String(request.getParameter("username").getBytes("iso-8859-1"));

    这样表单中usename输入的是汉字的话,再servle中用上述写法得到的才不是乱码

    2)普通表单get请求中文乱码处理方式

        String username=new   String(request.getParameter("username").getBytes("iso-8859-1"),"utf-8")

      (2)Post请求

        request.setCharacterEncoding("utf-8");--设置字符编码

    String username=request.getParameter("username");

      2:在servlet中响应信息,将信息相应在客户端浏览器上

       就是客户端浏览器要得到服务器响应的中文有两种情况:但是第二种情况用在第一种情况中也行,所以以后都用第二种情况。

        1)在jsp页面中用Js或是域对象得到中文在输出,在servlet中直接写:

      response.setCharacterEncoding("utf-8");

      response.getWriter().print("用户名输入错误");

    2)直接在servlet中做个输出流

        response.setContentType("text/html;charset=utf-8");

       response.getWriter().print("用户名123456");

    ==========================================

    1:mysql中文乱码?

     1:进入数据库--右击数据库----更改数据库---更改格式
     2:右击表---更改表-----高级属性---更改格式
     3:在mysql配置文件中设置----mysql安装路径---my.ini配置文件--更改两个属性
         default-character-set=utf8
         character-set-server=utf8
    注意:在MySQL中utf-8的写法是utf8,如果写成utf-8会报错

  • 相关阅读:
    sublime去除空白行和重复行
    python list删除数据 和复制 列表
    微博实现简繁体转换
    2017.10.27日面试总结
    python 类和__class__理解
    python 单例模式应用
    pt-query-digest 慢日志监控
    在线安全清空慢查询日志slowlog
    Linux高级系统恢复技术
    灾备演练
  • 原文地址:https://www.cnblogs.com/xueershewang/p/6832008.html
Copyright © 2011-2022 走看看