再线等,网上找了2天,仍然没解决的乱码问题,快疯了
我使用的是struts
我在tomcat的server.xml中加了URIEncoding= "UTF-8 "
在web.xml中写了过滤器,过滤器中加了
request.setCharacterEncoding( "UTF-8 ");
都没能解决。
在FormAction.java中的set方法中直接出来乱码
public void setGo_timehm(String go_timehm) {
this.go_timehm = go_timehm
}
也就是说,从页面过来的值已经是乱码了。
于是我添加了这个方法来转码
private String toGBK(String str) throws java.io.UnsupportedEncodingException
{
return new String(str.getBytes( "ISO-8859-1 "), "GBK ");
}
但是在set方法中引用的时候却报错
public void setGo_timehm(String go_timehm) {
this.go_timehm = new String( "123 ".getBytes( "ISO-8859-1 "), "GBK ");
}
unhandled exception type unsupportedEncodingException这个错误
小弟百思不得其解,现在我快疯了,大哥们救救我把
[解决办法]
大哥,方法throws异常了,下面得catch处理下把,错误报的很清楚
public void setGo_timehm(String go_timehm) throws UnsupportedEncodingException{
this.go_timehm = this.toGBK(go_timehm);
}
[解决办法]
sturts.cfg.xml下添加
<controller processorClass= "common.Trans " />
添加类
package common;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;
public class Trans extends RequestProcessor{
public Trans(){}
protected boolean processPreprocess( HttpServletRequest request,
HttpServletResponse response ){
try{
request.setCharacterEncoding( "UTF-8 ");
}
catch(Exception ex){
ex.printStackTrace();
}
return true;
}
}
试下
[解决办法]
启动NetBeans
打开原来的SimpleWebSite_web项目,将所依赖的SimpleWebSite_dao与SimpleWebSite_service两个项目同时打开
右键点SimpleWebSite_web项目的Source Packages
New
File/Folder...
选“Web”,File Type为Filter
Next
Class Name:EncodingFilter
Package:org.liupopo.simplewebsite.web.filter
Next
要选上“Add information to deployment descriptor (web)”, 这样NetBeans会自动在web.xml中配置好相应的Filter设置
Next
在这里我们可以设置Filter要用到参数,因为我想可以通过修改web.xml中的参数改变网站的编码,所以“New”,name为“Encode”value为“UTF-8”
Finish
看看NetBeans为我们做了什么
1.在web.xml增加了以下Filter的内容:
<filter>
<filter-name> EncodingFilter </filter-name>
<filter-class> org.liupopo.simplewebsite.web.filter.EncodingFilter </filter-class>
<init-param>
<param-name> Encode </param-name>
<param-value> UTF-8 </param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name> EncodingFilter </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
省了我们自己手动配置web.xml了。而且NetBeans对web.xml的编辑提供了可视化的操作界面,对web.xml中的各项设置进行了分类,非常方便我们的修改。
2.为我们生成了EncodingFilter这个类,刚打开的时候吓我一跳,一个Filter怎么这么多代码?原来是NetBeans为我们把一些常用的方法已经写好,我们只要在相应的位置加上我们的做具体事情的代码就可以了。NetBeans为我们加的doBeforeProcessing()和doAfterProcessing()两个方法是想我们在Filter之前和之后做些什么,生成的代码里已经做了日志处理,并且还有一大段被注释起来的示例代码,但我平时都是在init()里做一些初始化的处理,这两个方法显得有些罗嗦了,并且对于log,我回头要统一处理,所以全部去掉。
最后的EncodingFilter内容为:
package org.liupopo.simplewebsite.web.filter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
*
* @author liupopo
* @version 1.0
*/
public class EncodingFilter implements Filter {
// The filter configuration object we are associated with. If
// this value is null, this filter instance is not currently
// configured.
private FilterConfig filterConfig = null;
private String encode = null;
public EncodingFilter() {
}
/**
*
* @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 {
request.setCharacterEncoding(encode);
try {
chain.doFilter(request, response);
} catch(Throwable t) {
t.printStackTrace();
}
}
/**
* Return the filter configuration object for this filter.
*/
public FilterConfig getFilterConfig() {
return (this.filterConfig);
}
/**
* Set the filter configuration object for this filter.
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
/**
* Destroy method for this filter
*
*/
public void destroy() {
}
/**
* Init method for this filter
*
*/
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
if (filterConfig != null) {
this.encode = this.filterConfig.getInitParameter( "Encode ");
} else {
this.encode = "UTF-8 ";
}
}
}
好了,Clean and Build Project,
Run Project
输入中文进行注册,可以正常的保存与显示中文啦。
每次启动这个系统之前都要记得启动数据库Server,有时觉得挺烦的,本打算把HsqlDb的Server模式改为In-Process (Standalone) Mode,但数据库文件的路径又成问题,暂时还是先麻烦点吧。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=638078
NetBeans5.0中文版JSP中文问题终于解决 这些天用NetBeans5.0中文版编了一个项目,感觉确实不错,但解决JSP中文问题让我头疼一下午,现终于解决。
使用默认的NetBeans设置,所有JSP网页默认编码均为UTF-8 ,这样浏览器发送参数也是基于UTF-8的,所以用Servlet处理的时候会出现乱码问题。
按以前的方法,编码为“ISO-8859-1 ",无效,不但这个无效,其它编码如”GBK“,”UTF-8 "等,均无效。
后来偶然注意到NetBeans是用UTF-8编码JSP文件的(以前用DW)设计JSP,默认为GB2312 ,随即将其换为GB2312一试,但换编码无异于将网页重写一遍(幸好我只是做了个大概,还没有做美工设计,要不就惨了!),
因为所能的字符全变为乱码,将UTF-8编码的JSP文件复制到DW里,仍然是UTF-8 ,只好在DW里新建一个编码为GB2312的文件,将网页重写一遍。
写完后,再用NetBeans打开,这样文件就成为GB2312编码了,运行一试,果然不再乱码!
所以以后再用NetBeans编写JSP,一定要先将编码设置为GB2312,然后再写网页,我是忘 不了了,呵呵。
[解决办法]
试试看我的吧
我也刚刚做完基于struts得系统
乱码问题的解决 论文哦 呵呵
5.8.1 显示及输入为汉字
使页面显示为汉字,只需把页面的字符集设定为charset=“gb2312”和pageEncoding= "gb2312 "即可。
把输入转为汉字,需要在元素所在的form中添加enctype= "multipart/form-data ",并在将输入更新到数据库前,作如下转换
exp.getExp_cont().getBytes( "gb2312 ").toString();
即把输入内容转换为编码为gb2312的字节,然后再重新转换成字符串。这样就做到了显示在页面上及在数据库中都为汉字。
5.8.2 错误信息的中文显示
由于异常及错误信息都定义在Application.properties文件中,必须先将其为转换为unicode编码,否则在页面显示为乱码。
转换时用native2ascii命令,格式为:
native2ascii[encodinggbk]application.properties application_cn.properties