设置一个过滤器,
web.xml加
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>com.hotpepper.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter><filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>public class SetCharacterEncodingFilter
implements Filter { // ----------------------------------------------------- Instance Variables
/**
* 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; // --------------------------------------------------------- Public Methods
/**
* 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 {
String encoding = selectEncoding(request); if (encoding != null) {
request.setCharacterEncoding(encoding);
} else {
request.setCharacterEncoding(encoding);
}
response.setContentType("text/html;charset=UTF-8");
// 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"); } // ------------------------------------------------------ Protected Methods
/**
* 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
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) { return (this.encoding); }}
web.xml加
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>com.hotpepper.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter><filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>public class SetCharacterEncodingFilter
implements Filter { // ----------------------------------------------------- Instance Variables
/**
* 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; // --------------------------------------------------------- Public Methods
/**
* 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 {
String encoding = selectEncoding(request); if (encoding != null) {
request.setCharacterEncoding(encoding);
} else {
request.setCharacterEncoding(encoding);
}
response.setContentType("text/html;charset=UTF-8");
// 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"); } // ------------------------------------------------------ Protected Methods
/**
* 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
* <code>null</code>.
* <p>
* The default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) { return (this.encoding); }}
解决方案 »
- struts2 ajax 问题
- java反编译高手快进来救我啊!!!
- 级联列表框,的程序,谁有啊?
- 有关jsp中怎样在提交之后对所选的单选按钮计数
- 贴一段关于写excel的代码
- 要哭啦,请问我用JspSmartupload把文件上传到了服务器后,如何把它加入数据库?
- 超链接如何传递多个参数,并且在另一页面把传递过来的参数得到???
- How deal With JavaBean ? First Use JavaBean Thanks
- org.springframework.beans.factory.BeanCreationException:
- ajax如何传递数组到后台,后台在action中如何去接收
- lucene实现建立数据库索引并实现检索的问题,我很着急望各位指教
- session.createSQLQuery为什么不能更新数据库???急
UTF-8?是在哪里改?是在页面还是在配置文件里改??
谢谢各们啊!
GB2312是GBK的子集,GBK是GB18030的子集,GBK是包括中日韩字符的大字符集合,如果是中文的网站 推荐GB2312, GBK有时还是有点问题.为了避免所有乱码问题,应该采用UTF-8,将来要支持国际化也非常方便 ,UTF-8可以看作是大字符集,它包含了大部分文字的编码。
使用UTF-8的一个好处是其他地区的用户(如香港台湾)无需安装简体中文支持就能正常观看你的文字而不会出现乱码
jdbc:mysql://127.0.0.1:3306/NK_DB?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true
其中NK_DB是数据库名称,useUnicode表示是使用自定义编码,utf-8是编码方式,一般中文建议使用UTF-8,用不是gbk或gb2312,GBK和gb2312都为中文简体编码
而且要分你是get方式传递参数,还是post方式。
<filter>
<filter-name>TestFilter</filter-name>
<filter-class>org.net.TestFilter.TestFilter</filter-class> <init-param>
<param-name>config</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>TestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
过滤器中的代码:
package org.net.TestFilter;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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;public class TestFilter implements Filter { private FilterConfig config;
private String econding="GBK";
public void destroy() {
} public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
if(config!=null){
econding=config.getInitParameter("config");
}
request.setCharacterEncoding(econding);
response.setCharacterEncoding(econding);
chain.doFilter(request, response);
} public void init(FilterConfig config) throws ServletException {
this.config=config;
}}
2.解决程序中的乱码问题,可以编写过滤器,而spring已经给我们提供了,所以我们无需自己定义及
<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter>
<filter-name>TestFilter</filter-name>
<filter-class>org.net.TestFilter.TestFilter</filter-class> <init-param>
<param-name>config</param-name>
<param-va……
[/Quote] 还有该成UTF-8