org.apache.jasper.JasperException: Unable to compile D:\website\tomcat\work\DEFAULT\examples\test\_1_1.java:74: Method setCharacterEncoding(java.lang.String) not found in interface javax.servlet.http.HttpServletRequest. request.setCharacterEncoding("GBK");楼上凯凯老兄,会不会是我jdk的版本有问题。或者tomcat有问题。 在这里是不方便用form的。只能在url里面传递。
String mCaption=new String(request.getParameter("Caption").getBytes("8859_1"));
给你一个字符转换函数!你可以写倒类中,重复使用
/**** 字符集的转换函数 ****/
// added by baiqing 2003.02.18
public static String charConvert(String strSource){
if (strSource == null){
return "";
}
else{
try{
//return new String(strSource.getBytes("GB2312"), "ISO-8859-1");
String str = new String(strSource.getBytes("ISO-8859-1"), "GB2312");
System.out.println(str);
return str;
}
catch (Exception ex){
System.out.println(ex.toString());
return "";
}
}
}
test.html如下:
var test='你好';
var help=window.open("test.jsp?name="+test,"help","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=1200,height=1000");
test.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%
//response.setContentType("text/html;charset=UTF-8");
//String name = new String(request.getParameter("name").getBytes("8859_1"), "GB2312") ;
//String name = new String(request.getParameter("name").getBytes("8859_1"), "UTF-8");
//request.setCharacterEncoding("UTF-8");
//response.setContentType("text/html;charset=UTF-8");
String name = request.getParameter("name");
//String str = new String(name.getBytes("ISO-8859-1"), "GB2312");
String str = new String(name.getBytes("GB2312"), "ISO-8859-1");
/*if(name != null && name != "")
out.println("empty");
byte[] tmpbyte=name.getBytes("ISO8859_1");
String Hi=new String(tmpbyte);
name = new String(name.getBytes("ISO8859_1"), "UTF-8");
*/
out.println(str);
out.println("yuu succeed!");
%>
这些方法我都试过了。
传递
**************
<a href="<%=request.getRequestURL()%>?username=<%=URLEncoder.encode("中文")%>">click</a>
*********
接收
******
String username=new String(request.getParameter("username").getBytes("ISO8859_1"),"gb2312");
out.println(username);
var test="我们"
open("Jsp1.jsp?test=" + test,'','');
</script>Jsp1.jsp如下:
<%@ page contentType="text/html; charset=GBK" %>
<%request.setCharacterEncoding("GBK");%>
<html>
<head>
<title>
Jsp1
</title>
</head>
<body>
<h1>
<%= request.getParameter("test")%>
</h1>
</body>
</html>没有问题啊,挺好的
我上面说的escape好象用不着
可能在某些情况下,不方便用form,需要在url上附加参数。
request.setCharacterEncoding("GBK");楼上凯凯老兄,会不会是我jdk的版本有问题。或者tomcat有问题。
在这里是不方便用form的。只能在url里面传递。
URLEncoder.Encode(param);取出来后用
URLDecoder.decode(param);解码
如果你用jbuilder,里面就有Tomcat 4.0
<%@ page contentType="text/html; charset=GBK" %>
或者
<%@ page contentType="text/html;charset=gb2312"%>
就完全解决了
可我一直用的是Tomcat 4.0.3 啊,中文问题一直是这样解决的。
test.htm
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<script language="JavaScript">
var test = "例子";
//var test1 = escape(test);
alert(encodeURIComponent(test));window.open("test.jsp?select="+encodeURIComponent(test),'','');</script>
<body>
</body>
</html>test.jsp:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.awt.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body >
<div align="left">查询结果:
<%
boolean bFlag=true;
String sPoint ="";
byte[] bTemp;
sPoint = request.getParameter("select");
if(sPoint!=null)
{
bTemp=request.getParameter("select").getBytes("ISO8859_1");
sPoint= new String(bTemp,"UTF-8");
}
out.println(sPoint);
%>
</div>
</body>
</html>