Java Tutorial - Java Script :
JSP
Using Scriptlets:
You also can use Java statements in JSP—calling methods, assigning values to variables, creating conditional statements, and so on. These statements begin with the <% tag and end with the %> tag. More than one statement can be enclosed within these tags. Statements that appear inside a page are called scriptlets. You can use any of the servlet variables that were available for expressions. Listing 21.6 contains the code for shop-for-books.jsp, a web page that displays a list of books, with hyperlinks to each book’s page at an online bookstore.
This page includes a form at the bottom of the page that lets users choose which bookstore they like to use for online shopping. In line 29, the form is being submitted to the URL of the page. Because pages are actually servlets, they can also receive form data that’s sent by post or get. This page uses the store field to hold “Amazon” if Amazon.com is the preferred store and “BN” if Barnes & Noble is the preferred store. One thing to note as you test the page is how the radio buttons on the form always match the store you’ve chosen. This occurs because of expressions that appear on lines 29 and 31. Here’s one of those expressions:
<%= (store.equals(“Amazon”) ? “ checked” : “”) %>
This expression uses the ternary operator with the conditional store.equals(“Amazon”). If this condition is true, the word “checked” is the value of the expression. Otherwise, an empty string (“”) is the value. The value of expressions is displayed as part of the JSP page. Figure 21.6 shows what this page looks like in a web browser.
Using Declarations:
Another element you can insert into a JSP page is a declaration, which is a statement that sets up a variable or method that will be defined in the page when it’s compiled into a servlet. This feature is primarily used in conjunction with expressions and servlets. Declarations are surrounded by <%! and %> tags, as in the following example:
<!% boolean noCookie = true %>
<!% String userName = “New user” %>
These declarations create two instance variables: noCookie and userName. When the JSP page is compiled into a servlet, these variables will be part of the definition of that class. Listing 21.7 contains a page that uses a declaration to present a counter.
Before you can try this page, you need to create a helper class that’s called by statements in lines 8, 12, 13, and 18 of the page. The Counter class in Listing 21.8 represents a web counter that tallies each hit to a page.
After you compile this class successfully, it should be stored in a WEB-INF\classes\ counter subfolder in the same part of the webapps hierarchy as counter.jsp. For example, if the JSP page is in webapps\examples\jsp, the class file should be saved in webapps\examples\WEB-INF\classes\counter. The Counter class loads an integer value from a file called counter.dat that is stored on the web server. The getCount() method retrieves the current value of the counter, and the setCount(int) method sets the current value. After the value is set, it’s saved to the file so that the counter continues to incrementally increase. Figure 21.7 shows counter.jsp loaded in a web browser.
