Corporate Info | News | Solutions | Products | Partners | Services | Events | Download | How To Buy
Introduction to BEA WebLogic Server 5.1: Previous topic | Next topic | Contents

JSP (JavaServer Pages)

JSP (JavaServer Pages) is a standard for combining Java and HTML to provide dynamic content in web pages. JSP is part of Sun's Java 2 Platform, Enterprise Edition. WebLogic Server 4.5 implements the JSP 1.1 specification.

JSP depends upon HTTP servlets-the JSP compiler generates the source code for an HTTP servlet from a .jsp file. The Java classes produced using the HTTP servlet and JSP are essentially equivalent. You can use either technology to create dynamic web pages.

JSP emphasizes page design with standard HTML formatting, so it is more convenient to use JSP for creating dynamic web pages than it is to write a Java HTTP servlet.

With JSP, you embed Java code in HTML using special JSP tags similar to HTML tags. You install the JSP page, which has a .jsp extension, into the WebLogic Server document root, just as you would a static HTML page. When WebLogic Server serves a JSP page to a client, it tests whether the file has been compiled. If not, it calls the WebLogic JSP compiler to compile the file into a servlet. A .jsp file is only recompiled when it has changed.

Although you can embed as much Java code as you like into a JSP page, using JavaBeans and Enterprise JavaBeans lets you concentrate on page design and presentation in the JSP page and encourages you to move application logic into reusable components. Using beans from JSP pages also makes it possible for web page designers to create and maintain dynamic web pages without having to learn Java programming.

JSP Tags

JSP defines the following tags to enclose Java code fragments and directives for the JSP compiler:

<%! declaration %>
  Contains class-level declarations of Java variables or methods. The variables and methods declared can be referenced within other JSP tags on the page.

<% scriptlet %>
  Contains a Java code fragment that executes when the JSP page executes.

<%@ include file="relativeURL" %>
  Includes the contents of a file at compile time. The JSP compiler includes the file and processes its contents.

<%@ page attributes %>
  Defines page-level attributes. Attributes are specified as name="value" pairs. Multiple attributes are separated by white space within the tag.

<%= expression %>
  Writes the value of a Java expression into the response page.

<!-- <%= expression %> -->
  Evaluates a Java expression within an HTML comment. The resulting comment is not rendered by the browser, but can be viewed in the page source.

<%-- comment --%>
  Comments the JSP source. Comments written within these tags are not included in the HTML output.

<jsp:useBean ... />
  Specifies a JavaBean to be used on the JSP page.

<jsp:setProperty ... />
  Sets JavaBean properties.

<jsp:getProperty ... />
  Gets JavaBean properties.

JSP tags can also be written with XML tags. See Using WebLogic JSP for a table of equivalent tags.

Implicit Variables

JSP provides a number of implicit variables that can be referenced on any JSP page. The JSP compiler adds these class variable declarations to the generated servlet code:

    HttpServletRequest request;   // Request object
    HttpServletResponse response; // Response object
    HttpSession session;          // Session object for the client
    ServletConfig config;         // ServletConfig for this JSP
    ServletContext application;   // Servlet context
    PageContext pageContext;      // Page context for this JSP
    Object page;                  // Instance of the implementation
                                  // class for this JSP (i.e., 'this')
    JspWriter out;                // PrintWriter to the browser

The request and response variables provide access to the HTTP request and response objects. A JSP page can extract information from the request and write information on the response using methods from the javax.servlet.http package.

The session variable allows WebLogic Server to save state for a client between requests on a JSP page. You must set the "session=true" attribute in the JSP page directive so that the JSP compiler includes support for sessions. Then you can save arbitrary values and retrieve them again on subsequent requests with the getValue() and putValue() methods on the session.

The config variable provides access to the javax.servlet.ServletConfig object for the JSP page. The methods on this object return initialization parameters for the page. With WebLogic Server, you define initialization parameters by setting an initArg property when you register a servlet in the weblogic.properties file. To use initialization arguments with JSP pages, you must register the JSP page as a servlet.

The application variable gives you access to information about the environment of the JSP page and provides methods for writing messages in the WebLogic Server log.

The pageContext variable provides access to a javax.servlet.jsp.PageContext object. You can use methods on this object to access various scoped namespaces, servlet-related objects, and common servlet-related functionality.

