对于初步接触jsp开发的程序员来说,中文乱码往往是令人头疼的。今天,我就向大家介绍一下我常用的方法。
首先,我们以一个简单的例子来介绍:
例:
1. 第一个jsp页面index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset =utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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">
</head>
<body>
<%
String name="我是谁";
%>
<a href="show.jsp?name=<%=name %>">跳转</a>
</body>
</html>
2.跳转到页面show.jsp 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.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">
</head>
<body>
<%
String name=request.getParameter("name");
%>
<%=name %>
</body>
</html>
以上页面是把index.jsp页面中的name的值通过超链接传递到show.jsp页面中:
运行程序,我们发现:
在show.jsp页面中输出是 : ææ¯è°
这就出现了中文乱码,如何解决呢?
下面我介绍2种方法:
方法1:
(1) 在index.jsp页面中用jquery 或js 把超链接路径转换一下
代码如下:
<%@ page language="java" import="java.util.*" contentType="text/html;charset =utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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">
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script></head>
<body>
<%
String name="我是谁";
%>
<a href="show.jsp?name=<%=name %>">跳转</a>
</body>
</html>
<script>
var href=$("a").attr("href");
href = encodeURI(href);
href = encodeURI(href);
$("a").attr("href",href);
</script>
(2).在show.jsp页面中用java.net.URLDecoder.decode()方法进行转化.
代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.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">
</head>
<body>
<%
String name=request.getParameter("name");
name = java.net.URLDecoder.decode(name,"UTF-8");
%>
<%=name %>
</body>
</html>
再次运行程序,发现不在乱码了,问题也解决了 。但是,细心一点会发现 以上用到了jquery ,有点麻烦,有没有更好的方法呢?我们看另外一个方法:
二 只需在show.jsp 页面中把得到的name进行转码
1 index.jsp 页面代码:
<%@ page language="java" import="java.util.*" contentType="text/html;charset =utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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">
<body>
<%
String name="我是谁";
%>
<a href="show.jsp?name=<%=name %>">跳转</a>
</body>
</html>
2 show.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.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">
</head>
<body>
<%
String name=request.getParameter("name");
name= new String(name.getBytes("ISO-8859-1"),"utf-8");
%>
<%=name %>
</body>
</html>
运行,发现问题也解决了
建议大家使用第二种方法
谢谢大家!
首先,我们以一个简单的例子来介绍:
例:
1. 第一个jsp页面index.jsp
<%@ page language="java" import="java.util.*" contentType="text/html;charset =utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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">
</head>
<body>
<%
String name="我是谁";
%>
<a href="show.jsp?name=<%=name %>">跳转</a>
</body>
</html>
2.跳转到页面show.jsp 代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.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">
</head>
<body>
<%
String name=request.getParameter("name");
%>
<%=name %>
</body>
</html>
以上页面是把index.jsp页面中的name的值通过超链接传递到show.jsp页面中:
运行程序,我们发现:
在show.jsp页面中输出是 : ææ¯è°
这就出现了中文乱码,如何解决呢?
下面我介绍2种方法:
方法1:
(1) 在index.jsp页面中用jquery 或js 把超链接路径转换一下
代码如下:
<%@ page language="java" import="java.util.*" contentType="text/html;charset =utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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">
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script></head>
<body>
<%
String name="我是谁";
%>
<a href="show.jsp?name=<%=name %>">跳转</a>
</body>
</html>
<script>
var href=$("a").attr("href");
href = encodeURI(href);
href = encodeURI(href);
$("a").attr("href",href);
</script>
(2).在show.jsp页面中用java.net.URLDecoder.decode()方法进行转化.
代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.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">
</head>
<body>
<%
String name=request.getParameter("name");
name = java.net.URLDecoder.decode(name,"UTF-8");
%>
<%=name %>
</body>
</html>
再次运行程序,发现不在乱码了,问题也解决了 。但是,细心一点会发现 以上用到了jquery ,有点麻烦,有没有更好的方法呢?我们看另外一个方法:
二 只需在show.jsp 页面中把得到的name进行转码
1 index.jsp 页面代码:
<%@ page language="java" import="java.util.*" contentType="text/html;charset =utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<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">
<body>
<%
String name="我是谁";
%>
<a href="show.jsp?name=<%=name %>">跳转</a>
</body>
</html>
2 show.jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'show.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">
</head>
<body>
<%
String name=request.getParameter("name");
name= new String(name.getBytes("ISO-8859-1"),"utf-8");
%>
<%=name %>
</body>
</html>
运行,发现问题也解决了
建议大家使用第二种方法
谢谢大家!