你可以加一个监听器
类:
package com.bean;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 FilterEncoding implements Filter {
private FilterConfig filterConfig=null;
private String encoding=null; /* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
this.encoding=filterConfig.getInitParameter("encoding");
} /* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String encoding=this.encoding;
if(encoding!=null)
{
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
} /* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
this.encoding=null;
this.filterConfig=null;
}}
然后在web.xml配置<filter>
<filter-name>setEncoding</filter-name>
<filter-class>
com.bean.FilterEncoding
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
类:
package com.bean;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 FilterEncoding implements Filter {
private FilterConfig filterConfig=null;
private String encoding=null; /* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig=filterConfig;
this.encoding=filterConfig.getInitParameter("encoding");
} /* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String encoding=this.encoding;
if(encoding!=null)
{
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
} /* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
this.encoding=null;
this.filterConfig=null;
}}
然后在web.xml配置<filter>
<filter-name>setEncoding</filter-name>
<filter-class>
com.bean.FilterEncoding
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setEncoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
解决方案 »
- java.lang.UnsatisfiedLinkError这个错误怎么解决
- 关于前台传值,Action获值自动转换的问题,Pojo.......
- 关于servlet API下载
- Tomcat+Jsp+sql server 2000配置,访问数据库是出现的问题?
- 求马士兵老师的讲义
- Help : 我一个jsp页面,做一个下载快捷方式文件的方法,提示窗口,文件名是乱码,
- 初学者问题,高手进来看看吧
- 运行soap错误,帮我看一下NorwayWood()
- 在jboss中如何配置mysql数据库?
- 如何根据schema和数据库数据生成相应的XML?
- 一个关于struts radio的问题,想大家请教!!如果分不够就加!!!!
- 浮点数1.344用16进制表示是多少?
或者加个RequestProcessor
import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.RequestProcessor;public class MyRequestProcessor extends RequestProcessor { @Override
protected boolean processPreprocess(HttpServletRequest arg0, HttpServletResponse arg1) {
try {
arg0.setCharacterEncoding("utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.processPreprocess(arg0, arg1);
}
}
在struts-config.xml中还需要配置<controller processorClass="MyRequestProcessor"/>
package com.bean;import javax.servlet.*;
import java.io.*;public class EncodingFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws ServletException, IOException {
request.setCharacterEncoding("GBK"); chain.doFilter(request, response);
response.setCharacterEncoding("GBK"); } public void destroy() {
// TODO Auto-generated method stub
}}在web.xml中配置
.........
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.bean.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<servlet-name>*.jsp</servlet-name>
</filter-mapping>
........针对与struts用!
添加过滤器也是很不错的
也可以在server.xml设置编码
1种是写过滤器。
另外一种,就是在你的Tomcat下conf中的server.xml里面端口号为8080的<Connector>中,添加设置,也同样可以。
如果url含有中文请先编码:url = encodeURI(url);
后台取出数据 String str= new String(request.getParameter("str").getBytes("ISO-8859-1"),"UTF-8");
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.RequestProcessor;public class MyRequestProcessor extends RequestProcessor { protected boolean processPreprocess(HttpServletRequest arg0, HttpServletResponse arg1) {
// TODO Auto-generated method stub
try {
arg0.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
arg1.setCharacterEncoding("UTF-8");
return super.processPreprocess(arg0, arg1);
}}然后在struts-config.xml里配置
<controller
processorClass="com.mellow.cnyf.filters.MyRequestProcessor">
</controller>这个节点就可以了
还有一个快捷的方法就是 String data2 = new String(data1.getBytes("ISO8859-1"),"GBK");将data1接收到的数据使用gbk的字符编码集来保存到data2中
public static String Code(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}catch(Exception e){
return null;
}
}
%>
用这个方法处理一下String类型的就可以了!
String name = Code(request.(getParameter("Name"));
String phone = Code(request.getParameter("Phone"));
set DES_FILE=ApplicationResources_zh_cn.propertiesif exist "%DES_FILE%" del %DES_FILE%
native2ascii -encoding GBK %SRC_FILE% %DES_FILE%
方法2。在取得传递参数过来之前使用request.setCharacterEncoding("UTF-8");
页面编码
ide编码
request--过滤器
response--tomcat server。xml文件
都要统一