Skip Headers

Oracle9iAS Containers for J2EE User's Guide
Release 2 (9.0.2)

Part Number A95880-01
Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index

Go to previous page Go to next page

6
JSP Primer

This chapter covers the basics of running JavaServer Pages (JSP) applications in the Oracle9iAS Containers for J2EE (OC4J) environment. It is assumed that you have installed OC4J and that you have a basic understanding of Java programming and Web application technology. Although this chapter includes a brief overview, you should already be familiar with JSP technology. This chapter also introduces Oracle value-added features for JSP support.

For detailed information about the Oracle JSP implementation, as well as an overview of standard syntax and key features, please refer to the Oracle9iAS Containers for J2EE Support for JavaServer Pages Reference.

This chapter includes the following topics:

For a complete description on Web application deployment, see "Deploying Applications".

A Brief Overview of JavaServer Pages Technology

This section provides a quick overview of the following:

What Is JavaServer Pages Technology?

JSP, a part of the J2EE platform, is a technology that is specified by Sun Microsystems as a convenient way to generate dynamic content in pages that are output by a Web application. This technology, which is closely coupled with Java servlet technology, allows you to include Java code snippets and calls to external Java components within the HTML code, or other markup code such as XML, of your Web pages. JSP technology works nicely as a front-end for business logic and dynamic functionality encapsulated in JavaBeans and Enterprise JavaBeans (EJBs).

JSP syntax within HTML or other code is designated by being enclosed within <%...%> syntax. There are variations on this: <%=...%> to designate expressions or <%!...%> to designate declarations, for example.

A JSP page is translated into a Java servlet before being executed, and it processes HTTP requests and generates responses similarly to any other servlet. JSP technology offers a more convenient way to code the servlet. Translation usually occurs "on demand"--that is, as the application is run. The JSP translator is typically triggered by the .jsp file name extension in a URL. Additionally, as an Oracle feature, the .sqljsp file name extension, used for SQLJ JSP pages, will also trigger the JSP translator, as well as the SQLJ translator.

JSP pages are fully interoperable with servlets--a JSP can include output from a servlet or forward to a servlet, and a servlet can include output from a JSP or forward to a JSP.

Here is the code for a simple JSP, welcomeuser.jsp:

<HTML> 
<HEAD><TITLE>The Welcome User JSP</TITLE></HEAD> 
<BODY> 
<% String user=request.getParameter("user"); %> 
<H3>Welcome <%= (user==null) ? "" : user %>!</H3> 
<P><B> Today is <%= new java.util.Date() %>. Have a fabulous day! :-)</B></P> 
<B>Enter name:</B> 
<FORM METHOD=get> 
<INPUT TYPE="text" NAME="user" SIZE=15> 
<INPUT TYPE="submit" VALUE="Submit name"> 
</FORM> 
</BODY> 
</HTML> 

This JSP page will produce something like the following output if the user inputs the name "Amy":

Welcome Amy! 

Today is Wed Jun 21 13:42:23 PDT 2000. Have a fabulous day! :-) 

JSP Translation and Runtime Flow

Figure 6-1 shows the flow of execution when a user runs a JSP page, specifying its URL in the browser.

Because of the .jsp file name extension, the following steps occur automatically:

  1. The JSP translator is invoked, translating Hello.jsp and producing the file Hello.java. (For a .sqljsp file, it would produce Hello.sqlj and the SQLJ translator would be invoked to perform SQLJ translation and produce Hello.java.)

  2. The Java compiler is invoked, creating Hello.class.

  3. Hello.class is executed as a servlet, using the JSP runtime library.

  4. The Hello class accesses the database through JDBC or SQLJ, as appropriate, and sends its output to the browser.

Figure 6-1 JSP Translation and Runtime Flow

Text description of jsptrans.gif follows.

Text description of the illustration jsptrans.gif

Key JSP Advantages

For most situations, there are at least two general advantages to using JSP pages instead of servlets:

JSP in Application Architecture

JSP pages fit well into common application architectures such as Model-View-Controller. In this architecture, a "controller" servlet or JSP page acts as the front-end handler of the HTTP request, while JavaBeans or Enterprise JavaBeans provide the back-end data "model", taking care of business logic. The presentation from a JSP--perhaps, but not necessarily, the same page that acts as the controller--provides the final "view" of the data. Figure 6-2 shows this architecture.

Figure 6-2 JSP in the Model-View-Controller Architecture

Text description of mvc.gif follows.

Text description of the illustration mvc.gif

Running a Simple JSP Page

