码迷,mamicode.com
首页 > Web开发 > 详细

Web 项目遇到的乱码问题

时间:2017-08-31 22:25:43      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:web   问题:   内容   包含   ring   alt   中文编码   except   技术分享   

问题代码:

  jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
    <title>Insert title here</title>
  </head>
<body>

    <!-- 提交的时候必须是name=value键值对 -->
    
    <!-- 应用名+请求路径 -->
    <form action="/gp_web_day17/RequestDemo5" method="post">
        用户名:<input type="text" name="username"><br/>
        密    码:<input type="password" name="pwd"><br/>
        性    别:<input type="radio" name="sex" value="男" /><input type="radio" name="sex" value="女" />女<br />
               
        爱    好:<input type="checkbox" name="hobby" value="睡觉">睡觉           
               <input type="checkbox" name="hobby" value="游戏">游戏    
               <input type="checkbox" name="hobby" value="旅游">旅游<br />
        
        所在国家:<select name="sel">
                    <option>-----请选择-----</option>
                    <option value="cn">中国</option>
                    <option value="fa">法国</option>
                    <option value="en">英国</option>
                </select>
                
        <input type="submit" value="注册">
    </form>
</body>
</html>

   Servlet:

@WebServlet("/RequestDemo5")
public class RequestDemo5 extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public RequestDemo5() {
        super();
        
    }

    //处理get方式的乱码问题:
    //     get方式传递的参数内容默认编码方式问ISO-8859-1
    
    //    1 自己用再编码再解码处理
    //    2在tomcat的配置文件中加入URIEncoding="UTF-8"
    public String dispose(String str){
        
        try {
            
       //编码
byte[] arr = str.getBytes("ISO-8859-1");   //解码 String ssString = new String(arr, "UTF-8"); return ssString; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("解码前:"+request.getParameter("username")+" "+request.getParameter("sex")); String name = dispose(request.getParameter("username")); String sex = dispose(request.getParameter("sex")); System.out.println("解码后:"+name+" sex:"+sex); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

 

运行结果:

技术分享技术分享

 

问题分析:

技术分享

技术分享

问题解决:

//告诉浏览器使用ISO-8859-1编码方式,就可以检测get方法再编码再解码方式是否可以解决乱码问题

<meta http-equiv="Content-Type" content="text/html; charset="ISO-8859-1">

 

问题总结:

<!-- contentType="text/html; charset=UTF-8:  
        jsp在编辑器中保存文件,使用的是UTF-8编码格式
        问题:通过浏览器调用servlet文件,出现中文乱码
-->

<!-- pageEncoding="UTF-8:  
        jsp在编辑器中保存文件,使用的是UTF-8编码格式 
        问题:通过浏览器调用jsp,jsp出现中文乱码
-->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!-- 通过<meta>标签模拟response头,控制浏览器用ISO-8859-1的编码解析 --> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


//处理get方式的乱码问题:
  // get方式传递的参数内容默认编码方式ISO-8859-1

  // 1 自己用再编码再解码处理
  // 2在tomcat的配置文件中加入URIEncoding="UTF-8"

 

//处理post方法解决乱码问题:

//将response对象中的数据以UTF-8解码后的字节发送给浏览器
  // request.setCharacterEncoding("utf-8");

 

UTF-8国际编码  GBK中文编码(包含GB2312,即通过GB2312编码后可以通过GBK解码,反之不成立)

tomcat默认编码是ISO-8859-1

getBytes()通过平台默认字符集进行编码

get方式传递的参数内容默认编码方式ISO-8859-1

 

?? 表示没有编码

 

 参考博客:http://www.cnblogs.com/oldinaction/p/5167481.html

  

Web 项目遇到的乱码问题

标签:web   问题:   内容   包含   ring   alt   中文编码   except   技术分享   

原文地址:http://www.cnblogs.com/roxy/p/7460685.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!