strName = new String(request.getParameter("name").getBytes("ISO-8859-1"));
Filter最好,但要注意配置 在接受端采用 strName = new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312");也可以
request.setCharacterEncoding(“GBK”)和 strName = new String(request.getParameter("name").getBytes("ISO-8859-1")); 都预报 Incompatible type for constructor. Can't convert java.lang.String to byte[]. 这是什么错误?
to:poyer(瑞德) 出现如下错误: Unable to compile class for JSP/usr/local/jakarta-tomcat/work/localhost_8080/_0002ftest_00031_0002ejsptest1_jsp_16.java:57: Method setCharacterEncoding(java.lang.String) not found in interface javax.servlet.http.HttpServletRequest. request.setCharacterEncoding("gb2312");
写一个简单的JavaBean,以后调用也方便! package beans ; public class ISOtoGB2312 { public String getConvert(String str) { try { byte[] byteStr=str.getBytes("ISO-8859-1"); return new String(byteStr,"gb2312"); } catch(Exception e) { return str; } } } <jsp:useBean id="chage" scope="session" class="beans.ISOtoGB2312"/>String newname=chage.getConvert(request.getParameter("newname"));这样就可以!
发送端: strName = new String((request.getParameter("name")).getBytes("gb2312"),"ISO-8859-1"); 接受端: jsp头部加<%@ page contentType="text/html;charset=gb2312"%> 或jsp程序内部加 String title = null; while(rs.next()){ title = rs.getString("title"); title = new String(title.getBytes("GBK"), "ISO8859_1"); %>
在接受端采用
strName = new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312");也可以
strName = new String(request.getParameter("name").getBytes("ISO-8859-1"));
都预报
Incompatible type for constructor. Can't convert java.lang.String to byte[].
这是什么错误?
或加入函数试试public static String getStr(String str)
{
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859_1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
System.out.println(e.toString());
return "null";
}
}
如果name变量是中文,就不能传递过去,查了很多资料都不能解决
大家都是怎么解决的啊?郁闷死了!我快疯了啊
public static String getStr(String str)
{
try{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859_1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
System.out.println(e.toString());
return "null";
}
}
%>
<%String aa =getStr(request.getParameter("name"));%>
pnr="中文";
URL url=new URL( "http://202.106.139.30:80/servlet/zm_booktkt.SeePnr?pnrno="+URLEncoder.encode(pnr) );
strName = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"gb2312");
一定要注意括号(request.getParameter("name"))是不可缺少的!
<%
request.setCharacterEncoding("gb2312"); //正常接收中文
%>
出现如下错误:
Unable to compile class for JSP/usr/local/jakarta-tomcat/work/localhost_8080/_0002ftest_00031_0002ejsptest1_jsp_16.java:57: Method setCharacterEncoding(java.lang.String) not found in interface javax.servlet.http.HttpServletRequest.
request.setCharacterEncoding("gb2312");
package beans ;
public class ISOtoGB2312
{
public String getConvert(String str)
{
try
{
byte[] byteStr=str.getBytes("ISO-8859-1");
return new String(byteStr,"gb2312");
}
catch(Exception e)
{
return str;
}
}
}
<jsp:useBean id="chage" scope="session" class="beans.ISOtoGB2312"/>String newname=chage.getConvert(request.getParameter("newname"));这样就可以!
对于用Form POST方式提交的表单,不需要这个步骤;对于用直接写xxx.jsp?name=YYY的,需要这样做。
String strValue = request.getParameter("valueName");
strValue = new String(strValue.getBytes("ISO8859_1"),"gb2312");
String newStr=new String(request.getParameter("name").getBytes("ISO-8859-1"));
%>
String test="中文";
String name2=java.net.URLEncoder.encode(test);
String name3=java.net.URLDecoder.decode(name2); out.print(name3);
%>
显示结果还是??的乱码,怎么回事,还是没有解决啊?
我快疯了啊!
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
就ok了!
<%request.setCharacterEncoding("gb2312");%>
比如你要传递的参数名是“中文”,参数值是“姓名”
<%String name=request.getParameter("中文");%>
则<%=name%>的输出结果就是“姓名”了
strName = new String((request.getParameter("name")).getBytes("gb2312"),"ISO-8859-1");
接受端:
jsp头部加<%@ page contentType="text/html;charset=gb2312"%>
或jsp程序内部加
String title = null;
while(rs.next()){
title = rs.getString("title");
title = new String(title.getBytes("GBK"), "ISO8859_1");
%>
如果你是用tomcat的话,你升级到tomcat4.1版本的
我以前就遇到这种问题,其它的方法都试过后
升级tomcat到tomcat4.1版本就解决了