jsp超链接中文乱码

对于初步接触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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值