jsp页面使用utf-8编码,网页中文显示乱码,怎么办

jsp页面使用utf-8编码,网页中文显示乱码,怎么办,第1张

乱码一般有三个地方导致: 一个是开发环境,可

在MyEclipse中window -> preference,在d出的对话框中点击MyEclise--->Files and Editors--->JSP选择Encoding 为 UTF-8即可,其他的文件设置编码格式也是在此目录下,方法一样

一个是tomcat 还一个是数据库

转码可以在页面中或是sevlet以及过滤都可以解决的,下面以页面和servlet为例:

方法一:设置

request

response

的编码

[

页面编码必须为

u8

]

requestsetCharacterEncoding("UTF-8");//传值编码

responsesetContentType("text/html;charset=UTF-8");//设置传输编码

方法二:

String

str1=传来的数据。

String

ss=new

String(str1getBytes("ISO-8859-1"),"utf-8");

//转码UTF8

以下,是中文乱码的各种解决方法,要记住了,很有用,肯定有一种方法能帮你解决的。希望能帮到你。

URIENcoding 设成GBK或GB2312

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

String S=new String(rsgetString("news")getBytes("gb2312"),"ISO8859_1");

//rsgetString("news")为待转换的字符串

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

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

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

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

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

String S=new String(rsgetString("news")getBytes("gb2312"),"ISO8859_1");

//读的时候在转换回来

String S=new String(rsgetString("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方法),使用requestgetParameter方法得到乱码,这是因为tomcat处理提交的参数时默认的是iso-8859-1,表单提交get和post处理乱码问题不同,下面分别说明。

(1)POST处理

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

package cngovbeijingitutil;

import javaioIOException;

import javaxservletFilter;

import javaxservletFilterChain;

import javaxservletFilterConfig;

import javaxservletServletException;

import javaxservletServletRequest;

import javaxservletServletResponse;

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() {

thisencoding = null;

thisfilterConfig = 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 || (requestgetCharacterEncoding() == null)) {

String encoding = selectEncoding(request);

if (encoding != null) {

requestsetCharacterEncoding(encoding);

}

}

// Pass control on to the next filter

chaindoFilter(request, response);

}

/

Place this filter into service

@param filterConfig

The filter configuration object

/

public void init(FilterConfig filterConfig) throws ServletException {

thisfilterConfig = filterConfig;

thisencoding = filterConfiggetInitParameter("encoding");

String value = filterConfiggetInitParameter("ignore");

if (value == null) {

thisignore = true;

} else if (valueequalsIgnoreCase("true")) {

thisignore = true;

} else if (valueequalsIgnoreCase("yes")) {

thisignore = true;

} else {

thisignore = false;

}

}

protected String selectEncoding(ServletRequest request) {

return (thisencoding);

}

}

webxml文件加入过滤器

<filter>

<filter-name>Encoding</filter-name>

<filter-class>

cngovbeijingitutilSetCharacterEncodingFilter

</filter-class>

<init-param>

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

<param-value>gbk</param-value>

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

</init-param>

<init-param>

<param-name>ignore</param-name>

<param-value>true</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>Encoding</filter-name>

<servlet-name>/</servlet-name>

</filter-mapping>

注意filter元素要放在所有webxml元素之前。

(2) Get方法的处理

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

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

<Connector port="8080" max>

首先保证编码一至,jsp用的都是是gbk

当为post 方法提交

首行 应该用 requestsetCharacterEncoding("GBK");

当为get 方法提交

如变量name 用

String name = new String(requestgetParameter("name")getBytes("ISO-8859-1"), "GBK");

我本军团为你解答

1第一个地方的编码格式为jsp文件的存储格式。Eclipse会根据这个编码格式保存文件。并编译jsp文件,包括里面的汉字。 第二处编码为解码格式。因为存为UTF-8的文件被解码为iso8859-1,这样如有中文肯定出乱码。也就是必须一致。而第二处所在的这

楼主,给你两个页面测试一下是不是你要的效果:

ajsp

<%@ page language="java" import="javautil" pageEncoding="utf-8"%>

<%

String path = requestgetContextPath();

String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 401 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>这是a页面</title>

</head>

<body>

<a href="bjspcity=南京">bjsp</a>

</body>

</html>

bjsp

<%@ page language="java" import="javautil" pageEncoding="utf-8"%>

<%

String path = requestgetContextPath();

String basePath = requestgetScheme()+"://"+requestgetServerName()+":"+requestgetServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 401 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>这是b页面</title>

</head>

<body>

<%

String cityName = new String(requestgetParameter("city")getBytes("ISO-8859-1"),"utf-8");

outprintln("城市名称(中文):"+cityName);

%>

</body>

</html>

这里最关键的就是:

String cityName = new String(requestgetParameter("city")getBytes("ISO-8859-1"),"utf-8");

有问题找我,我一直在线

汉字编码问题。以下是我从网上搜索到的相关汉字编码问题的解决方案,希望能帮到你。1 表单提交的数据,用requestgetParameter(“xxx”)返回的字符串为乱码或者??

2 直接通过url如>

以上就是关于jsp页面使用utf-8编码,网页中文显示乱码,怎么办全部的内容,包括:jsp页面使用utf-8编码,网页中文显示乱码,怎么办、jsp传递中文参数出现乱码、jsp高手请进,我用jspsmartupload上传文件,但我表单获得的中文是乱码(UTF-8)的,如何解决等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9715974.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存