Measure the test taker's knowledge about the request attribute usage in JSP.

Source: Internet
Author: User

1. Difference between request. getParameter () and request. getAttribute ()

1. request. getParameter () is achieved through the implementation of the container to obtain data transmitted through methods such as post and get, request. setAttribute () and getAttribute () are transferred only within the web Container, but only in the request processing phase.

2. The data transmitted by the request. getParameter () method is transmitted from the Web Client to the Web server, representing the HTTP request data. The request. getParameter () method returns String-type data.

The data transmitted by the request. setAttribute () and getAttribute () methods only exists in the Web container. The HttpServletRequest class has the setAttribute () method instead of the setParameter () method. Let's take an example. If two WEB pages are in a link, they are starting from 1. JSP link 2. in JSP, the link is 2. JSP can obtain request parameters through the getParameter () method.

Suppose that 1. JSP contains

 
 
  1. <Form Name="Form1" Method="Post" Action="2.jsp"> 
  2. Enter the User Name:<Input Type="Text" Name="Username"> 
  3. <Input Type="Submit" Name="Submit" Value="Submit"> 
  4. Form> 

The request parameter username is obtained through the request. getParameter ("username") method in 2. JSP:

<% String username = request. getParameter ("username"); %> however, if the two WEB pages are in a forwarding relationship, you can use the getAttribute () method to forward data from the source WEB shared request range, let's take an example. 1. JSP and 2.JSP

1. JSP wants to pass the current username to 2.jsp. How can this data be transmitted? Call the following setAttribute () method in 1.jsp:

 
 
  1. <%
  2. StringUsername=Request. GetParameter ("username ");
  3. Request. setAttribute ("username", username );
  4. %> 
  5. <Jsp: forward Page="2.jsp" /> 
  6. In 2. jsp, use the getAttribute () method to obtain the username:
  7. <% StringUsername= (String) request. getAttribute ("username"); %> 

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

Request. getAttribute ("nameOfObj") to obtain the Value of the control in the form of JSP page 1. In fact, the Object name and value in the Form Control are stored in a hash table. Therefore, the name of the Object is given here and the corresponding value is found in the hash table.

The request is used to pass values between different pages. when setAttribute (position, nameOfObj) is set. JSP to B. after a JSP request is passed, the request will lose the scope of its function. setAttribute (). When session. setAttribute () is used, this value is always stored in a process.

P.S.: JavaScript and JSP cannot pass values to each other because JavaScript runs on the client and JSP runs on the server. If you want to transfer parameters between them, you can set a hidden control in JSP and use its value in combination with the preceding usage to pass the required value.

  1. Brief Introduction to advanced operations on JSP Databases
  2. Interactive use of JSP data and JavaScript data
  3. Functions and principles of several encodings in JSP and Servlet
  4. How to solve the problem of garbled JSP page display
  5. Connect to various databases using JDBC in JSP

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.