Programming with JSP

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 22.  JavaServer Pages (JSP)


Every programming language has special syntax that must be understood before you can program with it. JSP uses Java syntax wrapped in special character delimiters so that the servlet generator can differentiate between code that you have written to provide logic and the HTML code that is to be sent to the browser verbatim.

Expressions

The first special syntax is called the expression. Expressions are statements that are to be reduced to a literal value that will be included in the HTML document being created. The delimiting characters are <%= and "%>. In a JSP, you might find the following line:

 Hello, <b><%= request.getparameter("firstName") %></b> 

If the parameter value is Joe, the following would be what the user would see in his browser:

 Hello, Joe 

Adding Scriptlets to JSP Pages

Scriptlets are like expressions in that after they are run HTML commands are the result. They are less restrictive than expressions in that they enable arbitrary Java code to be used.

The syntax for scriptlets is simply:

 <% some Java code %> 

A typical use of scriptlet code is shown in this fragment:

 <%     for (int i=0; i<10; i++)     {  %>        <b>        <%= i %>        </b>  <%     }  %> 

Declarations

Declarations are for declaring servlet-level variables that will appear in the servlet outside of any methods. Code that is in a scriptlet can contain variable declarations, but these declarations will be placed in the _jspService() method, which will be called by the service() method. As such, their scope will be limited to that method.

The syntax for a declaration is

 <%! Declarative code %> 

A fragment that uses this syntax is shown here:

 <%! char c1 = 0; %> 

Adding Comments

There are two types of comments in JSP. The first type is the HTML comment, which takes the form:

 <!-- This is an HTML comment --> 

Like any other HTML expression, it appears outside of all scriptlets in the page and is passed through to the browser just like any other piece of HTML.

The second type of comment is a JSP comment. These comments are never intended to appear in the output, but are included for the benefit of the JSP's programmers, both current and future. This comment's syntax is

 <%-- This is a JSP comment --%> 

Directives

Directives are commands that modify the resulting servlet in a major way. The three types of directives are page, include, and taglib. The page directive enables you to import classes, specify the servlet's superclass, set the content type, and so on. The include directive enables you to insert an external file into a JSP page at a certain point. This allows a modular approach to creating JSP pages. The taglib directive can be used to describe custom markup tags. Chapter 24, "Custom Tag Libraries," will provide further explanation of this directive.

The syntax for a directive is

 <%@ directive text %> 

An example of a directive is shown here:

 <%@ page import="mypackage.myclass" %> 

Implicit Objects

Servlets have access to the request and response objects via the passing of these parameters in the doGet(), and the doPost() methods. These objects are accessible to JSP not as parameters passed in, but as implicit objects. From the standpoint of the JSP author, eight objects just exist and they are available for your use. These objects are

  • request The request object is the HttpServletRequest object from which you extract parameter and browser data in servlets.

  • response The response object is the HttpServletResponse object that you send data to for transmission to the browser.

  • out The out object is a buffered version of the PrintWriter object that you are used to declaring and printing to in servlets. JSP expressions use this object without naming it explicitly, whereas scriptlets make explicit reference to it.

  • session The session object is the HttpSession object that servlet writers use to preserve and retrieve state variables across different servlets called by the same browser.

  • application The application object is the ServletContext object that contains attributes that are available to all servlets in the servlet engine, independent of the browser that invoked them.

  • config This is the ServletConfig object for this page. The servlet container places information to this object during initialization.

  • pageContext The PageContext object is unique to JSP. Many of the page attributes are stored in this object. It can also be used to store shared data.

  • page The page object is a synonym for this, but it is not often used.

These objects enable the JSP programmer to have access to information about the environment that he is programming.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

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