首页 > 编程知识 正文

jsp的class文件乱码(jsp乱码解决方案)

时间:2023-12-03 11:56:28 阅读:311768 作者:QCUI

本文目录一览:

  • 1、JSP乱码一般有什么解决办法?
  • 2、我的jsp程序出现乱码了,大家知道为什么?谢谢了
  • 3、关于jsp被tomcat编译后的网页出现汉字乱码。
  • 4、jsp页面中文乱码,怎么解决
  • 5、jsp中经常出现乱码,怎么解决
  • 6、怎么解决jsp中文乱码问题,我要疯了

JSP乱码一般有什么解决办法?

URIENcoding 设成GBK或GB2312

2. 表单中或传递字符串:本来输入的汉字是正常的,但是提交后再显示出来是乱码,因为提交的一般是 ISO8859编码,所以显示的时候要转成GB2312编码:

String S=new String(rs.getString("news").getBytes("gb2312"),"ISO8859_1");

//rs.getString("news")为待转换的字符串

然后使用S字符串的值就可以了

3. 有的服务器端的语言环境如果设成简体中文的也可以解决此类问题

4. 插入数据库中的字符是乱码

看看数据库中支持的是何种编码方式,用类似2中的方式作一下转换即可。

5. 总之,用jsp开发,碰到乱码,你得分析是读的时候发生乱码,还是写的时候发生乱码,用2中的转换,基本就能解决问题,有些时候写的时候做一次转换,例如:

String S=new String(rs.getString("news").getBytes("gb2312"),"ISO8859_1");

//读的时候在转换回来

String S=new String(rs.getString("news").getBytes("ISO8859_1"),"GB2312");

或者把ISO8859-1和GB2312 的位置换一下,自己多试试,就能找到解决问题的办法。

将乱码问题分为三类JSP页面显示中文乱码;表单提交乱码;数据库应用乱码

1) JSP页面内输出中文时出现乱码

解决方案在JSP文件中使用page命令指定响应结果的MIME类型,如%@ page language="java" contentType="text/html;charset=gb2312" %

2)表单提交乱码

表单提交时(post和Get方法),使用request.getParameter方法得到乱码,这是因为tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不同,下面分别说明。

(1)POST处理

对post提交的表单通过编写一个过滤器的方法来解决,过滤器在用户提交的数据被处理之前被调用,可以在这里改变参数的编码方式,过滤器的代码如下:

package cn.gov.beijingit.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 SetCharacterEncodingFilter implements Filter {

/**

* The default character encoding to set for requests that pass through this

* filter.

*/

protected String encoding = null;

/**

* The filter configuration object we are associated with. If this value is

* null, this filter instance is not currently configured.

*/

protected FilterConfig filterConfig = null;

/**

* Should a character encoding specified by the client be ignored?

*/

protected boolean ignore = true;

// --------------------------------------------------------- Public Methods

/**

* Take this filter out of service.

*/

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);

}

/**

* Place this filter into service.

*

* @param filterConfig

* The filter configuration object

*/

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-nameEncoding/filter-name

filter-class

cn.gov.beijingit.util.SetCharacterEncodingFilter

/filter-class

init-param

param-nameencoding/param-name

param-valuegbk/param-value

!--gbk或者gb2312或者utf-8--

/init-param

init-param

param-nameignore/param-name

param-valuetrue/param-value

/init-param

/filter

filter-mapping

filter-nameEncoding/filter-name

