| JSP Components |
| |
| Writing the JSP File |
| |
| Here we briefly describes the JSP tags in the Account.jsp file. These tags are marked by bold font in the full listing of Account.jsp included at the end of this section. |
| |
| The first JSP tag in the Account.jsp file specifies the JavaBeans component. The jsp:useBean tag creates a JavaBeans component by instantiating the AccountBean class, names the component accountBean, and indicates that the component will be available for the current HTTP session. |
| |
| Unlike the other JSP tags, the jsp:useBean tag is executed just once during the session-- when the end-user first accesses the Account.jsp page: |
| <jsp:useBean id="accountBean" scope="session" class="AccountBean" /> |
| |
| The jsp:setProperty tag sets all of the property values in the accountBean to the parameters passed by the HTML form. |
| For example, if the end-user selects the Debit radio button and clicks Submit, the action parameter is set to debit. This parameter value is sent to the web server, which assigns it to the action property value of the accountBean. |
| |
| The asterisk in the tag indicates that all properties will be set: |
| <jsp:setProperty name="accountBean" property="*" /> |
| |
| JSP scripting elements are enclosed as follows: |
| <% element %>. |
| |
| The first scripting element declares the status variable: |
| <%! String status; %> |
| |
| The next scripting element invokes processRequest method of the accountBean object. |
| |
| This method checks the action property value and invokes methods on the entity bean: |
| <% status = accountBean.processRequest(); %> |
| |
| Near the bottom of the Account.jsp file, the status variable returned by the processRequest method is displayed: |
| <h3><b>Status :</b></h3> <%= status %> |
| |
| In the HTML form tag, the action parameter indicates that the Account.jsp page is to be executed when the end-user clicks the form's Submit button. |
| ( the web server transforms the Account.jsp page into a servlet which it then executes.) |
| |
| Except for the jsp:useBean tag, every JSP tag and scripting element in the Account.jsp page is executed whenever the end-user clicks Submit: |
| <form method=POST action=Account.jsp> |
| |
| Each jsp:getProperty tag fetches a property value from the accountBean. For example, the second jsp:getProperty tag retrieves the balance property from the accountBean. The balance is displayed in the text field of the HTML form. The next time the end-user clicks the Submit button, the value in the text field is sent to the web server as the balance parameter. The jsp:setProperty tag causes the web server to assign this parameter to the balance property of the accountBean. |
| |
| Here is the jsp:getProperty tag for the balance property: |
| |
<INPUT type=text name="balance" size="8"
value="<jsp:getProperty name="accountBean" property="balance" />">
The full listing for the Account.jsp file follows:
<html>
<jsp:useBean id="accountBean" scope="session" class="AccountBean" />
<jsp:setProperty name="accountBean" property="*" />
<%! String status; %>
<% status = accountBean.processRequest(); %>
<html>
<head>
<title>Account JSP</title>
</head>
<body background="back.gif">
<font size = 5 color="#CC0000">
<h1><b><center>Account JSP Example</center></b></h1>
<hr>
<br>
<form method=POST action=Account.jsp>
<br>
<br>Account ID
<INPUT type=text name="id" size="8"
value="<jsp:getProperty name="accountBean" property="id" />" >
Balance
<INPUT type=text name="balance" size="8"
value="<jsp:getProperty name="accountBean" property="balance" />" >
<br>
First Name
<INPUT type=text name="firstName" size="8"
value="<jsp:getProperty name="accountBean" property="firstName" />">
Last Name
<INPUT type=text name="lastName" "size=8"
Value="<jsp:getProperty name="accountBean" property="lastName" />"
><br>
<br>
<h2><b>Action :</b></h2>
<INPUT type="radio" name="action" value="find">Find
<INPUT type="radio" name="action" value="create">Create
<INPUT type="radio" name="action" value="debit">Debit
<INPUT type="radio" name="action" value="credit">Credit
<br>
<br>Amount <INPUT type=text name="amount"><br>
<INPUT type=submit name="submit" value="Submit">
</form>
</FONT>
</body>
</html>
<hr>
<h3><b>Status :</b></h3> <%= status %>
</html> |
| |
| |
|
| |
| |