How to invoke a variable in another JSP page in one JSP page

Source: Internet
Author: User

In JSP learning, often need to call in a JSP page in another JSP page variables, the following days of learning, summed up.

There are several ways to invoke variable calls between JSP pages:

1, through the JSP built-in object-request object to obtain the parameter:

(1) to pass the reference through the hyperlink:

Example: The value of I in the a.jsp is passed to the b.jsp:

The core code in the A.jsp page is:

<a href= "B.jsp?i=1" > Parameter </a> (Note: You can also use JSP expressions when assigning to I, such as i=<% variable name%>)

The core code in the B.jsp page is:

<%string J=request.getparameter ("I"); %>

<%=j%>

(2) Through the table conveys parameters:

Example: Transfer the value of name from the form in the a.jsp to the b.jsp:

The core code in the A.jsp page is:

<form action= "b.jsp" method= "POST" >  
      
     <ul>  
      
           <li><input type= "text" name= "username" >  
      
           <li>w<input type= "Submit" name= "Submission" value= "submitted" >  
      
     <ul>  
      
</form>

The core code in the B.jsp page is;

<%string Getusername=request.getparameter ("username");>

<%=getusername%>

(3) pass through the hidden fields of the form:

Example: Transfer the variables defined in the a.jsp to the b.jsp;

The core code in A.JSP is:

<%!int a=4; %>  
<form action= "b.jsp" method= "POST" >  
      <input type= "Hide" name= "B" value=<%a%>>  
      
      <input type= "Submit" value= "submitted" >  
      
</form>

The core code in B.JSP is:

<%int c= (Integer) request.getattribute ("B"); %>

This column more highlights: http://www.bianceng.cn/webkf/JSP/

(The previous code is either the <%string c=request.getattribute ("C"). ToString (); %> et cetera, type conversion pass Java)
<%=c%>

Run Result: The value of the variable a=4 through form hidden field B to C in b.jsp.

2, obtain form information through JavaBean:

Example: Use JavaBean to obtain user login form information:

The core code in LOGIN.JSP is:

<form action= "deal.jsp" method= "POST" >  
      
       <ul>  
      
            <li> user name: <input type= "text" name= "username ">  
      
            <li> Password: <input type=" password "name=" password ">  
      
            <li><input type=" Submit "Name=" Submit "value=" Login >  
      
       </ul>  
      
</form>

The core code in JavaBean is:

package com; public  
      
class userbean{  
      
       private String username;  
      
       private String password;  
      
       Public String GetUserName () {return  
      
              username;  
      
       }  
      
       public void Setusername (String username) {  
      
              this.username=username;   
      
       }  
      
       Public String GetPassword () {return  
      
              password;  
      
       }  
      
       public void SetPassword (String password) {  
      
              This.password=password  
      
       }}  
      
 

The core code in DEAL.JSP is:

<jsp:usebean id= "user" class= "com. UserBean ">  
      
<jsp:setproperty name=" user "property=" * ">  
      
<%string (); %>  
      
<%string Password=user.getpassword ();%>

The user named:<%=username%><br>

The password obtained is:<%=password%>

Where the asterisk in the <jsp:setproperty name= "user" property= "*" > in deal.jsp represents the assignment of all attributes with the same name, and, of course, can optionally assign values to properties: <jsp: SetProperty name= "user" property= "username" >, <jsp:setproperty name= "user" property= "password" > etc.

Of course, there must be better and more methods, look forward to master enlighten.

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.