Summary of the pass-through methods between JSP pages, Summary of jsp pages

Source: Internet
Author: User
Tags set cookie

Summary of the pass-through methods between JSP pages, Summary of jsp pages

Preface

Passing parameters between JSP pages is often required in projects. This is a basic web skill. Try to summarize the various methods and select the most appropriate method when necessary. Let's take a look at the detailed introduction:

1. append parameters after URL link

<A href = "next. jsp? ParamA = A & paramB = B... "rel =" external nofollow "> append parameters after URL </a> <jsp: include page =" next. jsp "> <jsp: param name =" paramA "value =" A "/> </jsp: include> <jsp: forward page =" next. jsp "> <jsp: param name =" paramA "value =" B "/> </jsp: forward> response. sendRedirect ("next. jsp? ParamA = A & paramB = B... ") window. location =" next. jsp? ParamA = A & paramB = B ..."

When the above code is executed, parameters will be included in the next. jsp page.

You can obtain the corresponding parameters on the next. jsp page as follows:

// Embedded java code <% String paramA = request. getParameter ("paramA"); %> // if EL {param. paramA} is introduced}

Advantages:Simple and multi-browser support (no browser does not support URLs ).

Disadvantages:

1) The transmitted data can only be strings, which have certain restrictions on the data type and size;

2) The transmitted data value is displayed in the address bar of the browser, and the security level is low.

2. Form

<Form action = "next. jsp "method =" post "> <input type =" text "name =" paramA "value =" A "> <input type =" hidden "name =" paramB "value = "B"> <input type = "submit" value = "submit"> </form>

The method for obtaining corresponding parameters on the next. jsp page is similar to (1.

Advantages:

1) simplicity and multi-browser support (no browser also supports form );

2) the amount of data that can be submitted is much larger than the URL method;

3) The transmitted value is displayed in the address bar of the browser, but the parameter list can be constructed from the source code of the page by a black means;

Disadvantages:

1) The transmitted data can only be strings, which has certain restrictions on data types;

3. Set Cookie

Using the client's authentication creden, a small Cookie can also be used to transmit the value of the JSP page.

<% Cookie c = new Cookie ("paramA", "A"); c. setMaxAge (60*60); // cookie is valid for 1 minute response. addCookie (c); // Add the cookie to the http response. %>

To read the cookie on the next. jsp page, you must callrequest.getCookies() Method to obtainjavax.servlet.http.Cookie Object array.

Then traverse the array and usegetName()Method andgetValue()Method to obtain the name and value of each cookie.

// The embedded java code <% Cookie cookie = null; // obtain the Cookie data, which is an array of cookies [] cookies = request. getCookies (); if (cookies! = Null) {for (int I = 0; I <cookies. length; I ++) {cookie = cookies [I]; out. print ("parameter name:" + cookie. getName (); out. print ("<br>"); out. print ("parameter value:" + URLDecoder. decode (cookie. getValue (), "UTF-8") + "<br>"); out. print ("------------------------------------ <br>") ;}} else {out. println ("

Advantages:

1) the Cookie value can be persistent. Even if the client machine is disabled, the value can still be obtained next time;

2) Cookies can help the server store multiple status information, but do not need to allocate storage resources on the server side, reducing the burden on the server side.

Disadvantages:

1) Although the security is much higher than that of URL and Form, there are also black means to obtain client cookies and expose customer information.

4. Set the Session

I personally think that the session and cookie are one on the server and the other on the client.

After adding key-value pairs to them, it not only provides transfer between pages, but also is a data sharing solution.

 <%   session.setAttribute("paramA","A");  response.sendRedirect("next.jsp");  %>

Method for reading session in next. jsp:

// Embedded java fragment <% = session. getAttribute ("paramA") %> // EL acquisition method {session. paramA}

For the advantages and disadvantages of Session, refer to Cookie.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.