Using Some Java in JSP


The code you use in scriptlets is just straight Java. For example, take a look at this scriptlet, which uses a Java if statement:

 <HTML>     <HEAD>         <TITLE>             Using Java in JSP         </TITLE>     </HEAD>     <BODY>         <H1>Using Java in JSP</H1>         <%             int temperature = 72;             if (temperature < 90 && temperature > 60) {                 out.println("Time for a picnic!");             }         %>     </BODY> </HTML> 

This scriptlet should send the text "Time for a picnic!" back to the browser. To check that out, enter it into a new JSP, ch05_02.jsp, and store it as webapps\chat\ch05_02.jsp. Then navigate to the URL http://localhost:8080/chat/ch05_02.jsp. As you see in Figure 5.4, this JSP does just what you'd expect.

Figure 5.4. Using Java in JSP.


There are some restrictions on what you can do in a JSP scriptlet, however. For example, you can't declare methods or new classes in a scriptlet. For that, you need a JSP declaration, which is surrounded with the markup <%! and %>. Here's an example that creates a new methodand a recursive one at thatwhich will calculate factorials (for example, the factorial of 6, written as 6!, is 6 x 5 x 4 x 3 x 2 x 1 = 72).

The factorial method is just a typical implementation of factorialsgiven a number, n, it multiples that number by the result of calling itself with a value of n-1, and it continues down, stage by stage, until it reaches 1, where it simply returns through all the successive stages and you end up with the factorial:

n x (n 1) x (n 2) … x 3 x 2 x 1

To use this method in JSP, you have to place it in a JSP declaration, enclosing it in the markup <%! and %>:

 <HTML>   <HEAD>     <TITLE>Using Recursion in JSP</TITLE>   </HEAD>   <BODY>     <H1>Using Recursion in JSP</H1>     <%!     int factorial(int n)     {         if (n == 1) {             return n;         }         else {             return n * factorial(n - 1);         }     }     %>     .     .     .   </BODY> </HTML> 

Now you can refer to the factorial method in scriptlets, as here, where this example is passing a value of 6 to that method:

 <HTML>   <HEAD>     <TITLE>Using Recursion in JSP</TITLE>   </HEAD>   <BODY>     <H1>Using Recursion in JSP</H1>     <%!     int factorial(int n)     {         if (n == 1) {             return n;         }         else {             return n * factorial(n - 1);         }     }     %>     <%         out.println("The factorial of 6 is " + factorial(6));     %>   </BODY> </HTML> 

To see this one running, enter the code into ch05_03.jsp and store it as webapps\chat\ch05_03.jsp. Then open that JSP in a browser, as shown in Figure 5.5. As you can see, the factorial method was declared and called, and the result is as expected.

Figure 5.5. Calling a method in JSP.


Note, however, that Java code inside a JSP declaration does not have access to the built-in JSP objects, such as out and request. Your code can still use those objects, however, if you simply pass them to your methods.

One of the big attractions of JSP is that you can mix Java code and HTML, and that's what happens in chat.jsp. For example, you could break up the earlier script that uses an if statement by ending the scriptlet and then starting it again in order to display some HTML. This version will only display the HTML <B>Time for a picnic!</B> if the value in temperature is less than 90 and greater than 60 (note that the scriptlet ends just before the HTML and then starts again in order to terminate the if statement with the closing curly brace):

 <HTML>     <HEAD>         <TITLE>             Using Java in JSP         </TITLE>     </HEAD>     <BODY>         <H1>Using Java in JSP</H1>         <%             int temperature = 72;             if (temperature < 90 && temperature > 60) {         %>                 <B>Time for a picnic!</B>         <%             }         %>     </BODY> </HTML> 

Besides just displaying text, chat.jsp also has to read text from the controls in the web page. So how do you read text from HTML controls?



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

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