The page variable references the current instance of the JSP page. page is declared as a java.lang.Object, so you must cast it to the name of the generated servlet class name if you want to use it. An easier alternative is to just refer to the current instance of the page with the Java this keyword.

The out variable is a Java PrintWriter you can use to write HTML to the browser page from inside JSP tags.

A Simple JSP Example

The Calendar.jsp example is a simple JSP page that calls a JavaBean to display a calendar for the current month. The source for the JavaBean is in the file CalendarBean.java.

CalendarBean gets the current date and provides two get methods, getTodayString() and getHtmlMonth(). getTodayString() returns the current date in a String such as "Thursday, October 14, 1999". getHtmlMonth() returns a String containing the calendar for the current month, formatted as an HTML table.

CalendarBean has one set method, setColor(), which is used to set the background color of the calendar.

Calendar.jsp shows how to specify a JavaBean, set its properties, and get its values from within a JSP page. The source of the JSP page is fairly simple, but it demonstrates how complicated web page elements can be delegated to reusable JavaBean components.

Here is the source for Calendar.jsp:

Listing 5-1 Calendar.jsp

    <!doctype html public "-//w3c/dtd HTML 4.0//en">
    <html>
    <head><title>Calendar</title></head>

    <%@ page
            info="Calendar JSP example"
            contentType="text/html"
    %>

    <jsp:useBean id="calendar"
      scope="page"
      class="examples.intro.CalendarBean"
    />

    <jsp:setProperty name="calendar"
         property="Color" value="#FFFFCC"/>

    <h1>Today is <jsp:getProperty name="calendar"
      property="TodayString"/></h1>

    <p>
    <center>
    <jsp:getProperty name="calendar" property="HtmlMonth" />
    </center>

    <p>
    <hr>
    <font face="Helvetica">
    <p>This page executed by
    <%= application.getServerInfo() %>.<br>
    Copyright 1999-2000 &copy; BEA Systems, Inc.
    All Rights Reserved.
    </body>
    </html>

The <jsp:useBean ... /> tag identifies the CalendarBean class and assigns it an ID (calendar), which is used to reference it in other JSP tags on the page. The scope attribute specifies that the instance of this bean is stored in the current page. Setting the scope to "session" would tell WebLogic Server to store the bean instance in the session so that any changes persist over multiple requests on the JSP page.

The <jsp:setProperty ... /> tag sets the background color of the HTML table. The JSP compiler translates this to a call to the CalendarBean.setColor() method.

The <jsp:getProperty ... /> tags are translated into calls to CalendarBean.getTodayString() and CalendarBean.getHtmlCalendar(). The results of those calls are embedded in the HTML output.

Here is an example of the output from the Calendar.jsp JSP page:

Running the Calendar.jsp Example

To use Calendar.jsp, your WebLogic Server must be configured to serve JSP pages. Then you compile CalendarBean.java, copy Calendar.jsp into WebLogic Server's document root, and call the page in a browser. Here are the steps:

1. If you have not yet configured WebLogic Server for JSP, edit the weblogic.properties file in the WebLogic installation directory. Find and uncomment the weblogic.httpd.register.*.jsp and weblogic.httpd.initArgs.*.jsp properties. Edit the compileCommand attribute to point to the location of your Java compiler. Edit the workingDir attribute so that it points to an existing directory where the JSP compiler can store its generated files.

   For more help on these properties see Setting up WebLogic for JSP in "Using WebLogic JSP".

2. Set up your development environment as described in Setting your Development Environment.

3. Change to the directory where you copied the example files and compile the CalendarBean.java file.

   Windows NT:

       javac -d %SERVER_CLASSES% CalendarBean.java

   UNIX:

       javac -d $SERVER_CLASSES CalendarBean.java

4. Copy the Calendar.jsp file into the document root of your WebLogic Server. By default, this is the myserver/public_html directory in your WebLogic installation.

5. Start WebLogic Server. See Setting up and Starting WebLogic Server for help.

6. In a browser, enter the URL for the JSP page. For example:

       http://localhost:7001/Calendar.jsp

More about JSP

You can find more about using WebLogic JSP in the developers guide Using WebLogic JSP.

See the Sun JSP page to find JSP specifications, tutorials, and examples.

Copyright © 2000 BEA Systems, Inc. All rights reserved. Required browser: Netscape 4.0 or higher, or Microsoft Internet Explorer 4.0 or higher.