关于htm向jsp传中文参数的问题 在jsp程序的头部加上:<%@ page contentType="text/html;charset=gb2312" %>试试! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个问题是你要做编码转换:比如设?号后的参数名为Name,得到参数后赋值的变量名为szName,则代码如下:<%@ page contentType="text/html;charset=gb2312"%>.......<%String szName;szName = new String(request.getParameter("Name").trim().getBytes("ISO8859-1"),"gb2312");%>.......其中“gb2312”是用于简体页面,若是繁体则page contentType和new String中均改用“Big5” 各位大侠,我的服务器用的是apache+tomcat。我帖出代码,麻烦大家帮我看看。。谢1.htm<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF" text="#000000"><p><a href="1.jsp?aa=你好"> test</a></p></body></html>1.jsp<%@ page contentType="text/html;charset=gb2312"%><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF" text="#000000"><% String szName; szName = new String(request.getParameter("aa").trim().getBytes("ISO8859-1"),"gb2312"); out.println(szName);%></body></html>这样还是打印出空白。。??? 你试试在传输中文参数前做中文编码,当然html就变成jsp了 你的转换有问题。用不着那么麻烦。直接用szName = (String)request.getParameter("aa");就可以了。 不加<%@ page contentType="text/html;charset=gb2312"%>并且不做getBytes("ISO8859-1"),"gb2312");插入数据取数据等可以支持中文,但是我得out.println("alert('对不起,此栏目只有注册用户以上权限才能访问!请注册或登录!');");会是乱码,而且String msg="用户名或密码空缺,请修正!"; session.putValue("login_message",msg);也会是乱码的,这样很矛盾的,我在连接数据库时做了DriverManager.getConnection("jdbc:mysql://localhost/bbs_db?useUnicode=true&characterEncoding=ISO-8859-1","root","shinco");谁能帮我解决 我这样写,没有问题1.<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>2.<% String szName; szName = new String(request.getParameter("aa").getBytes("ISO8859-1")); out.println(szName);%> 如果数据库里存的是中文表头改为:<%@ page contentType="text/html;charset=ISO8859_1"%>如果数据库里存的是乱码//试试下面的每个方法,转换得到的参数!public static String iso8859togbk(String strvalue) { try{ if(strvalue==null) return null; else { strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK"); return strvalue; } }catch(Exception e){ return null; } } public static String iso8859togb2312(String strvalue) { try{ if(strvalue==null) return null; else { strvalue = new String(strvalue.getBytes("ISO8859_1"), "gb2312"); return strvalue; } }catch(Exception e){ return null; } } public static String gb2312to8859(String strvalue) { try{ if(strvalue==null) return null; else { strvalue = new String(strvalue.getBytes("gb2312"), "ISO8859_1"); return strvalue; } }catch(Exception e){ return null; } } public static String gbkto8859(String strvalue) { try{ if(strvalue==null) return null; else { strvalue = new String(strvalue.getBytes("gbk"), "ISO8859_1"); return strvalue; } }catch(Exception e){ return null; } } to toxyboy: 试试a='你好' 然后直接用String szName=request.getParameter("aa"); 1.jsp<%@ page contentType="text/html;charset=gb2312"%><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF" text="#000000"><% request.setCharacterEncoding("GB2312");//***** String szName; szName = request.getParameter("aa");//***** out.println(szName);%></body></html>一定好用 To toxyboy(胡言乱语)我刚才用你贴出的代码,如下:1.htm<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF" text="#000000"><p><a href="1.jsp?aa=你好"> test</a></p></body></html>1.jsp<%@ page contentType="text/html;charset=gb2312"%><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body bgcolor="#FFFFFF" text="#000000"><% String szName; szName = new String(request.getParameter("aa").trim().getBytes("ISO8859-1"),"gb2312"); out.println(szName);%></body></html>做了测试,环境与你一样。完全是可以的,而且JSP中的中文问题。请注意以下三个方面:请注意以下三个方面:1、你JSP的每个页面前有注明 <%@ page contentType="text/html;charset=gb2312"%> 用来指定页面的编码。2、你JSP取数据库时有重新编码字串,如下: szName = new String(rs.getString("name").getBytes(),"gb2312");3、你JSP的取传入参数时,也要重新编码字串,如下: szSubject = new String(request.getParameter("Subject").trim().getBytes("ISO8859-1"),"gb2312"); 则是最规范的。所以我建议你重新启动机器及服务器,然后再试试 To shinco1(shinco)在页面前加上:<%@ page contentType="text/html;charset=gb2312"%>是用来指定页面的编码方式。因为JSP页面在执行时,服务器的原理是先将其传成Java Sevlet,然后再编译执行。所以编译阶段的编码很重要。你不指定这个编码,则在有些简单问题上中文是正确的。但复杂的问题上就邮错了。 晕了,,,不过还是谢谢各位的鼎立协助。没辄了。我换了台机器,重起n便,把各位的方法都试过了,还是空白正如zxhong所说,jsp就没有取到中文,连乱码都没有。但是如果是英文数字就都正确。唉,在up一把,如果明天还不行俺就散分答谢各位了。 用Resin不存这个问题,现在我正在开发一个jsp项目,就没问题 你试试如下代码,它能将你前一个页面所传达的所有参数和相应的值列举出来,从中你就可以看到你的问题了。Enumeration e=request.getParameterNames();String n_para="";while (e.hasMoreElements()) { n_para=(String)e.nextElement(); if (n_para!=null) { out.println("<br>"+n_para+" : "+request.getParameter(n_para)); }} 关于企业组织和权限管理 求ireport程序实例 初学struts问题运行tomcat问题 垃圾邮件的处理方案征集 JavaWeb开发中,能用得多少面向对象的思想? 请求参数乱码的问题 不知道有没有人能调通这个程序,关于数字签名的,up有分 (急急-在线等)一个复选框的问题 JSP如何运行外部程序。 100分相送,各位大虾,小弟我有一个关于SmartUpload的问题,请作答一下! JSP中import的问题 请问,什么叫B/S,什么叫C/S,二者有什么区别?
<%@ page contentType="text/html;charset=gb2312"%>
.......
<%
String szName;
szName = new String(request.getParameter("Name").trim().getBytes("ISO8859-1"),"gb2312");
%>
.......
其中“gb2312”是用于简体页面,若是繁体则page contentType和new String中均改用“Big5”
我帖出代码,麻烦大家帮我看看。。谢
1.htm
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body bgcolor="#FFFFFF" text="#000000">
<p><a href="1.jsp?aa=你好"> test</a></p></body>
</html>
1.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body bgcolor="#FFFFFF" text="#000000">
<%
String szName;
szName = new String(request.getParameter("aa").trim().getBytes("ISO8859-1"),"gb2312");
out.println(szName);
%></body>
</html>
这样还是打印出空白。。???
直接用
szName = (String)request.getParameter("aa");
就可以了。
但是我得out.println("alert('对不起,此栏目只有注册用户以上权限才能访问!请注册或登录!');");会是乱码,而且String msg="用户名或密码空缺,请修正!"; session.putValue("login_message",msg);也会是乱码的,这样很矛盾的,我在连接数据库时做了DriverManager.getConnection("jdbc:mysql://localhost/bbs_db?useUnicode=true&characterEncoding=ISO-8859-1","root","shinco");谁能帮我解决
1.<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
2.
<%
String szName;
szName = new String(request.getParameter("aa").getBytes("ISO8859-1"));
out.println(szName);
%>
表头改为:<%@ page contentType="text/html;charset=ISO8859_1"%>
如果数据库里存的是乱码
//试试下面的每个方法,转换得到的参数!
public static String iso8859togbk(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
return strvalue;
}
}catch(Exception e){
return null;
}
} public static String iso8859togb2312(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("ISO8859_1"), "gb2312");
return strvalue;
}
}catch(Exception e){
return null;
}
} public static String gb2312to8859(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("gb2312"), "ISO8859_1");
return strvalue;
}
}catch(Exception e){
return null;
}
}
public static String gbkto8859(String strvalue)
{
try{
if(strvalue==null)
return null;
else
{
strvalue = new String(strvalue.getBytes("gbk"), "ISO8859_1");
return strvalue;
}
}catch(Exception e){
return null;
}
}
试试a='你好'
然后直接用String szName=request.getParameter("aa");
1.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body bgcolor="#FFFFFF" text="#000000">
<% request.setCharacterEncoding("GB2312");//*****
String szName;
szName = request.getParameter("aa");//*****
out.println(szName);
%></body>
</html>一定好用
我刚才用你贴出的代码,如下:1.htm
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body bgcolor="#FFFFFF" text="#000000">
<p><a href="1.jsp?aa=你好"> test</a></p></body>
</html>1.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><body bgcolor="#FFFFFF" text="#000000">
<%
String szName;
szName = new String(request.getParameter("aa").trim().getBytes("ISO8859-1"),"gb2312");
out.println(szName);
%></body>
</html>做了测试,环境与你一样。完全是可以的,而且JSP中的中文问题。请注意以下三个方面:
请注意以下三个方面:
1、你JSP的每个页面前有注明
<%@ page contentType="text/html;charset=gb2312"%>
用来指定页面的编码。2、你JSP取数据库时有重新编码字串,如下:
szName = new String(rs.getString("name").getBytes(),"gb2312");3、你JSP的取传入参数时,也要重新编码字串,如下:
szSubject = new String(request.getParameter("Subject").trim().getBytes("ISO8859-1"),"gb2312");
则是最规范的。所以我建议你重新启动机器及服务器,然后再试试
在页面前加上:
<%@ page contentType="text/html;charset=gb2312"%>
是用来指定页面的编码方式。因为JSP页面在执行时,服务器的原理是先将其传成Java Sevlet,然后再编译执行。所以编译阶段的编码很重要。你不指定这个编码,则在有些简单问题上中文是正确的。但复杂的问题上就邮错了。
没辄了。我换了台机器,重起n便,把各位的方法都试过了,还是空白
正如zxhong所说,jsp就没有取到中文,连乱码都没有。但是如果是英文数字就都正确。唉,在up一把,如果明天还不行俺就散分答谢各位了。
String n_para="";
while (e.hasMoreElements()) {
n_para=(String)e.nextElement();
if (n_para!=null) {
out.println("<br>"+n_para+" : "+request.getParameter(n_para));
}
}