This section shows you how to run the JSP example from "What Is JavaServer Pages Technology?", and assumes the following:

Create and Deploy the JSP

Copy or type the sample code from "What Is JavaServer Pages Technology?" into a file, and save it as welcomeuser.jsp. Then, archive welcomeuser.jsp into a WAR file with an appropriate web.xml and deploy it using the Enterprise Manager deployment wizard, mapping it to the /wuser servlet context in the URL Mapping screen.

Run welcomeuser.jsp

When specifying a URL to execute an application in Oracle9iAS, note the following:

For example, if you mapped the WAR file containing welcomeuser.jsp to the /wuser servlet context, you can run the page through the Oracle HTTP Server a URL such as the following:

http://<apache_host>:<port>/wuser/welcomeuser.jsp

This uses <apache_host> to represent the name of the system where OC4J and the application are installed. Typically, use 7777 for the port.

If the JSP is not at the top level in the WAR file, but is contained within a subdirectory below the top level, then this directory must be included in the HTTP URL separated by a backslash. For example, if the welcomeuser.jsp is located in the mydir directory in the WAR file, then you would invoke it as follows:

http://<apache_host>:<port>/wuser/mydir/welcomeuser.jsp

When you first run the page, you will see something like the following output:

Text description of jspprim3.gif follows

Text description of the illustration jspprim3.gif

Submitting a name, such as Amy, updates the page, as shown in the next screen.

Text description of jspprim4.gif follows

Text description of the illustration jspprim4.gif

Running a JSP Page That Invokes a JavaBean

As mentioned earlier, JSP technology works nicely as a front-end for business logic and dynamic functionality encapsulated in JavaBeans. In fact, most well-designed JSP applications have relatively little Java code in the JSP page; instead, the Java logic and business logic are contained in other components, such as JavaBeans, that are invoked from the page. This section contains the code for a JavaBean and a JSP page that calls it, and also shows where to place the files appropriately in OC4J, and how to run the application.

This section documents the following steps:

Create the JSP--usebean.jsp

This section lists the source for a JSP page that uses a standard JSP useBean tag to invoke a JavaBean. To run the code, you can copy or type it into a file called usebean.jsp. For additional information, see the notes following the code.

<%@ page import="beans.NameBean"  %>

<jsp:useBean id="pageBean" class="beans.NameBean" scope="page" />
<jsp:setProperty name="pageBean" property="*" />

<HTML>
<HEAD> <TITLE> The Use Bean JSP </TITLE> </HEAD>
<BODY BGCOLOR=white>

<H3> Welcome to the Use Bean JSP </H3>

<% if (pageBean.getNewName().equals("")) { %>
  I don't know you. 
<% } else { %>
  Hello <%= pageBean.getNewName() %> !
<%  } %> 

<P>May we have your name?
<FORM METHOD=get>
<INPUT TYPE=TEXT name=newName size = 20>
<INPUT TYPE=SUBMIT VALUE="Submit name">
</FORM>
</BODY>
</HTML>

Code Notes

Create the JavaBean--NameBean.java

Here is the code for the JavaBean class, NameBean. The package name specified here must be consistent with the page directive and the useBean tag of the JSP page. To run the JSP page, you can copy or type this code into a file, NameBean.java, and then compile it. This file must be in a beans subdirectory, according to the package name.

package beans;

public class NameBean {

  String newName="";

  public void NameBean() {  }

  public String getNewName() {
    return newName;
  }
  public void setNewName(String newName) {
    this.newName = newName;
  }
} 

Run usebean.jsp

Deploy the WAR file that contains the usebean.jsp to the /usebean servlet context. You specify the servlet context in the URL Mapping screen within the deployment wizard.

This example, as before, uses <apache_host> as the name of the system where OC4J and the application are installed. Then, execute the JSP, as follows:

http://<apache_host>:<port>/usebean/usebean.jsp

This assumes that the OC4J Web server is still running. Typically use port 7777.

When you run this page, you will initially see the following output:

Text description of scn_useb.gif follows.

Text description of the illustration scn_useb.gif

Once you submit a name, such as Ike, the page is updated, as follows. The prompt is in case you want to enter another name.

Text description of scn_usea.gif follows.

Text description of the illustration scn_usea.gif

Running a JSP Page That Uses Custom Tags

The Sun Microsystems JavaServer Pages specification includes standard tags to use in JSP pages to perform various tasks. An example is the useBean tag employed in "Running a JSP Page That Invokes a JavaBean". The JSP 1.1 specification also outlines a standard framework that allows vendors to offer their own custom tag libraries in a portable way.

