Methods for passing values between JSP pages

Source: Internet
Author: User

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:include page="Relative URL"> 
  2.  
  3. < jsp:param name="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:forward page="Relative URL"> 
  2.  
  3. < jsp:param name="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.        < form method=get action=2.jsp> 
  3.        what's your name< input type=text name=username> 
  4.        < input type=submit value=submit> 
  5.        < /form> 
  6. < /html> 

4. 2.jsp

 
 
  1. < html> 
  2.  
  3.        < form method=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 hobby< input type=text name=hobby> 
  10.        < input type=submit value=submit> 
  11.        < /form> 
  12. < /html> 
  13.  
  14.     

5. 3.jsp

 
 
  1. < html> 
  2.        your name is:< %=session.getAttribute("username")%> 
  3.        < br> 
  4.        your hobby is:< %=request.getParameter("hobby")%> 
  5.        < br> 
  6.        your password is:< %=request.getParameter("pass")%> 
  7.        < br> 
  8.        < /form> 
  9. < /html> 

This article is from the javaeye blog: Passing parameters between JSP pages

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.