servlet-name/*/servlet-name

/filter-mapping

* 注意filter元素要放在所有web.xml元素之前。

(2) Get方法的处理

tomcat对post和get的处理方法不一样,所以过滤器不能解决get的乱码问题,它需要在其他地方设置。

打开tomcat_homeconf目录下server.xml文件,找到对8080端口进行服务的Connector组件的设置部分,给这个组件添加一个属性:URIEncoding="GBK"。修改后的Connector设置为:

Connector port="8080" maxHttpHeaderSize="8192"

maxThreads="150" minSpareThreads="25" maxSpareThreads="75"

enableLookups="false" redirectPort="8443" acceptCount="100"

connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" /

* 注意修改后重新启动tomcat才能起作用。

3)数据库应用的乱码,本文还是以mysql为例

(1)修改配置文件mysql_homemy.init

将default-character-set=latin1修改为default-character-set=gbk

(2) 对接受的中文字符重新编码,例

String name=requset.getParameter("name");

name = new String(name.getBytes("gbk"),"iso8859-1");

4)tomcat5.x的include页面乱码

为了能让tomcat5.x能像tomcat4那样处理include页面,需要修改项目web-info/web.xml文件,把默认的字符集设置成gbk/gb2312就可以了,如:

jsp-config

jsp-property-group

descriptionSpecial property group for JSP Configuration JSP example./description

display-nameJSPConfiguration/display-name

nbs

文章出处:DIY部落()

我的jsp程序出现乱码了,大家知道为什么?谢谢了

会出现这种情况 一般是你新建了一个JSP页面没有先把pageEncoding改为gbk或gb2312

而是在写了代码(有中文)后才来改成GBK保存

pageEncoding="GBK"

如果想不要乱 建完一个页面的第一件事就是设pageEncoding="GBK"并保存

再去写别的代码

关于jsp被tomcat编译后的网页出现汉字乱码。

%@ page contentType="text/html; charset=UTF-8" language="java" %

然后用记事本打开该jsp文件,另存为,选择编码为UTF-8。

jsp页面中文乱码,怎么解决

刚开始学习jsp的程序员都会遇到这样一个问题,就是网页上的中文无法显示.总结了以下几条方法。

1、在jsp页中加入一条语句:

%@ page contentType="text/html;charset=gb2312" %中文显示就正常了。

2、对于从网页中的文本框通过String parameter = request.getParameter(“parameter”);方式获得的字符串,均是8859_1的编码,

如果想把它显示在网页上就必须得用parameter = new String(parameter.getBytes(“8859_1”),”gb2312”)进行转换,windows和linux这两种系统都是一样的。

有一个简单方法,就是在 getParameter() 方法获取参数之前,使用request.setCharacterEncoding("GB2312");,将提交的信息转化为 GB2312 编码。

3、但是对于将数据写入数据库的问题,采取的方式就不同了:

windows下,必须要采用该字符串转换再插入数据库,而linux下就不需要,而是直接把8859_1编码的字符插入。

如果从数据库中读出的数据,对于windows因为在插入时已经做了转换,所以读出时已经是gb2312的,当把它显示在网页上时,不需要做编码转换,而 linux上的mysql中的数据是8859_1的所以要做编码的转换。

4、 如果你给某个字符串变量赋予一个中文字符串的值,那么在你取出他时,在网页上的显示是不需要做字符转换的,

但是如果你要在linux下做字符的比较,则还需要把字符做parameter = new String(parameter.getBytes(“gb2312”),”8859_1”)的转换。

5、长江电力网站解决中文问题的方法是:

1)在catalina.sh文件中的相关位置添加下面一行

-Dfile.encoding=GBK

2)在每个jsp文件的最前面添加下面两行

%@ page contentType="text/html; charset=GBK" %

%request.setCharacterEncoding("GBK");%

jsp中经常出现乱码,怎么解决

1.最简单的把顶部改为:%@ page language="java" import="java.util.*" pageEncoding="gbk"%

2.使用Filter:

在工具包中新建一个Filter:EncodingFilter类

代码如下:

public class EncodingFilter implements Filter {

public void destroy() {

// TODO Auto-generated method stub

}

public void doFilter(ServletRequest req, ServletResponse resp,

FilterChain chain) throws IOException, ServletException {

req.setCharacterEncoding("gbk");

resp.setCharacterEncoding("gbk");

chain.doFilter(req,resp);

}

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub

}

}

在web.xml中配置如下:

filter

filter-nameEncoding/filter-name

filter-classcom.ibm.common.EncodingFilter/filter-class

/filter

filter-mapping

filter-nameEncoding/filter-name

url-pattern/*/url-pattern

/filter-mapping

怎么解决jsp中文乱码问题,我要疯了

1、JSP页面出现的中英文乱码:

我们的PageCharset.jsp页面代码如下所示:

[html] view plain copy

%@ page language="java" import="java.util.*"%

html

head

title中文显示示例/title

/head

body

中文显示的示例。

%

out.print("这里是用jsp输出的中文");

%

/body

/html

当我们在保存我们的文件的时候会出现下面的提示:

整因为在我们的MyEclipse中默认的编码为ISO-8859-1,而ISO-8859-1不支持中文的编码,所以jsp页面代码如果出现中文就不能保存了。对于这样的错误,我们只要在页面上加上支持中文的编码格式就可以了,在jsp页面中加上pageEncoding=“gb2132” 支持中页面的编码格式就可以了。这样我们就能正常保存我们的jsp源文件了。

2、URL传递参数中文乱码

[html] view plain copy

%@ page language="java" import="java.util.*" pageEncoding="gb2312"%

html

head

titleURL传递参数中英文处理示例/title

/head

%

String param = request.getParameter("param");

%

body

a href="URLCharset.jsp?param='中文'"请单击这个链接/a

您提交的这个参数为:%=param %

/body

/html

启动tomcat运行结果出现url传递的中文乱码:

这里我们需要配置tomcat服务器文件,才能解决这个问题。具体方法是,在tomcat的conf目录下找到server.xml配置文件,找到如下代码

[html] view plain copy

span style="font-size:18px"  Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"  //span

在后面添加上编码方式,URIEncoding="gb2312" 重新启动tomcat问题就解决了。

3、表单提交中问乱码

对于表单中提交的数据,可以用request.getPraramter("");方法来获取,但是当表单中出现中文数据的时候就会出现乱码。

