JSP 为什么Request接收中文出现乱码 在线等 new String(request.getParameter("taskName").getBytes("8859_1")); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 方法一:<%@page contentType="text/html; charset=GBK"%> 方法二:String s="总类管理";byte[] sbyte=s.getBytes("ISO8859_1");s=new String(sbyte);或者strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK"); 例: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; } } * *文件名:Handle.java *实现功能:使用功能集合 * *最后修改日期:2003-3-20 */package com.ali.util;public class Handle { public Handle() {} /** *字符转换为中文编码 *@param String *@return String */ public static String getCh(String s) { try { String temp_s = s; byte[] sbyte = temp_s.getBytes("ISO8859_1"); String temp = new String(sbyte); return temp; } catch (Exception e) {} return "null"; }} //END Class Strtoch 用这个函数就可以了: public String getstr(String str) { try { String temp_str=str; byte[] temp_t=temp_str.getBytes("ISO8859-1"); String temp=new String(temp_t); return temp; } catch(Exception e) { } return "null"; } **********************************************************************更正:原因不是form提交的问题,而是a.html<input name="name" type="text" " id="name" >内容为“任务一”b.jspString name=request.getParameter("name"); <jsp:forward page="c.jsp"> <jsp:param name="sname" value="<%=name%>"/></jsp:forward>c.jsprequest.getParameter("name");显示“???” b.jspString name=new String(request.getParameter("name").getBytes("8859_1"));%><jsp:forward page="c.jsp"> <jsp:param name="sname" value="<%=name%>"/></jsp:forward>c.jsp:String namec=new String(request.getParameter("name").getBytes("8859_1"));%>namec is :<%=namec%> 在每个jsp页面的开头加上<%@ page contentType="text/html; charset=GBK" %><%request.setCharacterEncoding("GB2312");//设置编码%>就可以了。 中文我也没弄好,进来学习。servlet默认用哪种编码? 从form提交过去的中文字符都是unicode 编码的,要正常显示,需要转换成gbk编码,方法前面几位说了 我不是用form 提交的,是用<jsp:forward page="c.jsp"> <jsp:param name="sname" value="<%=name%>"/></jsp:forward>相反form不用转换 不会吧,在我机器上可以的啊!!b.jsp<%@ page contentType="text/html;charset=gb2312" %><html><head><title>无标题文档</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%String name=new String(request.getParameter("name").getBytes("8859_1")); %><jsp:forward page="c.jsp"> <jsp:param name="sname" value="<%=name%>"/></jsp:forward></body></htm>c.jsp:<%@ page contentType="text/html;charset=gb2312" %><html><head><title>无标题文档</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><%String namec=new String(request.getParameter("name").getBytes("8859_1"));%>namec is :<%=namec%></body></html>我用的是tomcat4.1.18! 推荐有resin,对中文的支持很好! 乱麻问题是跟操作系统和服务器软件有关的。Unix和windows的会有不同表现。上次使用Jbuilder的时候因为装的时候选的语言不一样,同样系统平台也是有的有乱麻,有的没有。呵呵,所以建议在开发之前一定要调查一下相关的字符问题,不然会很麻烦的。 试试 java.net.URLDecoder.decode(request.getParameter("taskName")); good luck! 试试 net.URLDecoder.decode(request.getParameter("taskName")); 大家说的都是治本不治根的方法,比如tomcat就是因为自己的服务端代码有问题导致出现乱码,所以需要修改代码象weblogic这个东西配置好编码就没有问题,resin我没用过,你看是否可以看看是否可以通过配置编码达到目的如果用的是tomcat,我可以提供如果修改tomcat代码达到目的 java.net.URLDecoder.decode(request.getParameter("taskName")); 不行 我用的也是Tomcat4.1.18为什么也不行?以前在jBuilder下用new String(strInString.getBytes("ISO-8859-1"),"gb2312");可以解决但后来用Tomcat时候就不行了? 我下了新的Resin2.1.9也不行啊,是不是配置时做了什么设置啊?? 我的机器是两种情况:1、POST、GET方法得到的字符串如果有中文的话,只要在page 中设置contentType为gb2312就没有问题了。包括了用out等输出的中文也没有问题。2、当是从数据库中读取的时候,就必须要使用getBytes("ISO8859-1")然后to GBK或者GB2312的方法才行。我都快弄晕了。 <%@ page contentType="text/html;charset=ISO8859_1"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//ZH">这样也可以啦。。当把jsp页面 指定成ISO8859_1 就跟Tomcat ,JDK 一致了,不用写中文转换了而且移植到linux 也没问题 <% request.setCharacterEncoding("GBK");%> 只要一句.request.setCharacterEncodeing("gb2312"); 不行,不行,没听明白我的意思,我说的不是用form提交的。而是用<jsp:forward ccc.jsp<jsp:param name=kdkd value=kdkd这种形式,这是一种特殊情况。 调用字符转换函数public String StringInfo(String strTemp){ if(strTemp! = null && !StrTemp.equals("")){ try{ strTemp = new String(strTemp.getBytes("iso-8859-1"),"gb2312"); } catch(Exception e){ strTemp = null; } } return(strTemp);}页面出现乱码可以添加<%@ page contentType="text/html;charset=gb2312"%> 试了,不行。我用的是Tomcat4.1.18 Apache 在你的代码最前端增加一行代码:<%@page contentType="text/html;charset=gb2312"%> //最好所有的页面上都写上它.......................................String s = new String(request.getParameter("taskName").getBytes("8859_1")); 应该可以了.你试试. String T_Name=new String (request.getParameter("member1").getBytes("ISO8859_1"),"GBK") 码制的问题挺复杂,那位大虾能在这终结一下给我们这学刚入门的学生听听!Thank you~~ 各位老友。你们所说的在网页头上加<%@page contentType="text/html;charset=gb2312"%> 和使用转换函数只适合于Tomcat4.0以下版本。4.0以上版本根本就不适用。如果用post,get接受数据库中的值还是会出现乱码。而且在url中加中文参数也会出现乱码。所以各位朋友谁用Tomcat4.0以上软件的朋友来价绍一下经验。其它的朋友别来泡分了。大家说点实际的问题。难道中国的软件行业就真的跟着老外走吗?难道就真没高手了吗? 在1和2的页面都加上下面的两句话<%@ page contentType="text/html; charset=gb2312" %><%request.setCharacterEncoding("gb2312")%>就可以了, <%@ page language="java" %><%@ page contentType="text/html; charset=gb2312" %><% String msg = "只要你好";%><jsp:forward page="forwardedit.jsp" > <jsp:param name="msg" value="<%=msg%>" /></jsp:forward>forwardedit.jsp<%@ page language="java" contentType="text/html; charset=gb2312" %><% request.setCharacterEncoding("utf-8"); String msg = null; msg = request.getParameter("msg"); out.println(msg);%>这样就可以了,终于想到了答案,想了很久。 jasperreport柱状图横坐标显示问题 华为彩信接口调用问题 这样写,为什么不能新开窗口?帮忙看看 wap的中文乱码问题怎么解决 在线等!!!!!关于连接到excel的问题~~~~~~~~~~~~~~` 关于tomcat的配置问题 网上商店的定单号怎么下? jsp 怎么才能去掉地址栏中的参数 文件读写操作的问题. 最近要做一个聊天室,各位给点建议该如何做? 那有下载连接 SQL Server 2000 的JDBC 如何实现 GB2312 <=> UTF-8 交流
byte[] sbyte=s.getBytes("ISO8859_1");
s=new String(sbyte);或者strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK"); 例: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;
}
} *
*文件名:Handle.java
*实现功能:使用功能集合
*
*最后修改日期:2003-3-20
*/
package com.ali.util;public class Handle { public Handle() {} /**
*字符转换为中文编码
*@param String
*@return String
*/
public static String getCh(String s) {
try {
String temp_s = s;
byte[] sbyte = temp_s.getBytes("ISO8859_1");
String temp = new String(sbyte);
return temp;
}
catch (Exception e) {}
return "null";
}} //END Class Strtoch
public String getstr(String str)
{
try
{
String temp_str=str;
byte[] temp_t=temp_str.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
}
return "null";
}
***********************************
更正:
原因不是form提交的问题,而是
a.html
<input name="name" type="text" " id="name" >
内容为“任务一”
b.jsp
String name=request.getParameter("name"); <jsp:forward page="c.jsp">
<jsp:param name="sname" value="<%=name%>"/>
</jsp:forward>
c.jsp
request.getParameter("name");显示“???”
String name=new String(request.getParameter("name").getBytes("8859_1"));%><jsp:forward page="c.jsp">
<jsp:param name="sname" value="<%=name%>"/>
</jsp:forward>
c.jsp:
String namec=new String(request.getParameter("name").getBytes("8859_1"));%>
namec is :<%=namec%>
在每个jsp页面的开头加上
<%@ page contentType="text/html; charset=GBK" %>
<%
request.setCharacterEncoding("GB2312");//设置编码
%>就可以了。
<jsp:forward page="c.jsp">
<jsp:param name="sname" value="<%=name%>"/>
</jsp:forward>
相反form不用转换
b.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
String name=new String(request.getParameter("name").getBytes("8859_1")); %>
<jsp:forward page="c.jsp">
<jsp:param name="sname" value="<%=name%>"/>
</jsp:forward>
</body>
</htm>c.jsp:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>无标题文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
String namec=new String(request.getParameter("name").getBytes("8859_1"));%>
namec is :<%=namec%>
</body>
</html>
我用的是tomcat4.1.18!
Unix和windows的会有不同表现。
上次使用Jbuilder的时候因为装的时候选的语言不一样,同样系统平台也是有的有乱麻,有的没有。呵呵,所以建议在开发之前一定要调查一下相关的字符问题,不然会很麻烦的。
象weblogic这个东西配置好编码就没有问题,resin我没用过,你看是否可以看看是否可以通过配置编码达到目的
如果用的是tomcat,我可以提供如果修改tomcat代码达到目的
以前在jBuilder下用new String(strInString.getBytes("ISO-8859-1"),"gb2312");可以解决但后来用Tomcat时候就不行了?
request.setCharacterEncoding("GBK");
%>
没听明白我的意思,我说的不是用form提交的。而是用
<jsp:forward ccc.jsp
<jsp:param name=kdkd value=kdkd
这种形式,这是一种特殊情况。
if(strTemp! = null && !StrTemp.equals("")){
try{
strTemp = new String(strTemp.getBytes("iso-8859-1"),"gb2312");
}
catch(Exception e){
strTemp = null;
}
}
return(strTemp);
}页面出现乱码可以添加
<%@ page contentType="text/html;charset=gb2312"%>
我用的是Tomcat4.1.18 Apache
<%@page contentType="text/html;charset=gb2312"%> //最好所有的页面上都写上它
.......................................
String s = new String(request.getParameter("taskName").getBytes("8859_1")); 应该可以了.你试试.
和使用转换函数只适合于Tomcat4.0以下版本。4.0以上版本根本就不适用。
如果用post,get接受数据库中的值还是会出现乱码。而且在url中加中文参数也会出现乱码。
所以各位朋友谁用Tomcat4.0以上软件的朋友来价绍一下经验。其它的朋友别来泡分了。
大家说点实际的问题。难道中国的软件行业就真的跟着老外走吗?
难道就真没高手了吗?
<%@ page contentType="text/html; charset=gb2312" %>
<%request.setCharacterEncoding("gb2312")%>
就可以了,
<%@ page contentType="text/html; charset=gb2312" %>
<%
String msg = "只要你好";
%>
<jsp:forward page="forwardedit.jsp" >
<jsp:param name="msg" value="<%=msg%>" />
</jsp:forward>forwardedit.jsp
<%@ page language="java" contentType="text/html; charset=gb2312" %>
<%
request.setCharacterEncoding("utf-8");
String msg = null;
msg = request.getParameter("msg");
out.println(msg);
%>这样就可以了,终于想到了答案,想了很久。