struts中的乱码问题?!

lyo 2003-10-17 10:55:03
我发现如果表单中用了ActionForm,在表单中输入中文,那麽在Action中得到的数据就是"????"。为什麽从ActionForm中得到的数据是乱码?怎样转换格式才能得到中文?
...全文
53 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
caodavid2003 2003-10-20
  • 打赏
  • 举报
回复
用第三种方法就可以解决了!你去试试!
yujinping 2003-10-20
  • 打赏
  • 举报
回复
用一个Servlet Filter 来进行转换,将所有的iso8859-1编码转化成gb2312编码就可以了。
软猫克鲁 2003-10-20
  • 打赏
  • 举报
回复
收了先。
pleonheart 2003-10-18
  • 打赏
  • 举报
回复
String str = new String((form.getXxx).getBytes("iso-8859-1"),"GB2312");
kissdavid 2003-10-18
  • 打赏
  • 举报
回复
用第三种方法就可以解决了!你去试试!
buggavin 2003-10-18
  • 打赏
  • 举报
回复
把楼上的那段代码,放到actionform中试试!!
lyo 2003-10-18
  • 打赏
  • 举报
回复
up!
lyo 2003-10-17
  • 打赏
  • 举报
回复
第一种方法我试过了,不行。我不是要把它放到数据库中,我是用它来发邮件,发到我的邮箱中,我发现Action中得到的ActionForm中的数据都是乱码,有没有在ActionForm中解决的方法?多谢
Wnyu 2003-10-17
  • 打赏
  • 举报
回复
解决办法:
第一:
1:在jsp页面加入:
<%@ page contentType="text/html; charset=gb2312" %>
2:在servlet里面:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=gb2312");//这是重要的

3:上面的如果在不行就用如下的方法在数据入库前进行调用:
public static String UnicodeToChinese(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}

public static String ChineseToUnicode(String s){
try{
if(s==null ¦ ¦s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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