|
???????JAVA?????????
1??jsp??????<%@page contentType=text/html;charset=GB2312%>
?servlet???httpServlerResponse.setContentTpye(text/html; charset=GB2312);??????????
2???JDBC??mysql???????????????????????????
jdbc://mysql://hostname:port/DBname?user=username&password=pwd&
useUnicode=true&characterEncoding= iso-8859-1
????????????????????????
<parameter>
<name>url</name>
<value>jdbc:mysql://hostname:port/DBname? &useUnicode=true&characterEncoding=iso-8859-1</value>
</parameter>
?????&??&?????XML????????????
3?????????????????????????????????
String desc = rs.getString(desc);
desc = new String(desc.getBytes(ISO-8859-1)?GB2312);
4????????????Servlet?Servlet??????????????????????
??????servlet?????????????
httpServlerRequest.setCharacterEncoding(GB2312);
5. ?struts??????????????JDK????????
>native2ascii -encoding BG2312 Myresource.properties Myresource_zh.properties
6??struts???org.apache.struts.action.RequestProcessor????????processPreprocess()??:
package com.mypro.action;
public class MyProRequestProcessor extends RequestProcessor
{
protected boolean processPreprocess (HttpServletRequest request,
HttpServletResponse response)
{
try
{
request.setCharacterEncoding(GB2312);
//other code
}
catch(Exception e){}
return true;
}
}
???????????struts-config.xml?
<controller processorClass= com.mypro.action.MyProRequestProcessor/>
7. ?filter??????
package com.kefeiannan;
import java.io.IOException;
import javax.servlet.*;
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 {
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
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,?<web-app>????
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.kefeiannan.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
[em3] [em18] |
|