request.getParameter()取值乱码

yczyh364 2011-09-13 11:15:07
大家好,我到网上找了很多关于request.getParameter这个乱码的解决方法都对我这个小程序不管用,把程序贴出来,大家帮我看看
我的jsp页面是这样的
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@page import="com.hanke.dao.Muju"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%request.setCharacterEncoding("UTF-8"); %>
<title>设备及模具系统</title>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<script type="text/JavaScript">

</script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>
<td height="30"> <table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="62" background="images/nav04.gif"> </td>
</tr>
</table></td></tr>
<tr>
<td><table id="subtree1" style="DISPLAY: " width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">

<tr>
<td height="40" class="font42">
<table width="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#464646" class="newfont03">
<tr class="CTitle" >
<td height="22" colspan="11" align="center" style="font-size:16px">设备一览表</td>
</tr>
<tr bgcolor="#EEEEEE">
<td height="22" align="center" >模具编号</td>
<td height="22" align="center" >模具名称</td>
<td height="22" align="center" >购买日期</td>
<td height="22" align="center" >价格</td>
<td height="22" align="center" >适应产品</td>
<td height="22" align="center" >确认人</td>
<td height="22" align="center" >使用部门</td>
<td height="22" align="center" >供应商</td>
<td height="22" align="center" >备注</td>
<td height="22" align="center" >操作</td>
</tr>
<% List list=(List)request.getAttribute("list");
if(list!=null&&list.size()>0){
Iterator it = list.iterator();
while (it.hasNext()) {
Muju j = (Muju) it.next();

%>
<tr bgcolor="#FFFFFF">
<td height="22" align="center" ><%=j.getMujuid()%></td>
<td height="22" align="center" ><%=j.getMujuname()%></td>
<td height="22" align="center" ><%=j.getBuydate()%></td>
<td height="22" align="center" ><%=j.getPrice()%></td>
<td height="22" align="center" ><%=j.getInproduct()%></td>
<td height="22" align="center" ><%=j.getConfimer()%></td>
<td height="22" align="center" ><%=j.getDepartment() %></td>
<td height="22" align="center" ><%=j.getSupplier()%></td>
<td height="22" align="center" ><%=j.getRemark()%></td>
<td height="22" align="center" ><a href="muju.do?opt=modify&id=<%=j.getMujuid()%>&name=<%=j.getMujuname()%>&buydate=<%=j.getBuydate()%>&price=<%=j.getPrice()%>&product=<%=j.getInproduct()%>&confimer=<%=j.getConfimer()%>&dept=<%=j.getDepartment() %>&supplier=<%=j.getSupplier()%> ">修改</a>  ;<a href="muju.do?opt=delete&id=<%=j.getMujuid()%>">删除</a>  <a href="mjweixiu.do?opt=weixiu&id=<%=j.getMujuid()%>&name=<%=j.getMujuname()%>">维修</a>
  <a href="sbbaoyang.do?opt=baoyang&id=<%=j.getMujuid()%>&name=<%=j.getMujuname()%>">保养</a>  <a href="sbbaoyangjh.do?opt=baoyangjh&id=<%=j.getMujuid()%>&name=<%=j.getMujuname()%>">保养计划</a>  <a href="sbbeijian.do?opt=beijian&id=<%=j.getMujuid()%>&name=<%=j.getMujuname()%>">备件</a>
  <a href="sbbaoyangjh.do?opt=baoyangjh&id=<%=j.getMujuid()%>&name=<%=j.getMujuname()%>">保养计划</a>  <a href="sbjianyan.do?opt=jianyan&id=<%=j.getMujuid()%>&name=<%=j.getMujuname()%>&dep=<%=j.getDepartment()%>">验收</a>
</td>
</tr>

<%
}
}else{
%>
<tr bgcolor="#FFFFFF">
<td height="22" colspan="10" align="center" >对不起,没有添加设备信息!!!</td>
</tr>
<%}%>
</table></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
我的Aaction是这样写的

