在Java Web编程的学习中,大家都会遇到URL传递参数问题。其中如果想在URL进行中文参数的传递,那就要注意中文转码问题。不然会出现乱码。
下面给出解决URL地址传递中文参数问题
第一种:利用URLEncoder类来进行处理
(1)首先创建一个FileList.jsp,代码如下
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import = "java.net.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件下载列表</title>
</head>
<body>
[url=Video.jsp?value=<%=URLEncoder.encode(]">视频文件[/url]
</body>
</html>
从上面的URL可以看出<a href="Video.jsp?value=<%=URLEncoder.encode("刘阳","gb2312")%>">,利用URLEncoder对中文的汉字进行编码
(2)创建Video.jsp,用来获取value参数值。代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>视频文件</title>
</head>
<body>
<%
String str = request.getParameter("value");
%>
<%
System.out.println(new String(str.getBytes("iso-8859-1"),"gb2312"));
%>
<object id= "player" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width= "320" height= "217">
<param NAME="AutoStart" VALUE="1">
<param name= "url" value= "I_will_survive_Alien.mpeg">
</object>
</body>
</html>
1、URL中编码格式为ISO-8859-1,处理中文只需将编码格式转换ISO-8859-1
2、IE缺省对URL后面的参数是不编码发送的,但是tomat缺省是按ISO8859-1来进行URL解码
第二种:在Tomcat的中server.xml文件中加入URIEncoding="GBK",具体代码如下
<!-- Define a non-SSL HTTP/1.1 Connector on port 8080 -->
<Connector port="8888" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK"/>
然后重新启动Tomcat服务器
(1)创建FileList.jsp,代码如下
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ page import = "java.net.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件下载列表</title>
</head>
<body>
[url=/JavaWeb/DownLoad?value=java精通swing程序设计.asp.chm]下载文件[/url]
</body>
</html>
(2)创建一个DownLoad.java文件,来获取value的值
String s = request.getParameter("value");
第三种:利用request.setCharacterEncoding("UTF-8"),下面会给出两个JSP页面,index.jsp和error.jsp
index.jsp页面的源码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.util.Date" %>
<%@ page errorPage="" %>
<%@ page session="true" %>
<%@ page import="java.net.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<% request.setCharacterEncoding("UTF-8"); %>
This is my JSP page. <br>
<jsp:include page="error.jsp">
<jsp:param name="value" value="我是帅哥"/>
</jsp:include>
</body>
</html>
error.jsp页面源码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String a = request.getParameter("value");
out.println("此内容是来源于index.jsp页面传过来的数据: " + a);
%><br>
</body>
</html>
以上代码的运行流程:在index.jsp页面中加入了<jsp:include>标签,同时为URL传递了一个中文参数。首先,设置此两个页面的编码方式为UTF-8,然后在index.jsp页面中加入request.setCharacterEncoding("UTF-8"),此方法作用就是将URL中的请求数据进行UTF-8编码。这样子才能显示正常的中文,因为URL传递参数的默认编码是“ISO-8859-1”