Call the variables on another JSP page on the JSP page

Source: Internet
Author: User

In jsp learning, variables on the other jsp page are often called on one jsp page. The following is a summary of the learning over the past few days.


There are multiple methods to call variables between jsp pages:

1. Get parameters through the built-in jsp object-request object:

(1) passing parameters through hyperlinks:

For example, transfer the I value in a. jsp to B. jsp:

The core code on the. jsp page is:

Passing parameters (Note: jsp expressions can also be used when I is assigned a value, for example, I = <% variable name %>)

The core code on the B. jsp page is:

<% String j = request. getParameter ("I"); %>

<% = J %>

(2) passing parameters through the form:

For example, transfer the name value in the form in a. jsp to B. jsp:

The core code on the. jsp page is:

The core code on the B. jsp page is;

<% String getUserName = request. getParameter ("username");>

<% = GetUserName %>

(3) passing parameters through hidden fields in the form:

For example, transfer the variables defined in a. jsp to B. jsp;

The core code in a. jsp is:

<%! Int a = 4; %>

The core code in B. jsp is:

<% Int c = (Integer) request. getAttribute ("B"); %>

(The code in the previous section is <% String c = request. getAttribute ("c"). toString (); %>. The type conversion is the same as that in java)
<% = C %>

Running result: the value of variable a = 4 is transmitted to c in B. jsp through form hiding field B.

2. Obtain form information through JavaBean:

For example, Use JavaBean to obtain the user login form information:

The core code in Login. jsp is:

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:

<% String username = user. getUserName (); %>

<% String password = user. getPassword (); %>

The username obtained is: <% = username %>

The obtained password is: <% = password %>

Where, in Deal. jsp The asterisk in is used to assign values to all attributes with the same name. Of course, you can also assign values to attributes selectively: , .


Of course, there must be more and more methods, and we hope that experts will give us some advice.




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.