public ActionForward modify(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
MujuForm mujuForm = (MujuForm) form;// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
//mujuForm.setMujuid(request.getParameter("id"));
//mujuForm.setMujuname(request.getParameter("name"));
//mujuForm.setInproduct(request.getParameter("product"));
//mujuForm.setConfimer(request.getParameter("confimer"));
mujuForm.setMujuid(new String(request.getParameter("id").getBytes("ISO-8859-1"),"UTF-8"));
mujuForm.setMujuname(new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8"));
mujuForm.setBuydate(new String(request.getParameter("buydate").getBytes("ISO-8859-1"),"UTF-8"));
mujuForm.setPrice(new String(request.getParameter("price").getBytes("ISO-8859-1"),"UTF-8"));
mujuForm.setInproduct(new String(request.getParameter("product").getBytes("ISO-8859-1"),"UTF-8"));
mujuForm.setConfimer(new String(request.getParameter("confimer").getBytes("ISO-8859-1"),"UTF-8"));
mujuForm.setDepartment(new String(request.getParameter("dept").getBytes("ISO-8859-1"),"UTF-8"));
mujuForm.setSupplier(new String(request.getParameter("supplier").getBytes("ISO-8859-1"),"UTF-8"));
String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
System.out.println("name====="+request.getParameter("name"));
System.out.println("product====="+request.getParameter("product"));
System.out.println("name====="+new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8"));

return mapping.findForward("xgmj");
}

...全文
1498 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuyuwei10427 2012-01-06
  • 打赏
  • 举报
回复
request.getParameter()的得到乱码是没写过滤器吧
XML code
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
这种是解决url传参出现乱码的方法,总之,解决乱码有很多种方式,要选择一种最有效最简便的,其实原理都是大同小异


liuguopin_2012 2011-12-12
  • 打赏
  • 举报
回复
我也是转码有问题 楼上各位大哥也帮帮小弟
页面第一次加载有一条数据是乱码 3604.1R版本 刷新一下就正常了
ie9一直没问题
其他版本的ie 火狐都是乱码 会是什么原因呢
wouhuh198343 2011-11-08
  • 打赏
  • 举报
回复
我的也是这种问题,jsp,java和tomcat的编码都是utf-8,直接post方式提交,java中获取的就是乱码。如果在传递之前用java.net.URLEncode.encode()方法编码一次,在后台java中在用decode方式解码一次,可以得到正确的。但是XXX.jsp?name=<%java.net.URLEncode.encode("")%>这种方式没办法把输入框的值取得,所以还没办法解决。
lvzhuze 2011-09-14
  • 打赏
  • 举报
回复
乱码解决问题:(服务器server.xml中配置)

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>

不知道能不能解决你的问题
quanlei1507053 2011-09-14
  • 打赏
  • 举报
回复
我的妈妈啊,看到你转码方式,我就疯,写个过滤器很简单,lz为什么说过滤器值只能post起作用?我转码post跟get都起作用
  • 打赏
  • 举报
回复 1
[Quote=引用 13 楼 yczyh364 的回复:]

现在是也不是有的中文都是乱码,就是中文中有个别字是乱码,这是怎么回事?
例如:制冰机模??
[/Quote]

转码格式,如果你是Form提交,method应该为post。
如果你利用URL传值,2byte的文字必须转为1byte的。
利用方法两种:
1.server端利用java.net.URIEncode.encode(String ,"utf-8");
2.客户端 encodeURIComponent利用。

最后取值利用 java.net.URIDecode.decode(String ,"utf-8");

yczyh364 2011-09-14
  • 打赏
  • 举报
回复
现在是也不是有的中文都是乱码,就是中文中有个别字是乱码,这是怎么回事?
例如:制冰机模??
甲壳虫 2011-09-13
  • 打赏
  • 举报
回复
在url中传值中文的时候需要java.net.URLEncoder.encode转一下,可以去网上搜搜这个的用法。
bijanen19861113 2011-09-13
  • 打赏
  • 举报
回复
System.out.println("name====="+new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8")这里打印的也是乱码吗?通过转码之后得到的应该是正常的中文才对啊..
风丶子 2011-09-13
  • 打赏
  • 举报
回复
个人建议:
不要去用<a> 去传中文值,
如果有中文值建议你把值放到隐藏表单域中,然后用表单提交。
yczyh364 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 phonenix66 的回复:]
<%request.setCharacterEncoding("UTF-8"); %>
<%request.setCharacterEncoding("GBK"); %>
[/Quote],

这个方法我试了也不行,真晕的,楼上各位兄弟提供的方法我之前都已经试过,都不行的,真是遇到怪事了
phonenix66 2011-09-13
  • 打赏
  • 举报
回复
<%request.setCharacterEncoding("UTF-8"); %>
<%request.setCharacterEncoding("GBK"); %>
yczyh364 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 anybyb 的回复:]
用java.net.URLEncoder.encode编码,注意有时候要编码两次编码
然后呢再把编码后的参数传到action。
这个方法反正我是用了很多次了 对ie6也管用,楼主试试看
[/Quote]

谢谢你
可以把你说的这个方法具体在我这里的使用方法写给我看下吗?我试了下也不行,可能是我使用方法有问题。
阿诺 2011-09-13
  • 打赏
  • 举报
回复
用java.net.URLEncoder.encode编码,注意有时候要编码两次编码
然后呢再把编码后的参数传到action。
这个方法反正我是用了很多次了 对ie6也管用,楼主试试看
yczyh364 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gqf19891018 的回复:]
写了过滤器没有啊?
[/Quote]
有过滤器的,但是这种方式没有post提交,所以他是不经过过滤器的。
风丶子 2011-09-13
  • 打赏
  • 举报
回复
写了过滤器没有啊?
yczyh364 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ajun_studio 的回复:]
前台和后天同意编码
然后再System.out.println("name====="+new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8")
不行 改一下tomcat的编码为utf-8 应该就可以了
[/Quote]

我前台和后台都是用的UTF-8 的编码啊, tomcat 中的server.xml 中也调整了,还是没有用啊
飓风zj 2011-09-13
  • 打赏
  • 举报
回复
前台和后天同意编码
然后再System.out.println("name====="+new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8")
不行 改一下tomcat的编码为utf-8 应该就可以了
yczyh364 2011-09-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bijanen19861113 的回复:]
System.out.println("name====="+new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8")这里打印的也是乱码吗?通过转码之后得到的应该是正常的中文才对啊..
[/Quote]

这个也是乱码哦

81,092

社区成员

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

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