前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JSP开发过程遇到的中文乱码问题及解决方法

JSP开发过程遇到的中文乱码问题及解决方法

作者头像
用户1289394
发布2018-02-26 17:13:52
9700
发布2018-02-26 17:13:52
举报
文章被收录于专栏:Java学习网Java学习网

JSP开发过程遇到的中文乱码问题及解决方法

1.数据库编码不一致导致乱码

解决方法:

首先查看数据库编码,输入:

代码语言:javascript
复制
show variables like "%char%";

确认编码一致,如果不一致,可输入:

代码语言:javascript
复制
SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';

也可设置成gbk编码;

也可以在安装Mysql目录下修改my.ini文件

代码语言:javascript
复制
default-character-set=utf-8

2.jsp页面乱码问题

在myeclipse中jsp的默认编码为ISO-8859-8;

只需在页面头部修改为

代码语言:javascript
复制
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>

在JSP页面头部加入下面这句话,告诉浏览器应该调用UTF-8的字符集。

代码语言:javascript
复制
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

3.jsp连接数据库存入中文乱码

在数据库连接时

代码语言:javascript
复制
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8

如果使用框架连接则把头文件都修改成UTF-8编码即可

4.在使用struts2可使用过滤器:

先变写一个过滤器

代码语言:javascript
复制
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中配置

代码语言:javascript
复制
<?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 处理单个字符串的中文乱码问题

代码语言:javascript
复制
String newname=new String(name.getBytes("iso-8859-1"),"utf-8"))
代码语言:javascript
复制
附:JSP中的编码设置

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

设置JSP编译成Servlet时使用的编码

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

对服务器响应进行重新编码,即jsp的输出流在浏览器中显示的编码

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

网页的编码信息 ,说明页面制作所使用的编码

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

作用是设置对客户端请求进行重新编码的编码,即post方式提交的数据进行编码。

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

对服务器响应进行重新编码,即jsp的输出流在浏览器中显示的编码,与<%@ page contentType=“text/html;charset=UTF-8″%>一样

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

对服务器响应进行重新编码,即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″>一样

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2015-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java学习网 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • JSP开发过程遇到的中文乱码问题及解决方法
  • 1.数据库编码不一致导致乱码
  • 2.jsp页面乱码问题
  • 3.jsp连接数据库存入中文乱码
  • 4.在使用struts2可使用过滤器:
  • 5 处理单个字符串的中文乱码问题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档