Pass the JSP page value, and pass the jsp page

Source: Internet
Author: User

Pass the JSP page value, and pass the jsp page

Parameters passing between JSP pages is a feature that is frequently used. Sometimes, parameters passing between multiple JSP pages are required. The following describes the implementation methods.

(1) Add

For example, <a href = "thexuan. jsp? Action = transparams & detail = directe "> directly passing parameters </a>

When using response. sendRedirect for page redirection, you can also use the following code:

Response. sendRedirect ("thexuan. jsp? Action = transparams & detail = directe "), which can be obtained by request. getParameter (name ).

(2) jsp: param

It allows you to pass parameters to the include page on the home page, as follows:

  1. < Jsp: includePage = "Relative URL"> 
  2.  
  3. < Jsp: paramName = "param name" value = "paramvalue"/> 
  4.  
  5. </Jsp: include> 

You can also pass parameters when using the jsp: forward action for page Jump, as follows:

  1. < Jsp: forwardPage = "Relative URL"> 
  2.  
  3. < Jsp: paramName = "paramname" value = "paramvalue"/> 

</Jsp: forward> in this way, parameters can be obtained through request. getParameter (name), which is the same as common form parameters.

(3) set the session and request

The displayed parameters are placed in the session and request to pass the parameters.

  1. Session. setAttribute (name, value );
  2.  
  3. Request. setAttribute (name, value)

Take the following parameters:

  1. Value = (value className) session. getAttribute (name );
  2.  
  3. Value = (value className) request. getAttribute (name );
  4.  

You must have noticed that the type conversion is performed when the parameter is obtained, because the attributes of the objects placed in the session and request are treated as Java. lang. if the Object type is not converted, The classcastexception exception is reported when the value is directly paid.

Passing parameters between multiple JSP pages

1. How to pass parameters between multiple JSP pages? Use the JSP built-in scope object session. Using its two methods setAttribute (), getAttribute ()

2. The following example implements the function of passing parameters of the first JSP page to the third page.

3. The Code is as follows: 1.jsp

  1. < Html> 
  2. < FormMethod = get action = 2.jsp> 
  3. What's your name< InputType = text name = username> 
  4. < InputType = submit value = submit> 
  5. </Form> 
  6. </Html> 

4. 2.jsp

  1. < Html> 
  2.  
  3. < FormMethod = post action = "3.jsp? Pass = 11"> 
  4. <%
  5. String name = request. getParameter ("username ");
  6. Session. setAttribute ("username", name );
  7. %> 
  8. Your name is:<% = Request. getParameter ("username") %> 
  9. < Br>What's your holobby< InputType = text name = holobby> 
  10. < InputType = submit value = submit> 
  11. </Form> 
  12. </Html> 
  13.  

5. 3.jsp

  1. < Html> 
  2. Your name is:<% = Session. getAttribute ("username") %> 
  3. < Br> 
  4. Your holobby is:<% = Request. getParameter ("holobby") %> 
  5. < Br> 
  6. Your password is:<% = Request. getParameter ("pass") %> 
  7. < Br> 
  8. </Form> 
  9. </Html> 

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.