基于web的应用开发都会涉及到编码问题,特别是中文编码,一直是开发人员常见问题之一,也最为初学者所困扰。我们知道计算机最初是按英语单字节字符设计的,现在很多软件及系统仍然默认使用ISO8859-1编码来表示。因此,有的时候处理中文字符就会出现乱码的现象。出现乱码不只是简单的由某个软件所造成的,很可能与系统或相关软件有关联影响。
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
例如在java的web应用中采用过滤器的方式进行编码处理,将所有来自浏览器的请求(request)转换为UTF-8,因为浏览器发过来的请求包根据浏览器所在的操作系统编码,可能是各种形式编码。网上这方面的源代码很多,下面就是编码过滤器的源代码。需要配置web.xml 激活该Filter。
SetCharacterEncodingFilter源文件
package
org.kyle.web.sample;
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
...
{
/** */
/**
* 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
;
/** */
/**
* Take this filter out of service.
*/
public
void
destroy()
...
{
this
.encoding
=
null
;
this
.filterConfig
=
null
;
}
/** */
/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
*
@param
request The servlet request we are processing
*
@param
result The servlet response we are creating
*
@param
chain The filter chain we are processing
*
*
@exception
IOException if an input/output error occurs
*
@exception
ServletException if a servlet error occurs
*/
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
*
*encoding
* UTF-8
*
*/
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
;
}
/** */
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* null.
*
* The default implementation unconditionally returns the value configured
* by the encoding initialization parameter for this
* filter.
*
*
@param
request The servlet request we are processing
*/
protected
String selectEncoding(ServletRequest request)
...
{
return
(
this
.encoding);
}
}
*
The
default
implementation unconditionally returns the value configured
*
by the encoding initialization parameter
for
this
*
filter.
*
*
@param request The servlet request we are processing
*/
protected
String selectEncoding(ServletRequest request)
...
{
return
(
this
.encoding);
}
}
|
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<display-name>setCharacterEncodingFilter</display-name>
<description>setCharacterEncodingFilter</description>
<filter-class>org.kyle.web.sample.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
|
|
<context-param>
<param-name>weblogic.httpd.inputCharset./*</param-name>
<param-value>utf-8</param-value>
</context-param>
|
default-character-set=utf-8
通过java程序操作数据库的时候,连接数据库的URL可以写成:
jdbc:mysql://localhost/test?useUnicode=true;characterEncoding=utf-8
Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=1382775
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
// 測試中文轉碼
String x="中文訊息";
//System.out.printf("%s:/t%s/n", "String", x);
System.out.println("%s:/t%s/n" + x);
System.out.println("BIG5:/t");
byte y[]=x.getBytes("big5");
System.out.println("start y");
for (int i=0; i<y.length; i++) {
//System.out.printf("%x ", y[i]);
System.out.println("%x "+ y[i]);
}
System.out.println("end y");
System.out.println(y);
System.out.println("UTF-8:/t");
byte z[]=x.getBytes("utf-8");
System.out.println("start z");
for (int i=0; i<z.length; i++) {
//System.out.println("%x ", z[i]);
System.out.println("%x "+ z[i]);
}
System.out.println("end z");
System.out.println(z);
String v=new String(y);
System.out.println("BIG5:/t".concat(v));
String w=new String(z);
System.out.println("UTF-8:/t".concat(w));
} catch (java.io.UnsupportedEncodingException e) {
System.out.println("Error: UnsupportedEncodingException - ".concat(e.getMessage()));
System.exit(1);
}
}
}
解决Web应用中的乱码问题
本文详细介绍了如何解决Web应用开发中常见的乱码问题,包括统一开发环境、浏览器、中间件和数据库的字符编码,重点讲解了UTF-8编码的应用。
5884

被折叠的 条评论
为什么被折叠?



