地址栏中输入中文参数,后台得到乱码

lhxzgw 2011-03-14 06:20:55
大体流程是这样的:我想在地址栏内访问后台的action,输入http://*****?id=中文,后台得到的id是乱码。经过查资料了解到id经过以下编码流程,在firefox浏览器中以gbk编码,传到tomcat后先经过iso-8859-1编码后又经过utf-8编码后传到后台,这样得到的id参数不知道如何解析成正确的编码格式。我想通过程序来编码,不想修改firefox的设置,请大家帮我想想办法。
...全文
3319 56 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
56 条回复
切换为时间正序
请发表友善的回复…
发表回复
NachOS4_1 2013-07-25
  • 打赏
  • 举报
回复
//获取请求中的参数 String username=(String)request.getParameter("username"); username =new String(username.getBytes("ISO8859-1")); //判断用户不为空切用户名不为字符串 转换下就可以了
yyy521fyy 2013-01-09
  • 打赏
  • 举报
回复
新的一年,我来补充一下吧。 是通过 window.location.href="http://www.csdn.net/a.jsp?name=中文汉字"; 与通过 直接在浏览器里面输入:http://www.csdn.net/a.jsp?name=中文汉字 得到的效果不一致。 即使能解决乱码,两者也只能解决一个。
tianzitoupai 2012-10-10
  • 打赏
  • 举报
回复
String id = new String(request.getParameter("id").getBytes("iso-8859-1"),"utf-8");
前面的朋友们都试过了。在火狐里不行的。火狐得用utf-8转,至少我测试的是这样。
IE用GBK可以转,utf-8却不可以。。
mipengchong 2011-12-01
  • 打赏
  • 举报
回复
[Quote=引用 43 楼 friendxxy 的回复:]

URL传汉字就是没事找抽型
[/Quote]

如果给第三方提供这样的接口 他要通过汉字查询。。。 你这回答 太。。。
wearmear 2011-11-16
  • 打赏
  • 举报
回复
说了半天没一个说到重点的,我也遇到这样的问题,这不是js的url传参问题也不是get方式传参问题,,,是浏览器里直接输入参数乱码问题(chrome浏览器不乱),期待解决..............................
zhou686269 2011-11-16
  • 打赏
  • 举报
回复
这个问题我也遇到过,首先地址栏直接输入中文参数不是需求,而是有人会这么做。为了系统稳定需要考虑到问题。
地址栏输入参数乱码,是因为浏览器自动把字符串按照GBK的编码方式进行编码的。
估计这个问题很难解决了。
国内大部分网站都是用GBK编码的。
ppxiao320 2011-08-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hahaytao 的回复:]

后台取值的时候需要处理:
String id = new String(request.getParameter("id").getBytes("iso-8859-1"),"GBK");
你可以尝试一下
[/Quote]

此乃正解
e生态_修身 2011-03-22
  • 打赏
  • 举报
回复
在后台可以设置:
request.setCharacterEncoding("UTF-8")
response.setCharacterEncoding("UTF-8")
就可以了,不会出现乱码了
dcyydc 2011-03-22
  • 打赏
  • 举报
回复
必须先经过编码,然后再传递~~~
哈哈哈3234 2011-03-22
  • 打赏
  • 举报
回复
String id = new String(request.getParameter("id").getBytes("iso-8859-1"),"GBK");
幻蝶成仙 2011-03-21
  • 打赏
  • 举报
回复
String id = new String(request.getParameter("id").getBytes("iso-8859-1"),"GBK");
这样就可以处理了,不过这样一般放在filter里面
liumin_0705 2011-03-21
  • 打赏
  • 举报
回复
首先需要先将页面编码:http://*****?id=encodeURI(中文)
然后在action的方法中再解码:String id = new String(id.getBytes("GBK"),"ISO8859-1");
zhang_san888 2011-03-21
  • 打赏
  • 举报
回复
1.request.setCharacterEncoding("UTF-8")或者 后台取值的时候需要处理:
String id = new String(request.getParameter("id").getBytes("iso-8859-1"),"GBK"); 这两种你都试过了吗?这应该是可以解决的呀?
xxy8100 2011-03-21
  • 打赏
  • 举报
回复
URL传汉字就是没事找抽型
songminghong 2011-03-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wula0010 的回复:]
你不能直接传中文参数,如果要传中文参数,必须先编码,encodeURI("我的参数")
[/Quote]
下一站 2011-03-20
  • 打赏
  • 举报
回复
字符编码的过滤器

import javax.servlet. * ;
import java.io.IOException;

/** */ /**
* 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题
*/
public class CharacterEncodingFilter
implements Filter
{
protected FilterConfig filterConfig = null ;
protected String encoding = "" ;

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
if (encoding != null )
servletRequest.setCharacterEncoding(encoding);
filterChain.doFilter(servletRequest, servletResponse);
}

public void destroy()
{
filterConfig = null ;
encoding = null ;
}

public void init(FilterConfig filterConfig) throws ServletException
{
this .filterConfig = filterConfig;
this .encoding = filterConfig.getInitParameter( " encoding " );

}
}
crazyzf 2011-03-20
  • 打赏
  • 举报
回复
不错 [Quote=引用 1 楼 taoshengyijiuwu 的回复:]

在后台将传过来的链接进行编码啊
[/Quote]
wyx100 2011-03-19
  • 打赏
  • 举报
回复
首先需要先将页面编码:http://*****?id=encodeURI(中文)
然后在action的方法中再解码:String id = new String(id.getBytes("GBK"),"ISO8859-1");

幻蝶成仙 2011-03-19
  • 打赏
  • 举报
回复
new String(utf.getByte("iso-8859-1"),"gbk");
tubage408 2011-03-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bao110908 的回复:]
这问题是 CSDN Java/Web 版周经帖!

Google 搜“Java GET 乱码 site:csdn.net”,能搜出一堆!
[/Quote]
哈哈,不愧是火龙果.........
加载更多回复(36)

81,122

社区成员

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

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