url,sendRedirect当中有中文

url,sendRedirect当中有中文
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
中文当想出现在url当中,或通过网络http header或request的parameter或response传送时,得需要变成iso格式传送,到目的地后,再用GBK转换一下,人才能看懂。



例 1.3.1

jsp5.jsp:

<%@ page contentType="text/html; charset=GBK" %>
<html>
<body>
<h1>
<a href="MarkToWinServlet?name=马克-to-win">ok</a>
<a href="show.jsp?name=马克-to-win">ok</a>
</h1>
<form action="MarkToWinServlet" method="POST">
<input type="submit"/>
</form>
</body>
</html>





package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHello1 extends HttpServlet {
      private static final String CONTENT_TYPE = "text/html; charset=GBK";
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws
          ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
    //    out.println("马克-to-win");
        String str="马克-to-win";
    /* you can use the following either one to encode.
str.getBytes("GBK"),用GBK方式把字符串变成数组,

    public static String encode(String s,String enc)   : Translates a string into
     application/x-www-form-urlencoded format using a specific encoding scheme.
         */
     //  str=java.net.URLEncoder.encode(str,"GBK");
/* 中文当想出现在url当中,或通过网络传送时,得需要变成iso格式传送,URLEncoder方法的用意和下面一模一样,只不过地址栏里人眼看不懂,其他都一样。这里不能变成UTF-8,也许我们机器缺省是GBK,所以eclipse用GBK等。*/      
        str = new String(str.getBytes("GBK"), "iso-8859-1");
        response.sendRedirect("home.jsp?username="+str);
      }
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
/*中文当想出现在url当中,或通过网络传送时,得需要变成iso格式传送,*/        
            String str = request.getParameter("name");
            System.out.println(str);
            if(str != null){
              try {
                str = new String(str.getBytes("ISO-8859-1"), "GBK");
                System.out.println(str);
/*下句话不能少, 否则response不是中文*/              
                response.setContentType(CONTENT_TYPE);
                response.getWriter().println("response STR"+str);
              }
              catch (UnsupportedEncodingException ex) {
              }
            }
      }
}





show.jsp:


<%@ page contentType="text/html; charset=GBK" %>
<html>
<body >
<h1>
<%String str = request.getParameter("name");
    //System.out.println(ua.getName());
    if(str != null){
        str = new String(str.getBytes("ISO-8859-1"), "GBK");
        System.out.println("STR"+str);
      out.println(str);
    }
%>
</h1>
</body>
</html>



home.jsp:


<%@ page contentType="text/html; charset=GBK" %>
<html>
<body >
<h1>
JSP
<%
//request.setCharacterEncoding("GBK");
String str=request.getParameter("username");
str = new String(str.getBytes("ISO-8859-1"), "GBK");
out.println("username is"+str);
System.out.println("username is"+str);%>
</h1>
</body>
</html>




此项目从jsp5.jsp开始运行。