字符編碼亂碼問題(servlet底層 編碼大揭祕)

好多初學者會遇到,請求過去的信息內包含中文(通常會是get方式提交過去的請求會出現)。好鬱悶,這是爲何呢。有下面分析下,說的很差能夠吐槽javascript

話說咱們能遇到這種編碼的問題,歸根結底就是這  這 web開發不是中國人開發的,中國文化博大精深,四大發明淵源流傳,惋惜,咱們太本身爲是了,來了個閉關鎖國政策,弄得中國跟不上時代的步伐,不潮了,落伍了,互聯網時代被西方人搶了個先,發明了,咱們只能用別人的了。咱們也知道,西方講的是英語,因此他們採用的編碼格式是iso-8895-1。而這  這種編碼方式只佔兩個字節,不適合咱們中文(不光是中文,其餘的語言也不怎麼支持,嘿嘿),因此,爲了世界各國的語言都支持,Ken Thompson(有一外國人,where is 中國人),發明了utf-8編碼格式,是可變長度字符編碼的萬能碼(又有多少真正被堪稱萬能的,牛)。其中,中文佔3個字節。因此  因此想服務器發送請求的時候,get方式都要轉碼。html

廢話很少說,正題例子才霸氣。java

package com.servlet;

import java.io.IOException;

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

public class Register extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        process(req, resp);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        process(req, resp);
    }
    
    public void process(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        
        String usernmae = req.getParameter("username");
        String gender = req.getParameter("gender");
        String[] interest = req.getParameterValues("interest");
        String address = req.getParameter("address");
        String connent = req.getParameter("connent");
        
        usernmae = new String(usernmae.getBytes("iso-8859-1"),"UTF-8");//字符的解碼和轉碼,iso-8859-1表明的2個字節大小,utf-8各個國家中的
        gender = new String(gender.getBytes("iso-8859-1"),"UTF-8");
        connent = new String(connent.getBytes("iso-8859-1"),"UTF-8");
        address = new String(address.getBytes("iso-8859-1"),"UTF-8");
        
        
        System.out.println(usernmae);
        System.out.println(gender);
        System.out.println(interest.length);
        System.out.println(address);
        System.out.println(connent);
    }
}
<%@ 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">
<meta content="">
<title>Insert title here</title>
<script type="text/javascript">
    function validate()
    {
        var username = document.getElementsByName("username")[0];
        if(username.value.length < 1)
            {
                alert("用戶名不能爲空");
            }
        
        var gender = document.getElementsByName("gender");
        if(!gender[0].checked && !gender[1].checked)
            {
            alert("請選擇性別");
            }
        
        var interest = document.getElementsByName("interest");
        
        var n = 0;
        for(var i = 0; i < interest.length; i++)
            {
                if(interest[i].checked)
                    n++;
            }
        if(n < 1)
            {
            alert("興趣至少選擇一個");
            }
        var address = document.getElementsByName("address");
        
        
    }
</script>
</head>
<body>

<form action="Register">
    用戶名:<input name="username" type="text"><br>
    性別:男<input type="radio" value="男" name="gender">&nbsp;&nbsp;&nbsp;<input type="radio" value="女" name="gender">
    <br>
    興趣:足球<input type="checkbox" name="interest" value="足球">&nbsp;&nbsp;
    藍球<input type="checkbox" name="interest" value="籃球">&nbsp;&nbsp;
    羽毛球<input type="checkbox" name="interest"value="羽毛球">&nbsp;&nbsp;
    拍球<input type="checkbox" name="interest" value="排球">&nbsp;&nbsp;<br>
    地址:<select name="address">
        <option value="上海">上海</option>
        <option  value="天津">天津</option>
        <option value="濟南">濟南</option>
        <option value="德州">德州</option>
    </select>
    <br>說明:<textarea name="connent" rows="15" cols="20"></textarea><br>
    <input type="submit" onclick="validate()" value="充值">
&nbsp;</form>
</body>
</html>
usernmae = new String(usernmae.getBytes("iso-8859-1"),"UTF-8");理解,usernmae.getBytes("iso-8859-1")就是以iso-8859-1的方式獲得username。而後新建一個string類型的字符串,將其編碼格式定位utf-8,就完活了,簡單不,不過,仍是那句話,爲何web不是中國人發明的。讓老外也嚐嚐編碼的苦楚。
相關文章
相關標籤/搜索