Data sharing between JSP and Servlet (data transmission and data calling

Source: Internet
Author: User
Data sharing between JSP and Servlet (data transmission and data calling) (1) the most common way for JSP (PAGE) to transmit data to servlet: transmit data using form parameters or URL hyperlink parameters (previously mentioned ). (2) servlet transfers data to JSP (PAGE) to call jsp from servlet. There are two call methods: forwarding and redirection (the last time we talked about how these two methods are implemented and specific syntaxes), the data transmission methods vary based on different call (forwarding and redirection) methods. (3) using redirection to implement servlet calling JSP on the JSP file, some java Code , You must put the Java code in Program . If a return value of a variable or method exists, it must be displayed at the specified position, and the expression can be used. A: Use URL hyperlink parameters to transmit JSP page data to servlet. On the redirected JSP page, add the hyperlink parameter information. Disadvantage: It is limited to transmitting simple and a small amount of information (4 kb), and cannot be used to transmit set information. B: Using session to transfer JSP page information to servlet is called session. It is a working mechanism for data sharing between different Web components (such as JSP and Servlet. Representative: A group of connections between users and applications. Step 1: Get the session, and the session does not need to be new. For example, session ["user"] = A value or null. Step 2: add the data to be passed (either a string or a value or an object or a set) to the session as an attribute. Session. setattribute ("attribute name", attribute value) // pay attention to the syntax and understand it as an attribute added to the session. // Set the attribute value, including key (attribute name) and value (attribute value ). // Note: the attribute value must be the object type. Step 3: retrieve the attribute value from the session in servlet Based on the attribute name and obtain the attribute value based on the attribute name: object value = session. getattribute ("attribute name"); // not only restricted, but can be obtained in servlet in this way in all Java programs. (4) using forwarding to implement servlet call JSpA: Using URL hyperlink parameters to implement data transmission B: Using request attributes to implement data transmission supplement: Redirection: response. sendredirect ("redirected page"); Forward: request. getrequestdispatcher ("JSP page "). forward (request, response); // The parameters in forward are certain. Recommendation: When page redirection is implemented using redirection, we recommend that you use session for data transmission. When page redirection is implemented using forwarding, we recommend that you use the request attribute for data transmission.

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.