采用get方式的乱码问题

一饼团队 2010-02-05 12:47:26
在分页的时候请求页面search.jsp采用post方式提交,所以在search_result.jsp中第一页可以正常显示,但是想要翻页的时候,就会出现问题。 在Action中设置要翻页的URL为:pg.setLinkUrl(request.getContextPath()+"/search.do?method=doSearch&bid="+bid+"&type="+type+"&loginId="+loginId+"&title="+title);此时前台的请求页面显示为:http://localhost:8080/bbs/search.do?method=doSearch&bid=0&type=content&loginId=&title=测试&cpage=2,此时title为中文,怎样解决这种get方式下的乱码问题,我的项目全部都是用的utf-8编码。不想修改tomcat的server.xml,也就是添加一行URIEncoding="UTF-8",要不然服务器上不要发布就郁闷了。我试过很多方法了,filter中设置request.setCharacterEncoding("utf-8"),也不可以,盼求解决方法。
...全文
275 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
SambaGao 2010-02-06
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 d_star 的回复:]
import java.io.UnsupportedEncodingException;

public class UTFUtil {

public static String toUtf(String str){

String afterChangeStr = "";
try {
byte[] bt = str.getBytes("ISO8859_1");
afterChangeStr= new String(bt,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return afterChangeStr;

}

}
[/Quote]

我常用的。
crazylaa 2010-02-06
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 seven_zhao 的回复:]
我的项目中search方法用post方法提交,然后在翻页的时候才用get方法传递参数,怎样保证此时Action获得的参数可以以中文的形式正确显示。
[/Quote]
觉得翻页也用post提交就完了。不就是把链接方式改成js的submit么?
crazylaa 2010-02-05
  • 打赏
  • 举报
回复
sorry,是获取后
crazylaa 2010-02-05
  • 打赏
  • 举报
回复
不想设置server.xml
那就set前encode,获取前decode
cyc123007512 2010-02-05
  • 打赏
  • 举报
回复
我遇到同样的问题,已经解决。请参考我发的帖子。
http://topic.csdn.net/u/20100126/18/a1c6a7d1-1d93-4b35-b14d-cd1398a1e363.html
岁月之梦 2010-02-05
  • 打赏
  • 举报
回复
过滤器 转码!
njlywy 2010-02-05
  • 打赏
  • 举报
回复
用过滤器
ty_seven 2010-02-05
  • 打赏
  • 举报
回复
刚才写错了,获取参数时:new String(title.getBytes("iso-8859-1"),"utf-8")
ty_seven 2010-02-05
  • 打赏
  • 举报
回复
获取参数时:new String(title.getBytes("iso-8859-1"),title)
wsl542416776 2010-02-05
  • 打赏
  • 举报
回复
String url = "http://...?temp="+URIencode(传的参数);
pengrong 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 liuchenyu 的回复:]
用过滤器可以吧
[/Quote]
过滤器只过滤post方法吧

跳转使用js,实现吧把的title用js内置的编码函数编下title,然后再做后台一系列的转码操作。


js:
window.location='search.do?method=doSearch&bid=0&title='+encodeURICompent('测试')

后台:

String title = new String(request.getParameter("title").getBytes("iso-8859-1"),"utf-8");



liuchenyu 2010-02-05
  • 打赏
  • 举报
回复
用过滤器可以吧
d_star 2010-02-05
  • 打赏
  • 举报
回复
import java.io.UnsupportedEncodingException;

public class UTFUtil {

public static String toUtf(String str){

String afterChangeStr = "";
try {
byte[] bt = str.getBytes("ISO8859_1");
afterChangeStr= new String(bt,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return afterChangeStr;

}

}
luhui436 2010-02-05
  • 打赏
  • 举报
回复
上传前先转码
andyxl 2010-02-05
  • 打赏
  • 举报
回复
不管获取前还是后;; 超链接上传中文就是这个样子。 毕竟这些语言不是国产的,。


可以考虑写个过滤器。
一饼团队 2010-02-05
  • 打赏
  • 举报
回复
我的项目中search方法用post方法提交,然后在翻页的时候才用get方法传递参数,怎样保证此时Action获得的参数可以以中文的形式正确显示。
huangminyanghe 2010-02-05
  • 打赏
  • 举报
回复
在传递和接受的时候都应该进行编码的设置,一个好的编码习惯会减少很多非必要的麻烦,
jisi772864447 2010-02-05
  • 打赏
  • 举报
回复
可以专门写一个方法验证是不是中文,不是中文就转化一下返回中文。

public static String ISOToUtf(String str) {
String s = "";
try {
s = new String(str.getBytes("iso-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}

public static String ISOToGBK(String str) {
String s = "";
try {
s = new String(str.getBytes("iso-8859-1"), "GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}

public static String GBKToUTF(String str) {
String s = "";
try {
s = new String(str.getBytes("GBK"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return s;
}

/**
* 判断是否汉字
*
* @param str
* @return
*/
public static String vd(String str) {

String ret = ISOToGBK(str);
char[] chars = ret.toCharArray();
boolean isGBK = false;
for (int i = 0; i < chars.length; i++) {
byte[] bytes = ("" + chars[i]).getBytes();
if (bytes.length == 2) {
int[] ints = new int[2];
ints[0] = bytes[0] & 0xff;
ints[1] = bytes[1] & 0xff;
if (ints[0] >= 0x81 && ints[0] <= 0xFE && ints[1] >= 0x40
&& ints[1] <= 0xFE) {
isGBK = true;
break;
}
}
}
if (isGBK) {
return ret;
} else {
return str;
}
}
jisi772864447 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 bleibo 的回复:]
在server.xml中的修改,加上红色部分
<Connector port="8080" maxHttpHeaderSize="8192"
              maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
              enableLookups="false" redirectPort="8443" acceptCount="100"
              connectionTimeout="20000" disableUploadTimeout="true"
             URIEncoding="gbk"

/>
[/Quote]
是有这个问题, 改成这个也可以,建议用js转码2次, 后台解码。


txq1989620 2010-02-05
  • 打赏
  • 举报
回复
添加过滤器 即可 万能的
加载更多回复(4)

67,513

社区成员

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

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