编码转化了,还是乱码,大家帮帮忙,在线等待!

599899 2004-10-30 10:56:35
jsp页面里传过去一个值例如:<html:link page="/XmList.do?xmLx=基建项目库">,在控制台输出request编码是gb2312,但是得到的值还是乱码,为什么?

大家帮帮忙,小弟谢过了~~~很郁闷~
相关文件如下(编译成功):

filter文件:
-------------------------------------
package 包;

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 {

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)
System.out.println("filter2 is doing !");
System.out.println("encoding:"+encoding);
System.out.println("ignore:"+ignore);
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文件:
-----------------------------------------------------------
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>包</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GB2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
--------------------------------------------------------
--------------------------------------------------------


action文件:
--------------------------------------------------------
package 包.Actions;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import 包.GetXmList;
import 包.PageForm;

public class XmListAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
PageForm f = (PageForm)form;


//Get the parameter from Form
String xmLx = f.getXmLx();

//调试
System.out.println("request CharacterEncoding :" + request.getCharacterEncoding());
System.out.println("response CharacterEncoding :" + response.getCharacterEncoding());
System.out.println("xmLx in action is:" + xmLx);
//调试结束


int countPerPage = f.getCountPerPage();
int page = f.getPage();

if(page <= 0 ) f.setPage(1);

GetXmList gal = new GetXmList();

if(xmLx == null || xmLx.equals("") || xmLx.equals("*"))
{
gal.getList("*",countPerPage,page);
}
else
{
gal.getList(xmLx,countPerPage,page);
}

if(page > gal.getPageTotal())
{
f.setPage(gal.getPageTotal());
}

request.setAttribute("coll",gal.getColl());
request.setAttribute("xmTotal",String.valueOf(gal.getXmTotal()));
request.setAttribute("pageTotal",String.valueOf(gal.getPageTotal()));
request.setAttribute("page",String.valueOf(gal.getPage())); //获得当前页
request.setAttribute("pageForm",f);
return mapping.findForward("showXmList");

}
}
---------------------------------------------------------------------
---------------------------------------------------------------------
PageForm文件:
-------------------------------------------------------------
package 包.FormBeans;

import javax.servlet.http.*;

import org.apache.struts.action.*;
import java.util.HashMap;

public class PageForm
extends ActionForm
{
private String xmLx = "*";
private int countPerPage = 20;
private int page = 1;

private HashMap nextLinkMap = new HashMap(); //该变量用于存放上下页链接所用的Map,下一页
private HashMap lastLinkMap = new HashMap(); //该变量用于存放上下页链接所用的Map,上一页

//上一页
public HashMap getLastLinkMap()
{
this.lastLinkMap.put("xmLx",this.xmLx);
this.lastLinkMap.put("page",String.valueOf(this.page-1));
return this.lastLinkMap;
}

//下一页
public HashMap getNextLinkMap()
{
this.nextLinkMap.put("xmLx",this.xmLx);
this.nextLinkMap.put("page",String.valueOf(this.page+1));
return this.nextLinkMap;
}

public String getXmLx()
{
return xmLx;
}

public void setXmLx(String xmLx)
{
this.xmLx = xmLx;
}

public int getCountPerPage()
{
return countPerPage;
}

public void setCountPerPage(int countPerPage)
{
this.countPerPage = countPerPage;
}

public int getPage()
{
return page;
}

public void setPage(int page)
{
this.page = page;
}

public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{
/**@todo: finish this method, this is just the skeleton.*/
return null;
}

public void reset(ActionMapping actionMapping,
HttpServletRequest httpServletRequest)
{
xmLx = "*";
countPerPage = 20;
page = 1;
}
}
-------------------------------------------------------
------------------------------------------------------
...全文
183 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
dafei0320 2004-10-31
  • 打赏
  • 举报
回复
xmLx= new String(xmLx.getBytes("ISO8859_1"), "GBK");
这个应该没有问题!
cold_blooded 2004-10-31
  • 打赏
  • 举报
回复
:<html:link page="/XmList.do?xmLx=基建项目库">,

中的汉字写在java代码里,
或使用 标签的属性,
pararmId,
paramName等.
simaxingyun 2004-10-31
  • 打赏
  • 举报
回复
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>action-mappings</param-name>
<param-value>servlets.actions</param-value>
</init-param>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
gong1 2004-10-31
  • 打赏
  • 举报
回复
改成自动选择
简体中文。^_^
gong1 2004-10-31
  • 打赏
  • 举报
回复
那你看一下你的ie是什么编码格式。^_^
599899 2004-10-30
  • 打赏
  • 举报
回复
我试过了,通过表单形式传过去的值都能被filter正确的转化,但是为什么,以**do?xmLx=“基建项目”这种url类型传过去的值就不行呢?
599899 2004-10-30
  • 打赏
  • 举报
回复
恩,这个好使,我以前就试过,但是,我的filter为什么不好使呢,我看乱码这方面帖子,不是说一个filter就全解决了吗?各位大哥,帮帮忙
tyong 2004-10-30
  • 打赏
  • 举报
回复
其中
String xmLx=(String)(request.getParameter("xmLx"));
tyong 2004-10-30
  • 打赏
  • 举报
回复
试试这个
xmLx= new String(xmLx.getBytes("ISO8859_1"), "GBK");
599899 2004-10-30
  • 打赏
  • 举报
回复
简短点说就是:
jsp页面里传过去一个值例如:<html:link page="/XmList.do?xmLx=基建项目库">,在action类里,得到的xmLx这个值是"??????"乱码,我用了filter在控制台输出request编码也是gb2312,但是得到的值还是乱码,为什么?并没有转化!
追求自由 2004-10-30
  • 打赏
  • 举报
回复
真长呀
599899 2004-10-30
  • 打赏
  • 举报
回复
我把tomcat 的URI Encoding改成gb2312了,这样,bean里得到的确实是中文了。

但是问题又出现了:jsp页面里的“上页、下一页”的链接,其中参数xmLx乱码了,这个参数是是从pageform这个formbean里得到的,(pageform里的xmLx这个参数是中文),jsp页面我已经设置request.setCharacterEncoding("gb2312");,可为什么还是乱码呢?

AHUA1001 2004-10-30
  • 打赏
  • 举报
回复
这个问题过去好像有人问过,你去MSDN的精华区看看,应该能找到。
HITZXL 2004-10-30
  • 打赏
  • 举报
回复
是不是还应该设置它
response.setContentType("text/html; charset=gb2312");

据觉得这个参数的数据不是用post方式发出去的,对他的字符转换他会和html页面的字符统一!
试试先

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