• No results found

JSP Basics

In document Java Web Programming (Page 124-131)

5.2 Cookies

6.1.2 JSP Basics

A JSP page is a mixture of server side coding along with HTML mixed into a single web page.

When a request for a JSP is made, the server side Java within the page is executed on the server and the resulting HTML is returned to the end user. There are three basic tags used to denote JSP code.

They are the scriptlet, expression tags, and directive tags. The following sections will focus on the first two types of tags.

JSP Scriptlet Tags

Scriptlets allow developers to embed Java commands within a JSP page. These commands are executed on the server and are used to create the resulting dynamic HTML page. The scriptlet tags are specified using the <% and %> symbols. The Java commands are embedded within these two tags.

Any valid Java statements can be placed with the scriptlet tags. Typically, the Java code is used to create conditionals, looping constructs, manipulate HTML header data, connect to databases, and interface with Enterprise Java Beans. Anything that can be done within the Java language can be placed within these tags.

Java code is generally used to create conditionals and looping constructs, manipulate HTML header data, connect to databases, or interface with Enterprise Java Beans. Anything that can be done within the Java language can be done in between these tags. Below is an example of Java code embedded within an HTML page.

The example above is not very interesting because the code doesn’t interact with the HTML. What makes scriptlets interesting is how they can be interspersed between HTML tags to create dynamic HTML. The concept is most prevalent with looping and conditional constructs. The following example uses looping to generate dynamic HTML.

<ul>

<%

for ( int x = 0; x < 5; x++ ) {

%>

Here the for loop is split between two scriptlet tags. This is not only legal , but is in fact desired.

It allows the HTML in between the scriptlets to be repeated multiple times. The output from this code is shown below.

<ul>

<li>this is a bullet point</li>

<li>this is a bullet point</li>

<li>this is a bullet point</li>

<li>this is a bullet point</li>

<li>this is a bullet point</li>

</ul>

Study this example until it makes sense. It is important to grasp the concept of code spread across multiple scriptlet tags intermingled within HTML. The previous example can be expanded upon to use conditionals for determining the HTML to display.

<ul>

<%

for ( int x = 0; x < 5; x++ ) { if ( x % 2 == 0 ) {

%>

<li>this is an even bullet point</li>

<%

} else {

%>

<li>this is an odd bullet point</li>

<%

} }

%>

</ul>

JSP Expressions Tags

The JSP scriptlet tags are nice for embedding Java logic within a web page, but what if a developer wants to embed a dynamic value in the HTML? The JSP expression tags provides the developer with a mechanism to accomplish the task. Expression tags take any valid Java expression and evaluates the expression to find its string value. Primitives are automatically converted to strings.

The resulting String replaces the expression tag within the final HTML returned to the client.

The expression tag is opened with <%= and closed with a %>. In between, is where the Java expression is placed. Below are two simple examples of using the expression tag.

<p>This is a <%= "simple string" %></p>

<p>The value of 45 + 23 = <%= 45 + 23 %></p>

The first line embeds the String “simple string” within the HTML and the second line evaluates 45 +23 and converts the resulting value into a string and embeds it. Below is the output from the example.

<p>This is a simple string</p>

<p>The value of 45 + 23 = 68</p>

One important note about expressions is the lack of semicolon. Semicolons in Java are used to define a statement. A statement can be composed of an expression, but an expression is not a statement.

An expression must evaluate to a value and is not a statement of code. Thus the semicolon can not be used within an expression tag. The reason for this becomes apparent later when the process of converting JSPs to Java Servlets is explained.

On top of simple expressions, the JSP expression tag can use previously defined variables. The variables are normally defined within previous JSP scriptlets. Below is an example of a using values defined in a scriptlet within a series of expressions.

<%

In this example a scriptlet tag is used to define the values of x and y. The rest of the page uses various expression tags to generate various outputs using these variables. The resulting HTML is shown below.

In addition, expression tags can be embedded within scriptlet conditionals and loops. The following example extends the previous section’s looping example. It puts the value of “odd” or “even” next to the value of the loop.

<ul>

<li><%= x %> is an <%= value %> number</li>

<%

}

%>

</ul>

In this instance the loop goes from zero to four. Each time through the loop the value of loop variable is evaluated to see if it is an odd or even value. The String value of odd or even is placed within String variable oddEven. The value of x and the value of oddEven are then placed within the resulting HTML The process is repeated until the loop is exhausted. The resulting HTML is shown below.

<ul>

<li>0 is an even number</li>

<li>1 is an odd number</li>

<li>2 is an even number</li>

<li>3 is an odd number</li>

<li>4 is an even number</li>

</ul>

Comments

This is a good point to talk about embedding comments within a JSP page. The <%- - and - -%>

tags are used to indicate comments.

<%-- Example of a JSP comment --%>

<%--Example of a multi line JSP comment --%>

Comments are notes that can be attached to a JSP page to provide information about the dynamic portions of the page, but do not show up within the HTML. JSP comments are removed at the server level and thus never appear in the resulting HTML. They are only available within the JSP source code.

In document Java Web Programming (Page 124-131)