Redirect the sample code in Chinese and the sample code in Chinese displayed on the jump page with Chinese Parameters

Source: Internet
Author: User

Redirect the sample code in Chinese and the sample code in Chinese displayed on the jump page with Chinese Parameters

Recently, I found a problem. When I access servlet and use redirection to jump to the page with Chinese parameters, I found that the address bar could not find the parameter as Chinese. For a moment, I will discuss the cause of Garbled text: the contents in the URL address bar are generally ISO-8859-1 encoded, so, if we use redirection, it is to re-modify the content in the address bar (URL ), so if we want to add Chinese parameters to the URl, We must encode the Chinese in ISO-8859-1 before it can be displayed on the address bar, otherwise, the Chinese characters cannot be found, or the Chinese characters in the address bar are garbled. You will understand it without reading the code.

I have posted my code below. If you don't understand it, you can communicate with me:

Create two jsp files under WebRoot:

(1) index. jsp

(2) success. jsp

Create a servlet folder under src and create a servlet named UserSerlvet. java.

The following code continues:

(1) index. jsp code:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %> <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 

(2) success. jsp code:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

(3) web. xml code:

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <display-name></display-name>  <servlet>    <servlet-name>UserSerlvet</servlet-name>    <servlet-class>servlet.UserSerlvet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>UserSerlvet</servlet-name>    <url-pattern>/servlet/UserSerlvet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

(4) UserSerlvet. java code in src/servlet:

Package servlet; import java. io. IOException; import java. io. printWriter; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class UserSerlvet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {do Post (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/*** note that the url in the browser Get request can only be ISO-8859-1 encoding, if * requires a parameter after the url, the data must be converted to ISO-8859-1 encoding before the URL can be followed by a * Chinese parameter. To the page, be sure to convert the ISO-8859-1 to UTF-8 to show chinese. * // Method 1: In the program, the default encoding is utf8 or GBK, which is generally utf8. In this case, the Chinese character must be used as a parameter to implement redirection transmission, be sure to switch to ISO-8859-1 can pass successful! // String name = new String ("Zhang San's snacks ". getBytes ("UTF-8"), "iso-8859-1"); // Method 2: Get the ISO-8859-1 encoding from the page, without transcoding, direct redirect to the page, decodes String name = request in UTF-8. getParameter ("name"); System. out. println (name); // The console is garbled, at this time it is a ISO-8859-1, so the display must be garbled response. sendRedirect (".. /success. jsp? Name = "+ name );}}

The above is all the code. You can directly copy, test, and communicate with each other if any problem occurs.

This article is self-tested. Feel free to test it !!

 


In java web, how does one output a paragraph on this page after redirecting to an html page?

The attributes in the jsp request cannot be obtained on the html page! The parameter can only pass through the url to the html page during redirection.
Then you can get parameters through js on the html page. However, you need to pay attention to Chinese garbled characters, and this method is not particularly good.

<Script type = "text/javascript">
Function getpara () // function for obtaining Parameters
{
Var url = document. URL;
Var para = "";
If (url. lastIndexOf ("? ")> 0)
{
Para = url. substring (url. lastIndexOf ("? ") + 1, url. length );
Var arr = para. split ("&");
Para = "";
For (var I = 0; I <arr. length; I ++)
{
Para + = "no." + (I + 1) + "parameter> name:" + arr [I]. split ("=") [0];
Para + = "value:" + arr [I]. split ("=") [1] + "<br> ";
}
Document. write (para );
}
Else
{
Document. write ("No parameter! ");
}

}
Getpara (); // executes the Function
</Script>

In the page Jump process, how to implement url redirection, or how to hide the parameters passed by the page, regardless of Encryption

Post is used for transmission. get is a URL-based parameter. It is best to use ajax post to pass Parameters and then use js for redirection (do not use Microsoft's ajax control ).
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.