中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

JSP開發(fā)過程遇到的中文亂碼問題及解決方案

2018-07-20    來源:編程學習網(wǎng)

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

 對于程序猿來說,亂碼問題真的很頭疼,下面列舉幾種常見的亂碼。

 1.數(shù)據(jù)庫編碼不一致導致亂碼

  解決方法:

  首先查看數(shù)據(jù)庫編碼,輸入:

show variables like "%char%";

  確認編碼一致,如果不一致,可輸入:

SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';

  也可設(shè)置成gbk編碼;

  也可以在安裝Mysql目錄下修改my.ini文件

default-character-set=utf-8

 2.jsp頁面亂碼問題

  在myeclipse中jsp的默認編碼為ISO-8859-8;

  只需在頁面頭部修改為

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

  在JSP頁面頭部加入下面這句話,告訴瀏覽器應(yīng)該調(diào)用UTF-8的字符集。

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

 3.jsp連接數(shù)據(jù)庫存入中文亂碼

  在數(shù)據(jù)庫連接時

jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

  如果使用框架連接則把頭文件都修改成UTF-8編碼即可

 4.在使用struts2可使用過濾器:

  先變寫一個過濾器

package com.oumyye.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class CharacterEncodingFilter implements Filter{

    protected String encoding = null;
    protected FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        if (encoding != null) {
            request.setCharacterEncoding(encoding);
            response.setContentType("text/html; charset="+encoding);
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
    }
}

  在web.xml中配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>0001web</display-name>
  <!-- 中文編碼過濾器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.oumyye.util.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

  在表單中只能使用post傳值,此方法對于get無效。

 5 處理單個字符串的中文亂碼問題

String newname=new String(name.getBytes("iso-8859-1"),"utf-8"))
  附:JSP中的編碼設(shè)置

  1. pageEncoding:<%@ page pageEncoding=“UTF-8″%>

  設(shè) 置JSP編譯成Servlet時使用的編碼

  2. contentType: <%@ page contentType=“text/html; charset=UTF-8″%>

  對服務(wù)器響應(yīng)進行重新編碼,即jsp的輸出流在瀏覽器中顯示的編碼

  3. html頁面charset:<META http-equiv=“Content-Type” content=“text/html; charset=UTF-8″>

  網(wǎng)頁的編碼信息 ,說明頁面制作所使用的編碼

  4. request.setCharacterEncoding()  — 可用在servlet和jsp頁面中

  作用是設(shè)置對客戶端請求進行重新編碼的編碼,即post方式提交的數(shù)據(jù)進行編碼。

  5. response.setCharacterEncoding() — 可用在servlet和jsp頁面中

  對服務(wù)器響應(yīng)進行重新編碼,即jsp的輸出流在瀏覽器中顯示的編碼,與<%@ page contentType=“text/html;charset=UTF-8″%>一樣

  6. response.setContentType() — 可用在servlet和jsp頁面中

  對服務(wù)器響應(yīng)進行重新編碼,即jsp的輸出流在瀏覽器中顯示的編碼,與<%@ page contentType=“text/html;charset=UTF-8″%>一樣 

  7.response.setHeader(“Content-Type”,”text/html;charset=UTF-8″);   — 可用在servlet和jsp頁面中

  與<META http-equiv=“Content-Type” content=“text/html; charset=UTF-8″>一樣

標簽: isp Mysql 服務(wù)器 數(shù)據(jù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:每個Java程序員需要了解的8個Java開發(fā)工具

下一篇:沒有代碼的編程語言:Folders