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 defines the following tags to enclose Java code fragments and directives for the JSP compiler:
JSP tags can also be written with XML tags. See Using WebLogic JSP for a table of equivalent tags.
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">
info="Calendar JSP example"
<h1>Today is <jsp:getProperty name="calendar"
<jsp:getProperty name="calendar" property="HtmlMonth" />
<p>This page executed by
<%= application.getServerInfo() %>.<br>
Copyright 1999-2000 © BEA Systems, Inc.
All Rights Reserved.
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:
For more help on these properties see Setting up WebLogic for JSP in "Using WebLogic JSP".
javac -d %SERVER_CLASSES% CalendarBean.java
javac -d $SERVER_CLASSES CalendarBean.java
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.