您的位置:首页 > Web前端 > JavaScript

教你5种JSP页面显示为乱码的解决方法

2011-08-26 09:22 666 查看
教你5种JSP页面显示为乱码的解决方法

JSP编程中网页显示出现乱码的情况,基本可以归为5类:

1. JSP页面显示乱码。

2. Servlet接收Form/Request传递的参数时显示为乱码

3. JSP接收Form/Request传递的参数时显示为乱码

4. 用<jsp:forward page="catalog2.html"></jsp:forward>时页面显示乱码

5. 数据库存取的时候产生乱码。

下面给出全部解决方法:

步骤/方法

1. JSP页面显示乱码。

第一种为在页面的开头加上:

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

注:有时候如果不再页面开头加上这句,则页面中无法保存中文字符,并提示:中文字符在不能被iso-8859-1字符集mapped,这是由于默认情况下,JSP是用iso-8859-1来编码的,可以在Window->Preferences->General->Content Type选项下,在右边的窗口选择Text->Jsp,然后在下面的Default Encoding由默认的iso-8859-1改为GBK,然后点击update即可解决。

然而这种方式会带来一些问题:由于这一句在其他文件include该文件的时候不能被继承,所以include它的文件也需要在文件开头加上这句话,此时如果用的是pageEncoding="gbk"则会出现问题。类似于org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html;charset=GBK,
new: text/html;charset=gbk).

类似地,如果两个文件一个用的是gbk,一个用的是gb2312也会出现问题。

另一种更好的解决方式为:

在项目的web.xml中添加以下片段:

<jsp-config>

<jsp-property-group>

<description>

Special property group for JSP Configuration JSP example.

</description>

<display-name>JSPConfiguration</display-name>

<url-pattern>*.jsp</url-pattern>

<el-ignored>true</el-ignored>

<page-encoding>GBK</page-encoding>

<scripting-invalid>false</scripting-invalid>

<include-prelude></include-prelude>

<include-coda></include-coda>

</jsp-property-group>

<jsp-property-group>

<description>

Special property group for JSP Configuration JSP example.

</description>

<display-name>JSPConfiguration</display-name>

<url-pattern>*.html</url-pattern>

<el-ignored>true</el-ignored>

<page-encoding>GBK</page-encoding>

<scripting-invalid>false</scripting-invalid>

<include-prelude></include-prelude>

<include-coda></include-coda>

</jsp-property-group>

</jsp-config>

2. Servlet接收Form/Request传递的参数时显示为乱码的解决方式:

第一种解决方式为在用到request方法的前面加上这条语句:

request.setCharacterEncoding("GBK");

同样地,这也会由于页面设置中GbK或gB2312大小写不同或者采用不同的汉语字符集而发生错误。

另一种更好的解决方式为:添加一个名为SetCharacterEncodingFilter的filter。

filter的源文件为(参见apach安装目录下\webapps\jsp-examples\WEB-INF\classes\filters中的SetCharacterEncodingFilter.java文件):

package com.filters;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;

import javax.servlet.UnavailableException;

public class SetCharacterEncodingFilter implements Filter {

protected String encoding = null;

protected FilterConfig filterConfig = null;

protected boolean ignore = true;

public void destroy() {

this.encoding = null;

this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain)

throws IOException, ServletException {

// Conditionally select and set the character encoding to be used

if (ignore || (request.getCharacterEncoding() == null)) {

String encoding = selectEncoding(request);

if (encoding != null)

request.setCharacterEncoding(encoding);

}

// Pass control on to the next filter

chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

this.encoding = filterConfig.getInitParameter("encoding");

String value = filterConfig.getInitParameter("ignore");

if (value == null)

this.ignore = true;

else if (value.equalsIgnoreCase("true"))

this.ignore = true;

else if (value.equalsIgnoreCase("yes"))

this.ignore = true;

else

this.ignore = false;

}

protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}

同时在web.xml中添加一下片段:

<filter>

<filter-name>SetCharacterEncoding</filter-name>

<filter-class>com.filters.SetCharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>GBK</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>SetCharacterEncoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

3. JSP接收Form/Request传递的参数时显示为乱码

当我们按照第二种乱码的解决方式修改了web.xml并添加了filter之后,有时候并不一定就对乱码问题高枕无忧了,有时候我们会奇怪的发现Sevlet接收Form/Request传递的参数可以正常显示了,但是jsp页面接受Form/Request传递的参数却仍然显示为乱码。这是为什么呢?

对于我遇到的情况而言,我发现是由于我在用Form发送信息的页面采用了这样的html:

<form action="getParam.jsp" >

姓名<input type="text" name ="UserName"> <br>

选出你喜欢吃的水果:

<input type ="checkbox" name = "checkbox1" value = "苹果"> 苹果

<input type ="checkbox" name = "checkbox1" value = "西瓜"> 西瓜

<input type ="checkbox" name = "checkbox1" value = "桃子"> 桃子

<input type ="checkbox" name = "checkbox1" value = "葡萄"> 葡萄

<input type = "submit" value = "提交">

</form>

也就是说没有指定form的method属性。而问题就发生在此,Form的默认mothod属性为get.

而get是通过在发送请求的url后面加?然后加参数和值来传递数据的的,编码格式为ASCII.这就要求我们在传递的数据中有非ASCII字符或是超过了100个字符,那么你必须使用method="post",否则就会出现乱码。

所以解决方式为:第二种乱码的解决方式+在发送页面的Form中指定method为post.

4. 用<jsp:forward page="catalog2.html"></jsp:forward>时页面显示乱码的解决方式

此时实际上乱码的原因和产生其他几种乱码的原因不同,它的原因在于我们用eclipse编辑要forward的html或者jsp文件时,采用的编码是可以产生中文乱码的编码而不是GBK或者GB2312.所以解决方式就是把eclipse编辑器的编码方式改为GBK或者GB2312.

具体操作方式见:上文红色字体部分。

5. 数据库存取的时候产生乱码的解决方式

当然,在写数据库时,也要保正数据库的编码与其它一致:

我们可以在系统盘windows目录下的my.ini文件,在文件中插入一行default-character-set=GBK,但上面说了这么多,大家也应该明白些了吧,改动太多的默认设置不是我的风格,因此上,这一行还是不要加的好。

但不管怎么样,我们还是要创建一个基于中文编码的数据库,当然,用客户端登录的时候,某些客户用自动把字体编码转换成中文编码。在这里,我想说一下在DOS下创建中文编码数据库的方法:

在进入数据库的时候,用mysql --default-character-set=gbk -u root -p 这句话进入mysql,然后创建数据库,如:create database admin;这样创建起来的数据库就是基于中文编码的了。

用连接数据库的时候,读出的数据也可能是乱码,解决这个问题的方法非常简单,只要在你建立数据库连接的时候把URL设置成下面这个样子就可以了:URL= jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=GBK

最后总结,把各种地方的编码统一起来,所在的乱码问题就都解决了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: