Page #477 (35.4. JSP Scripting Constructs)

 
[Page 1213 ( continued )]

35.5. Predefined Variables

You can use variables in JSP. For convenience, JSP provides eight predefined variables from the servlet environment that can be used with JSP expressions and scriptlets. These variables are also known as JSP implicit objects .

  • request represents the client's request, which is an instance of HttpServletRequest . You can use it to access request parameters and HTTP headers, such as cookies and hostname.

  • response represents the servlet's response, which is an instance of HttpServlet-Response . You can use it to set response type and send output to the client.

  • out represents the character output stream, which is an instance of PrintWriter obtained from response.getWriter() . You can use it to send character content to the client.

  • session represents the HttpSession object associated with the request, obtained from request.getSession() .


    [Page 1214]
  • application represents the ServletContext object for storing persistent data for all clients . The difference between session and application is that session is tied to one client, but application is for all clients to share persistent data.

  • config represents the ServletConfig object for the page.

  • pageContext represents the PageContext object. PageContext is a new class introduced in JSP to give a central point of access to many page attributes.

  • page is an alternative to this .

As an example, let us write an HTML page that prompts the user to enter loan amount, annual interest rate, and number of years , as shown in Figure 35.4(a). Clicking the Compute Loan Payment button invokes a JSP to compute and display the monthly and total loan payments, as shown in Figure 35.4(b).

Figure 35.4. The JSP computes the loan payments.

The HTML file is named ComputeLoan.html (Listing 35.2). The JSP file is named ComputeLoan.jsp (Listing 35.3). Store both files in c:\jakarta-tomcat-5.5.9\webapps\liangweb .

Listing 35.2. ComputeLoan.html
 1  <! ComputeLoan.html >  2   <html>   3   <head>   4   <title>   ComputeLoan   </title>   5   </head>   6   <body>   7    <form method   =   "get"   action   =   "/liangweb/ComputeLoan.jsp"   >  8     Compute Loan Payment   <br />   9     Loan Amount 10   <input type   =   "text"   name   =   "loanAmount"   /><br />   11     Annual Interest Rate 12   <input type   =   "text"   name   =   "annualInterestRate"   /><br />   13     Number of Years 14   <input type   =   "text"   name   =   "numberOfYears"   size   =   "3"   /><br />   15   <p><input type   =   "submit"   name   =   "Submit"   16   value   =   "Compute Loan Payment"   />   17   <input type   =   "reset"   value   =   "Reset"   /></p>   18    </form>    19   </body>   20   </html>   


[Page 1215]
Listing 35.3. ComputeLoan.jsp
 1  <!-- ComputeLoan.jsp -->  2   <html>   3   <head>   4   <title   >ComputeLoan   </title>   5   </head>   6   <body>   7   <% double   loanAmount = Double.parseDouble(8  request.  getParameter(   "loanAmount"   ));  9   double   annualInterestRate = Double.parseDouble(10  request.  getParameter(   "annualInterestRate"   )); 11   double   numberOfYears = Integer.parseInt(12  request.  getParameter(   "numberOfYears"   )); 13   double   monthlyInterestRate = annualInterestRate /   1200   ; 14   double   monthlyPayment = loanAmount * monthlyInterestRate / 15        (   1   -   1   / Math.pow(1 + monthlyInterestRate, numberOfYears *   12   )); 16   double   totalPayment = monthlyPayment * numberOfYears *   12   ;   %>   17    Loan Amount:    <%=   loanAmount   %>      <br />   18    Annual Interest Rate:   <%=   annualInterestRate   %><br />   19    Number of Years:   <%=   numberOfYears   %><br />   20   <b>   Monthly Payment:   <%=   monthlyPayment   %><br />   21    Total Payment:   <%=   totalPayment   %><br /></b>   22   </body>   23   </html>   

ComputeLoan.html is displayed first to prompt the user to enter the loan amount, annual interest rate, and number of years. Since this file does not contain any JSP elements, it is named with an .html extension as a regular HTML file.

http://localhost:8080/liangweb/ComputeLoan.jsp is invoked upon clicking the Compute Loan Payment button in the HTML form. The JSP page obtains the parameter values using the predefined variable request in lines 7 “12 and computes monthly payment and total payment in lines 13 “16. The formula for computing monthly payment is given in §2.12.1, "Example: Computing Loan Payments."

What is wrong if the JSP scriptlet <% in line 7 is replaced by the JSP declaration <%! ? The predefined variables (e.g., request , response , out ) correspond to local variables defined in the servlet methods doGet and doPost . They must appear in JSP scriptlets, not in JSP declarations.

Tip

ComputeLoan.jsp can also be invoked using the following query string: http://localhost:8080/liangweb/ComputeLoan.jsp?loanAmount=10000&annualInterestRate=6&numberOfYears=15 .


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net