前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >url,sendRedirect当中有中文

url,sendRedirect当中有中文

作者头像
马克java社区
修改2021-10-11 10:25:09
9840
修改2021-10-11 10:25:09
举报
文章被收录于专栏:java大数据java大数据java大数据

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){

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) {

}

}

}

更多请看:https://blog.csdn.net/qq_44638460/article/details/104157305

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档