url,sendRedirect当中有中文

3.url,sendRedirect当中有中文
中文当想出现在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){

更多请见:http://www.mark-to-win.com/index.html?content=Jsp/jspUrl.html&chapter=Jsp/jsp8_web.html#UrlHasChinese

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值