Overview of JSP Syntax
Now that we're actually working with JSP, the next step is to get an overview of JSP itself, which means getting an overview of the JSP syntax. After we've gotten the components of JSP syntax down, everything else will fit into that framework. There are four different types of elements you can use in JSP:
Scripting elementsThis is where your Java goes, as in the scriptlets we've already seen.
CommentsComments are text you add to annotate a JSP documentthey're like notes to yourself or other programmers. They're ignored by the Web server, and are only for programmers to read.
DirectivesDirectives are instructions specific to a particular page that let you set how the page is to be processed. For example, you can use a directive to indicate that you want the output of a page to be XML, not HTML.
ActionsActions let you perform some operation, such as including other pages in the current page, or including Java applets, or working with JavaBean components (which you'll see in Day 6). Unlike directives, actions are re-evaluated each time the page is accessed.
Let's take a look at these various types of JSP elements, starting with scripting elements.
There are three types of scripting elements in JSP:
ScriptletsScriptlets can contain Java code. This is the most general of all the scripting elements.
DeclarationsDeclares a variable or a method for use in your code.
ExpressionsContains a Java expression that the server evaluates. The result of the expression is inserted into the Web page.
The most general place for your Java code is in a scriptlet. Scriplets hold Java code fragments, and are enclosed with the tags <% and %>. We've already seen an example of a scriptlet in our first JSP, where we used the Java statement out.println("Hello there!"); to display text in a Web page:
<HTML> <HEAD> <TITLE>A Web Page</TITLE> </HEAD> <BODY> <% out.println("Hello there!"); %> </BODY> </HTML>
A scriptlet can hold any number of Java statements, declarations, or expressions, making scriptlets the most general of all the JSP scripting elements. Note that they must hold valid Java statements, so you can't include direct HTML in a scriptlet.
Scriptlets are what many JSP programmers think of when they think of JSP. Scriptlets are used to embed Java code in HTML documents, turning them into JSP documents. But besides scriptlets, you can also use declarations and expressions.
As you'll see tomorrow, you can store data using variables in Java. For example, here's how you can store our text string Hello there! in a variable of type String:
String msg = "Hello there!";
This declares a new variable named msg that holds the text Hello there!. In Java, you must declare variables before you use them, giving them a name. After you've named a variable, you can refer to it in your code using that name.
There's a special set of tags, <%! and %>, that you can use to declare variables in JSP (you can also declare variables in scriptlets, which is a more common thing to do). Here's an example: In this case, the code declares the variable msg:
<HTML> <HEAD> <TITLE>A Web Page</TITLE> </HEAD> <BODY> <%! String msg = "Hello there!"; %> </BODY> </HTML>
Now you can use this new variable in a scriptlet; for example, here's how you can display the text in msg in the Web page using the out.println method:
<HTML> <HEAD> <TITLE>A Web Page</TITLE> </HEAD> <BODY> <%! String msg = "Hello there!"; %> <% out.println(msg); %> </BODY> </HTML>
That's what the process looks like in overview; you'll get all the details on declaring and using variables in Day 2.
Expressions are any Java code fragment that can be evaluated to yield a value. For example, the expression 2 + 2 yields a value of 4, the expression 44 - 10 yields 34, and so on. In JSP, you can surround a Java expression in the tags <%= and %>. The expression's value is inserted into the Web page as text by the server.
You can even use a variable as an expressionwhen such an expression is evaluated, it yields the value of the variable. For example, if we had stored our string Hello there! in a variable named msg, we can insert that string into the Web page simply by using the variable as an expression this way:
<HTML> <HEAD> <TITLE>A Web Page</TITLE> </HEAD> <BODY> <%! String msg = "Hello there!"; %> <%= msg %> </BODY> </HTML>
Expressions like this are similar to scriptlets, because you can place Java code in them, but they have to be able to result in a single value when they're evaluated.
Expressions like this are useful for shorter JSP pages, but note that you don't have to use them to insert text into a Web pageyou can use methods like out.println in scriptlets instead.
You can use JSP comments to document what's going on in a JSP page; they act like notes to you or other programmers. Comments are purely for the benefit of the programmers that work on the page, because the server will strip them out before sending the page back to the browser. You enclose the text in a comment between the tags <%-- and --%>.
Here's an example; in this case, the code includes the comment Display the message now. to this JSP example:
<HTML> <HEAD> <TITLE>A Web Page</TITLE> </HEAD> <BODY> <%-- Display the message now. --%> <% out.println("Hello there!"); %> </BODY> </HTML>
This comment lets you and other programmers know what's going on in your JSP. You can add as many comments to a JSP as you likethey're entirely for the benefit of programmers, and will all be stripped out before the page is sent back to the browser.
JSP directives let you give directions to the server on how a page should be processed. There are three directives in JSP; we'll see them throughout the book, but here's an overview:
pageThis directive lets you configure an entire JSP page, such as whether its output is HTML or XML. You'll see more on this directive in Day 6.
includeThis directive lets you include another page or resource in a JSP page.
taglibThis directive lets you use a set of custom JSP tags as defined in a tag library. More on taglibs is coming up in Day 9, "Using Custom JSP Tags," and Day 10, Creating Custom Tags."
The page directive is going to be a useful one for us as we work with Java. You can use this directive to configure the page you're working on. Like other directives, you use the special tags <%@ and %> with this directive; to create a page directive, you also include the keyword page like this: <%@ page ... %>.
Here's an example to show what the page directive can do for us. In this case, the code wants to indicate that if there's an error in the current page, the server should send an error page named error.jsp back to the browser; that we're programming in Java; that the output of the current page should be XML (not HTML, which is the defaultmore on XML in Day 18, "Using XML and XSLT in JSP"); and that we want to import the java.sql package so that our code can work with SQL databases. You'll see more on importing Java packages when we start working with Java in depthJava is divided into many sections called packages, and to use a particular section's functionality, you must import the corresponding package, which makes it accessible to your code.
You can do all that with attributes of a single page directive. In JSP, attributes work just as they do in HTMLthey're part of an element's opening tag, and have the form attribute=value. Here's how you can use the errorPage, language, contentType, and import attributes of the page directive to configure the page as you want (note that the page directive comes at the very beginning of the page):
<%@ page errorPage="error.jsp" language="java" contentType="application/xml" import="java.sql.*" %> <HTML> <HEAD> . . .
As you can already see, the page directive is going to give us a lot of programming power.
The include directive, which looks like <%@ page ... %>, lets you include another page in the current page. When you include a page at a particular point, that page's entire contents are simply inserted at that point.
The taglib directive (which looks like this: <%@ taglib ... %>) specifies a library of custom JSP tags (see Days 9 and 10) that you want to use in the current page. Tag libraries let you define your own JSP tags. We're going to see how to use this directive later, so we won't go into the details now.
There are rumors that scriptlets are going to be phased out in JSP 2.0 and tag libraries will be used instead, so it's a good idea to make sure you know what's going on with tag libraries in Days 9 and 10.
JSP also includes actions. As their name implies, actions let you perform some action. Unlike directives, actions are re-evaluated each time the page is accessed.
There are two types of actions: custom and standard. Custom actions are actions you create yourself, and standard actions come built into JSP. Here are the standard actions in overview:
<jsp:forward>Forwards the browser request for a new Web page to an HTML file, JSP page, or servlet. In this way, you can delegate how your Web applications respond to the browser.
<jsp:include> Includes a file or Web component. Note that <jsp:include> is different than the include directive because it re-evaluates the included file every time the page is accessed, whereas the include directive does not.
<jsp:plugin> Lets you execute applets or JavaBeans with a plug-in. If the browser doesn't have the required plug-in module, it will display a dialog box asking you to download it. We'll see this one in Day 13.
<jsp:getProperty>, <jsp:setProperty>, and <jsp:useBean>You use these actions with JavaBean components, as you'll see in Day 6.
We'll get all the details on these standard actions when we put them to work in the coming days. And that's itthat completes our overview of JSP syntax. Now we've got the foundation we'll need to move on to Day 2.