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.