In this excerpt from chapter 4 of Joel Murach's Java Servlets and JSP, you'll learn how to develop a web application that consists of HTML pages and JavaServer Pages (JSPs). As you will see, JSPs work fine as long as the amount of processing that's required for each page is limited. When you complete this chapter, you should be able to use JSPs to develop simple web applications of your own.
Now that you have a general idea of how JSPs are coded, you're ready to learn some specific skills for creating a JSP. To start, you need to know more about coding scriptlets and expressions.
How to Code Scriptlets and Expressions
Figure 4 summarizes the information you need for coding scriptlets and expressions within a JSP. To code a scriptlet, for example, you code Java statements that end with semicolons within the JSP scriptlet tags. To code an expression, you code any Java expression that evaluates to a string. Since primitive data types like integers or doubles are automatically converted to strings, you can also use expressions that evaluate to these data types.
Figure 4:How to code scriptlets and expressions
The syntax for a JSP scriptlet
<% Java statements %>
The syntax for a JSP expression
<%= any Java expression that can be converted to a string %>
The syntax for getting a parameter from the implicit request object
request.getParameter(parameterName);
Examples that use scriptlets and expressions
A scriptlet and expression that display the value of the firstName parameter
An expression that displays the value of the firstName parameter
The first name is <%= request.getParameter("firstName") %>.
Two scriptlets and an expression that display an HTML line 5 times
<% int numOfTimes = 1; while (numOfTimes <= 5){ %> <h1> This line is shown <%= numOfTimes %%> of 5 times in a JSP.</h1> <% numOfTimes++; } %>
Description
Within a scriptlet, you can code one or more complete Java statements. Because these statements are Java statements, you must end each one with a semicolon.
Within a JSP expression, you can code any Java expression that evaluates to a string. This includes Java expressions that evaluate to any of the primitive types, and it includes any object that has a toString method. Because a JSP expression is an expression, not a statement, you don't end it with a semicolon.
When you're coding a scriptlet or an expression, you can use any of the methods of the implicit request object. In this figure, only the getParameter method is used, but you'll learn about two more methods of the request object in the next figure.
In this figure, the first two examples show different ways that you can display the value of a parameter. The first example uses a scriptlet to return the value of the firstName parameter and store it in a String object. Then, this example uses an expression to display the value. In contrast, the second example uses an expression to display the value of the firstName parameter without creating the firstName object.
The last example in this figure shows how two scriptlets and an expression can be used to display an HTML line five times while a Java variable within the HTML line counts from 1 to 5. Here, the first JSP scriptlet contains the code that begins a while loop. Then, a line of HTML code uses a JSP expression to display the current value of the counter for the loop. And finally, the second scriptlet contains the code that ends the loop.
Remember: this is from chapter four ofJoel Murach's Java Servlets and JSP (Mike Murach & Associates, ISBN 1890774189, 2003). Grab a copy at your favorite book store today!