OC4J supplies portable tag libraries with functionality in several areas, including database access, XML/XSL processing, e-mail, file uploading and downloading, and programming convenience. These libraries are described in the Oracle9iAS Containers for J2EE JSP Tag Libraries and Utilities Reference.

This section shows an example that uses tags from the Oracle SQL tag library to access and query a database and output the results to the browser.

Here are the steps in using a JSP tag library:

This section documents the following steps:

For information about the standard tag library framework, including TLD files, tag handler classes, and tag-extra-info classes, please refer to the Oracle9iAS Containers for J2EE Support for JavaServer Pages Reference.

Create the JSP Page--sqltagquery.jsp

This section provides the source for a JSP page that uses SQL tags that are supplied with OC4J to open a database connection, run a simple query, output the results as an HTML table, and close the connection. To run the code, you can copy or type it into a file called sqltagquery.jsp. For additional information, see the notes following the code.

<%@ taglib uri="/WEB-INF/sqltaglib.tld" prefix="sql" %>
<HTML>
  <HEAD>
    <TITLE>The SQL Tag Query JSP</TITLE>
  </HEAD>
  <BODY BGCOLOR="#FFFFFF">
    <HR>           
      <sql:dbOpen URL="jdbc:oracle:thin:@dbasehost:5521:orcl"
                  user="scott" password="tiger" connId="con1">
      </sql:dbOpen>
      <sql:dbQuery connId="con1">
           select * from EMP
      </sql:dbQuery>
      <sql:dbClose connId="con1" />
    <HR>
  </BODY>
</HTML>
Code Notes

For more information about the standard JSP tag library framework and features, please refer to the Oracle9iAS Containers for J2EE Support for JavaServer Pages Reference. For more information about the SQL tag library that is supplied with Oracle9iAS, refer to the Oracle9iAS Containers for J2EE JSP Tag Libraries and Utilities Reference.

Set Up Files for Tag Library Support

Put sqltaglib.tld into the /WEB-INF directory of the WAR file for the application.

Your Web application uses the following JAR files that are installed with OC4J: ojsp.jar, ojsputil.jar, xmlparserv2.jar, and xsu12.jar. Typically, these are installed into the j2ee/home/lib directory, which is included in the CLASSPATH. The tag handler and tag-extra-info class files are in ojsputil.jar.


Notes:

  • Placing ojsputil.jar into the j2ee/home/lib directory also gives you access to data-access JavaBeans and other Java utility classes that come with OC4J. These classes are described in the Oracle9iAS Containers for J2EE JSP Tag Libraries and Utilities Reference.

  • If you choose to run the demos in the ojspdemos.ear file, the TLD files are automatically placed in the WEB-INF directory of the ojspdemos application. (The demos as a whole have their own application root.)


Run sqltagquery.jsp

As with earlier examples in this chapter, you will use a similar URL to run the page from a browser. Deploy the WAR file that contains the sqltagquery.jsp to the /sqltag servlet context. You specify the servlet context in the URL Mapping screen within the deployment wizard.

This example, as before, uses <apache_host> as the name of the system where OC4J and the application are installed. Then, execute the JSP, as follows:

http://<apache_host>:<port>/sqltag/sqltagquery.jsp

This assumes that the OC4J Web server is still running. Typically use port 7777.

This page produces output such as the following screen.

Text description of scn_stag.gif follows.

Text description of the illustration scn_stag.gif


Important:

The Oracle JDBC driver classes are supplied with the OC4J download, in the j2ee/home/lib directory, but you must ensure that they are compatible with your JDK and your database version. The classes111.zip or .jar library is for JDK 1.1.x; the classes12.zip or .jar library is for JDK 1.2.x or higher. Also, the driver release number, such as 8.1.7 or 9.0.1, must be compatible with your database release number.


Overview of Oracle Value-Added Features for JSP Pages

OC4J JSP provides the following extended functionality through custom tag libraries and custom JavaBeans and classes that are generally portable to other JSP environments. These features are documented in the Oracle9iAS Containers for J2EE JSP Tag Libraries and Utilities Reference.

In addition, the OC4J JSP container offers integration with caching technologies, documented in the Oracle9iAS Containers for J2EE JSP Tag Libraries and Utilities Reference:

The OC4J JSP container also supports the following Oracle-specific programming extensions, documented in the Oracle9iAS Containers for J2EE Support for JavaServer Pages Reference.


Go to previous page Go to next page
Oracle
Copyright © 2002 Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Solution Area
Go To Table Of Contents
Contents
Go To Index
Index