Four Methods for passing parameters in jsp and four methods for passing parameters in jsp

Source: Internet
Author: User

Four Methods for passing parameters in jsp and four methods for passing parameters in jsp

Today, the teacher talked about four methods for passing parameters in jsp. I think it is good to summarize them for later use!

1. form

2. request. setAttribute (); and request. getAttribute ();

3. hyperlink: <a herf = "index. jsp "? A = a & B = B & c = c> name </a>

4. <jsp: param>

The following are examples:

1. form

Form. jsp:

<% @ Page contentType = "text/html; charset = GB2312" %> 

Result. jsp:

<% @ Page language = "java" import = "java. util. * "pageEncoding =" GB2312 "%> 

Note:Form submission method is get, and Chinese characters are garbled during parameter passing. A simple solution is to convert the received string into a byte array first, use String to construct a new encoding format String, such:

1. String name = request. getParameter ("name ");

2. name = new String (name. getBytes ("iso-8859-1"), "GB2312 ");

If the form is submitted in post mode, you can use request. setCharacterEncoding ("GB2312"); To solve the garbled problem.

Why is Chinese garbled characters? Because the default system encoding method of the Tomcat server is iso-8859-1, when you pass parameters to the server, the encoding method of the default iso-8859-1 is used, but when the server returns information to you, is the encoding method set in the page command, such as: <% @ page language = "Java" import = "java. util. * "pageEncoding =" GB2312 "%>. In this way, two encoding methods are mixed, so garbled characters are displayed. The solution is to unify the transmission and receipt encoding methods.

2. request. setAttribute () and request. getAttribute ()

Set. jsp:

<% @ Page contentType = "text/html; charset = GB2312" %> 

Get. jsp:

<% @ Page contentType = "text/html; charset = GB2312" %> 

Request. setAttribute () and request. getAttribute () are implemented in combination with the <jsp: forward> or include command.

3. hyperlink: <a herf = "index. jsp? A = a & B = B & c = c "> name </a>

Href. jsp:

<% @ Page contentType = "text/html; charset = GB2312" %> 

GetHref. jsp:

    <%@page contentType="text/html; charset=GB2312"%>     

This method of passing parameters is similar to the get method of form. It is a parameter passed through the address bar, and its garbled solution is the same as the get method of form.

4. <jsp: param>

Param. jsp:

<% @ Page contentType = "text/html; charset = GB2312" %> 

GetParam. jsp:

    <%@page contentType="text/html; charset=GB2312"%>     

A strange problem is found here. It is still caused by Chinese garbled characters. In the form example, if the transfer method is post, you only need to set the request Encoding Method on the page for receiving parameters, that is, request. setCharacterEncoding ("GB2312"); note that if the sentence is placed in the form on the page receiving parameters, it does not work and is still garbled. In this example, in order to keep the passed parameters ungarbled, request is executed. setCharacterEncoding ("GB2312"); on the sending parameters page, Chinese characters are displayed normally and placed on the receiving parameters page, which does not work. Maybe this is the difference between <jsp: param> and form passing parameters. Why is the difference? It may be because the parameters in the form are transmitted from the client to the server and need to go through a request packaging process. However, the parameters passed by <jsp: param> are themselves on the server, there is no need to go through the process from the client to the server, but is the transfer of parameters in the server the case? I don't know this question! Actually, knowledge is an expanded circle. The more you know, the more you don't know! Work hard!

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

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.