JavaWeb开发中的中文乱码问题

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>中文乱码.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body>
        <form action="/Servlet/servlet/RequestDemo4" method="post">
                        用户名:<input type="text" name="username">
                        <input type="submit" value="提交">
            </form>

            <form action="/Servlet/servlet/RequestDemo4" method="get">
                        用户名:<input type="text" name="username">
                        <input type="submit" value="提交">
            </form>
  </body>
</html>
package request;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestDemo4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        test1(request);



        //test2(request);
        //test3(request,response);
    }

    //测试题,下面程序有没乱码 :答案是没有
    private void test3(HttpServletRequest request,HttpServletResponse response)
        throws IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        //上面执行完之后相当于String username="中国"
        response.setCharacterEncoding("gb2312");//response设置为gb2312方式编码
        response.setContentType("text/html;charset=gb2312");//通知浏览器用gb2312打开
        response.getWriter().write(username);
    }


    //解决post提交的问题
    private void test1(HttpServletRequest request)
            throws UnsupportedEncodingException {
        //在之前设置
        request.setCharacterEncoding("UTF-8");//只对post有效

        String name = request.getParameter("username");
        System.out.println(name);//出现乱码 浏览器使用当前使用的码表提交
    }

    //超链接提供的中文,服务器想不乱码,也只能手工处理,即get方式处理
    //解决get提交的问题
    private void test2(HttpServletRequest request)
            throws UnsupportedEncodingException {
        String name = request.getParameter("username");
        name = new String(name.getBytes("iso8859-1"),"UTF-8");  //反向去查iso8859-1码表的表示,再把他在转为UTF-8码表对应的字符
        System.out.println(name);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

浏览器输入:http://localhost:8080/Servlet/form1.html
**


  • 先测试test1(request);注释其他;

  • 使用post提交

这里写图片描述
发现正确获取中国两个字.

  • 使用get提交

这里写图片描述
发现出现乱码问题
【解析】:
request.setCharacterEncoding():是设置从request中取得的值或从数据库中取出的值。
指定后可以通过getParameter()则直接获得正确的字符串,如果不指定,则默认使用iso8859-1编码。值得注意的是在执行setCharacterEncoding()之前,不能执行任何getParameter()。而且,该指定只对POST方法有效,对GET方法无效分析原因,应该是在执行第一个getParameter()的时候,java将会按照编码分析所有的提交内容,而后续的getParameter()不再进行分析,所以setCharacterEncoding()无效。而对于GET方法提交表单是,提交的内容在URL中,一开始就已经按照编码分析提交内容,setCharacterEncoding()自然就无效。


2. 测试test2(request);注释掉其他

  • 使用post提交
  • 这里写图片描述
    结果发现正常显示中国

  • 使用get提交

结果发现正常显示中国
【解析】:
浏览器将数据(假设为“中国”)发送给服务器的时候,将数据变成0101的二进制数据(假设为98 99)时必然要查码表,浏览器以哪个码表打开网页,浏览器就以哪个码表提交数据。数据到达服务器后,数据(98 99)要封装到request中,在servlet中调用Request的getParameter方法返回的是字符串(“中国”),方法内部拿到数字后要转成字符,一定要查码表,由于request的设计者是外国人,所以默认查的是他们常用的ISO8859-1,这就是请求数据产生乱码的根源。 get方式提交的数据依然是浏览器用什么码表打开就用什么码表发送。不同的是,以get方式提交数据时,request设置编码无效。即使设置了UTF-8还是会去查ISO8859-1。得到乱码,要解决这个问题,需要拿着乱码反向查ISO8859-1,拿到(98 99)后,再去查正确码表。所以我们反向去查iso8859-1码表的表示,再把他在转为UTF-8码表对应的字符,来解除乱码问题。


3. 测试test3(request,response);注释掉其他

  • 使用post提交
    这里写图片描述
    结果发现正常显示中国

【解释】:post提交后,setCharacterEncoding对post有效。执行完request.setCharacterEncoding(“UTF-8”);
String username = request.getParameter(“username”);
相当于String username=”中国”这里没有出现乱码问题,故后面可以正常显示出中国。

  • 使用get提交
    这里写图片描述
    发现出现乱码问题
    【解析】:get提交后,setCharacterEncoding对get无效。执行完request.setCharacterEncoding(“UTF-8”);
    String username = request.getParameter(“username”);
    相当于String username=”??”出现乱码问题,故后面的输出也出现乱码问题。

提交数据中文乱码问题总结:

1.如果提交方式为post,想不乱码,只需要设置request对象的编码即可。

  注意:客户机数据是以哪种方式提交的,request就应该设成什么编码。

2.如果提交方式为get,设置request对象的编码是无效的,想不乱码,只能手工转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值