我们的提交表单的页面,FormCharset.jsp页面如下:

[html] view plain copy

%@ page language="java" contentType="text/html; charset=GB18030"

pageEncoding="GB18030"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=GB18030"

titleForm中文处理示例/title

/head

body

下面是表单内容:

form action="AcceptFormCharset.jsp" method="post"

用户名:input type="text" name="userName" size="10" /

密    码:input type="password" name="password" size="10"/

input type="submit" value="提交"/

/form

/body

/html

我们的AcceptFormCharset.jsp页面:

[html] view plain copy

%@ page language="java" contentType="text/html; charset=GB18030"

pageEncoding="GB18030"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=GB18030"

titleForm中文处理示例/title

/head

body

下面是表单提交以后request取到的表单的数据:br

%

out.println("表单输入的userName值为:" + request.getParameter("userName") +"br");

out.println("表单输入的pasword值为:" +request.getParameter("password") + "br");

%

/body

/html

提交表单:

结果如下:

我们看到我们在表单中的中文数据出现了乱码,为什么楚翔这种情况呢?是因为我们的tomcat中,对于以post方式提交的表单编码格式默认为ISO-8859-1的编码格式,而这种编码格式是不能编码中文的,所以就会出现乱码的现象了。对于这种情况,我们可以对表单中的数据进行处理,在取得表单参数的时候设置编码方式,我们更改了我们的接受表单数据的页面如下所示:

[html] view plain copy

%

String userName = request.getParameter("userName");

String passWord = request.getParameter("password");

out.println("表单输入的userName值为:" +  new String(userName.getBytes("ISO-8859-1"),"gb2312")+"br");

out.println("表单输入的pasword值为:" + new String(passWord.getBytes("ISO-8859-1"),"gb2312")+"br");

%

这样就得到我们想要的效果啦:

4、数据库操作中文乱码

我们在建立数据库的时候,最好是能选择支持中文编码格式,最好是能和jsp页面编码格式保持一致,这样就尽可能的减少数据库操作中文乱码的问题,最开始的连接数据库的时候,编写好数据库的编码策略,也就是使用这种形式的URL:jdbc:Oracle:thin:@localhost:1521:TEST;userEnicode=true;characterEncoding=gb2312; 这样我们选择的数据库的编码和我们的jsp编码就一致了。

写入到数据库的时候,数据库中中文乱码:

但是如果我们在最开始的时候没有对数据库的编码进行设置,并且我们的数据库中已经有大量的数据的话,我们再向数据库中写入中文数据,数据库中的中文显示为乱码。在写入数据的时候出现乱码,是因为我们在处理表单的时候没有对字符的编码设置,对于这种情况,我们在jsp中或servlet中加入:

rquest.setCharacterEncoding("gb2312");//处理表单请求的时候设置编码。

这样再看我们的数据库,插入的中文字段就不会乱码了。

从数据库中读出中文乱码:

数据库中的中文出现乱码,就是在读取数据库的时候进行转码,这样显示就不会乱码了。我们整理这样的一个转码函数:

public String encoder(String str) throws UnsupportedEncodingException

{

String result = new String(str.getBytes("ISO-ISO-8859-1)"),"gb2312");

}

5 在myeclipse开发工具中打开中文显示乱码

在myeclipse中默认的编码方式为ISO-8859-1,所以打开有其他编译器编码的jsp页面就会出现乱码,是因为两个编译器保存源文件编码格式不同,在UltralEdit可以支持中文,但是在Eclipse对jsp文件的保存方式为ISO-8895-1,这种编码不支持中文,所以就会出现乱码。

对于这种情况,我们可以更改myeclipse默认的编码方案,myeclipse-Window-Preferences-General-Content types-Test-JSP

这样问题就解决啦!

6 Filter批量设置编码格式

我们对于每一个jsp或servlet我们都要设置编码格式,效率有些低,我们的servlet的Filter解决了我们的问题。在前篇写过Filter的解决中文乱码问题的文章,在这里就不赘述了。

需要强调的一点,开始使用Java model1模型的时候,我们在web.xml中配置只需要配置好jsp页面就可以了,在model2模型中我们使用servlet作为控制器,我们就需要在Filter的配置文件web.xml中配置好servlet的设置,对所有的servlet处理的表单编码进行设置。

[html] view plain copy

filter

filter-nameCharsetEncodingFilter/filter-name

filter-classcom.bjpowernode.drp.util.filter.CharsetEncodingFilter/filter-class

init-param

param-nameencoding/param-name

param-valueGBK/param-value

/init-param

/filter

filter-mapping

filter-nameCharsetEncodingFilter/filter-name

url-pattern*.jsp/url-pattern

/filter-mapping

filter-mapping

filter-nameCharsetEncodingFilter/filter-name

url-pattern/servlet/*/url-pattern

/filter-mapping

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。