web.xml<!-- 配置Struts 2的核心过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 自定义字符集过滤器 -->
<filter>
<filter-name>filter</filter-name>
<filter-class>cn.jbit.accp.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
--------------------------------------------------------------------------------------------------------
自定义的过滤器SetCharacterEncodingFilter.javapackage cn.jbit.accp.filter;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; /**
* Take this filter out of service.
*/
@Override
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
*/
@Override
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);
} @Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("run filter class init!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
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
* <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); }}
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 自定义字符集过滤器 -->
<filter>
<filter-name>filter</filter-name>
<filter-class>cn.jbit.accp.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
--------------------------------------------------------------------------------------------------------
自定义的过滤器SetCharacterEncodingFilter.javapackage cn.jbit.accp.filter;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; /**
* Take this filter out of service.
*/
@Override
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
*/
@Override
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);
} @Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("run filter class init!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
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
* <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); }}
解决方案 »
- 自学JSP麻烦各位推荐比较好的视频教程
- MyEclipse 启动后自动停止在 Executors.class内
- Java生成静态页面
- 在Tomcat中运行Struts+Hibernate的一个错误
- 启动Tomcat的时候报错!请高手帮忙!报错信息见正文!
- Java Applet程序怎样发布到WEB上,并且不需要下载控件?
- 请问我新建了一个testjsp的虚拟文件夹,beans和servlet应该放在什么地方?
- 随机数字问题?
- applet接收来自Javabean的数据怎么办,各位老兄老弟请帮忙出个点子!我先谢谢了!!!
- xxljob调度失败
- 对于运行在tomcat下的java项目进行加密 class文件不可以反编译 有什么新技术可以实现?
- org.springframework.beans.factory.CannotLoadBeanClassException:
import="java.lang.*,cn.jbit.accp.constants.*"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">----------------------------------------------------------------------------
这个是数据访问层:public TAccountDetail saveClaimVoucherDetail(TAccountDetail detail) {
System.out.println("持久化层观察中文是否为乱码:"+detail.getItem());
System.out.println("持久化层观察中文是否为乱码:"+detail.getDes());
this.getHibernateTemplate().saveOrUpdate(detail);
this.getHibernateTemplate().flush();
return detail;
}从页面输入中文值后还没执行插入数据库操作之前就变成了乱码
----------------------------------------------------------------------------------
window-perferences-workspace里设置的Text file encoding是默认的GBK在tomcat的安装目录里设置的server.xml里: <Connector port="8181" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="GBK"/>
--查询Oracle数据库的编码
select * from nls_database_parameters where parameter ='NLS_CHARACTERSET';
查询出的表是:
parameter value
NLS_CHARACTERSET ZHS16GBK--查看Oracle客户端编码
select * from nls_instance_parameters where parameter='NLS_LANGUAGE';
查询出的表是:
parameter value
NLS_LANGUAGE AMERICAN