Oracle Application Server Containers for J2EE User's Guide 10g (9.0.4) Part Number B10322-01 |
|
In Oracle Application Server 10g, OC4J includes a JavaServer Pages (JSP) container that is fully compliant with the JSP 1.2 specification. This chapter covers the basics of running JSP applications in the OC4J environment. There is also a brief JSP review, although it is assumed that you are at least somewhat familiar with JSP technology.
There are a few assumptions before you try running the primers. See "Introduction to OC4J".
This chapter includes the following sections:
For a complete description of Web application deployment, see "Deploying Applications".
For detailed information about the Oracle JSP implementation, refer to the Oracle Application Server Containers for J2EE Support for JavaServer Pages Developer's Guide .
Here is a quick JSP overview in the following sections:
JavaServer Pages, a part of the J2EE platform, is a technology that provides 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 (EJB).
Traditional 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.
Note: The JSP 1.2 specification introduces an XML-compatible JSP syntax as an alternative to the traditional syntax. This allows you to produce JSP pages that are syntactically valid XML documents. The XML-compatible syntax is described in the Oracle Application Server Containers for J2EE Support for JavaServer Pages Developer's Guide . |
A JSP page is translated into a Java servlet, typically at the time that it is requested from a client. The JSP translator is triggered by the .jsp
file name extension in a URL. The translated page is then executed, processing HTTP requests and generating responses similarly to any other servlet. Coding a JSP page is more convenient than coding the equivalent servlet.
JSP pages are fully interoperable with servlets. A JSP page can include output from a servlet or forward to a servlet, and a servlet can include output from a JSP page or forward to a JSP page.
Here is the code for a simple JSP page, 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! :-)
Figure 6-1 shows the flow of execution when a user runs a JSP page, specifying its URL in the browser. Assume that Hello.jsp
accesses a database.
Because of the .jsp
file name extension, the following steps occur automatically:
Hello.jsp
and producing the file Hello.java
.
Hello.class
.
Hello.class
is executed as a servlet, using the JSP runtime library.
Hello
class accesses the database through JDBC (or perhaps SQLJ), as appropriate, and sends its output to the browser.
For most situations, there are at least two general advantages to using JSP pages instead of servlets:
The OC4J JSP implementation 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 Oracle Application Server Containers for J2EE JSP Tag Libraries and Utilities Reference.
JspScopeListener
for event-handling
In addition, the OC4J JSP container offers integration with caching technologies, documented in the Oracle Application Server Containers for J2EE JSP Tag Libraries and Utilities Reference:
The OC4J JSP container also supports Oracle-specific programming extensions, such as for globalization support. These are documented in the Oracle Application Server Containers for J2EE Support for JavaServer Pages Developer's Guide:
This section shows you how to run the elementary JSP example from earlier in this chapter.
Copy or type the sample code from "What Is JavaServer Pages Technology?" into a file, and save it as welcomeuser.jsp
.
Archive welcomeuser.jsp
into the WAR and EAR files for the JSP primer samples, and deploy the EAR file using the Oracle Enterprise Manager deployment wizard. This is all described in "Creating and Deploying the JSP Primer Samples EAR File".
Assuming you specify a URL mapping of /jspprimer
, as described in "Deploy the EAR File", you can run welcomeuser.jsp
with a URL such as the following:
http://host:port/jspprimer/welcomeuser.jsp
This uses host
to represent the name of the system where Oracle Application Server and the application are installed.
If the JSP were within a subdirectory below the top level of the WAR file, then this directory must be included in the URL. For example, if welcomeuser.jsp
is located in the mydir
directory in the WAR file, you would invoke it as follows:
http://host:port/jspprimer/mydir/welcomeuser.jsp
When you first run the page, you will see something like the following output:
Submitting a name, such as "Amy", rewrites the URL to indicate the input:
http://host:port/jspprimer/welcomeuser.jsp?user=Amy
and updates the page:
As mentioned earlier, JSP technology works nicely as a front-end for business logic and dynamic functionality encapsulated in JavaBeans. This section contains the code for a JavaBean and a JSP page that calls it.
The steps involved are covered in the following sections:
This section lists the source for a JSP page that uses the standard JSP jsp:useBean
tag to invoke a JavaBean. To run the code, you can copy or type it into a file called usebean.jsp
. The numbers in JSP comment statements along the right edge correspond to the notes following the code.
<%@ page import="beans.NameBean" %> <%--1--%>
<jsp:useBean id="pageBean" class="beans.NameBean" scope="page" /><%--2--%>
<jsp:setProperty name="pageBean" property="*" /> <%--3--%>
<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>
page
directive. In this case, it imports the JavaBean class. (There are other uses for page
directives, and other kinds of directives.)
jsp:useBean
tag instantiates the JavaBean, specifying the package name, class name, and instance name. A scope setting of page
specifies that the JavaBean instance is accessible only from the JSP page where it was created. Other possible scopes are request
, session
, and application
.
jsp:setProperty
tag sets the values of one or more properties for the specified bean instance. A property setting of *
results in iteration over the HTTP request parameters, matching bean property names with request parameter names, and setting bean property values according to the corresponding request parameter values. In this case, the only bean property is newName
. This corresponds to the newName
HTTP request parameter specified in the HTML forms code in the page. (For use with the jsp:useBean
tag, in addition to the jsp:setProperty
tag, there is a jsp:getProperty
tag to retrieve parameter values.)
For general information about any of these topics, see the Oracle Application Server Containers for J2EE Support for JavaServer Pages Developer's Guide.
Here is the code for the JavaBean class, NameBean
. The package name specified here must be consistent with the page
directive and the jsp:useBean
tag of the JSP page. To run the JSP page, you can copy or type this code into a file called NameBean.java
and then compile it. The resulting NameBean.class
file must be placed in a beans
subdirectory under the WEB-INF/classes
directory for your application, according to the beans
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; } }
Archive usebean.jsp
and Namebean.class
into the WAR and EAR files for the JSP primer samples, and deploy the EAR file using the Oracle Enterprise Manager deployment wizard. This is all described in "Creating and Deploying the JSP Primer Samples EAR File".
Assuming you specify a URL mapping of /jspprimer
, as described in "Deploy the EAR File", you can run usebean.jsp
with a URL such as the following:
http://host:port/jspprimer/usebean.jsp
This example, as before, uses host
as the name of the system where Oracle Application Server and the application are installed.
When you run this page, you will initially see the following output:
Once you submit a name, such as "Ike", the page is updated (but still prompts you in case you want to enter another name):
The Sun Microsystems JavaServer Pages specification includes standard tags to use in JSP pages to perform various tasks. An example is the jsp:useBean
tag employed in "Running a JSP Page That Invokes a JavaBean". The JSP specification also outlines a standard framework that allows vendors to offer their own custom tag libraries in a portable way.
Each custom tag library has a tag library descriptor (TLD) file that specifies the tags, tag attributes, and other properties of the library. Each tag requires support classes, at least a tag handler class with the code to execute tag semantics. (Tag handler classes implement standard tag interfaces, according to the JSP specification.) These classes must be available to your Web application.
OC4J supplies portable tag libraries with functionality in several areas. This section shows an example that uses tags from the Oracle data-access (SQL) tag library to access and query a database and output the results to the browser. A standard JSP taglib
directive is used to access the TLD file and to specify a tag prefix to use for the tags of this library.
Here are the steps in using a JSP tag library:
The steps involved are covered in the following sections:
For information about the standard tag library framework, including TLD files, tag handler classes, and the taglib
directive, refer to the Oracle Application Server Containers for J2EE Support for JavaServer Pages Developer's Guide.
This section provides the source for a JSP page that uses data-access tags, supplied with OC4J, to open a database connection, run a simple query, and output the results as an HTML table. To run the page, you can copy or type the code into a file called sqltagquery.jsp
. The numbers in JSP comment statements along the right edge correspond to the notes following the code.
<%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ojsp/sqltaglib.tld" prefix="sql" %> <%--1--%>
<HTML> <HEAD> <TITLE>The SQL Tag Query JSP</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF"> <HR> <sql:dbOpen dataSource= "jdbc/OracleDS" > <%--2--%>
<sql:dbQuery> <%--3--%>
select * from EMP </sql:dbQuery> </sql:dbOpen> <%--4--%>
<HR> </BODY> </HTML>
taglib
directive uses a JSP 1.2-style uri
value, which is used as a keyword instead of actually indicating a physical location. (See the Oracle Application Server Containers for J2EE Support for JavaServer Pages Developer's Guide for information about such URI functionality.) The taglib
directive also specifies the tag prefix, "sql
", to use in any tag statements.
OracleDS
is the JNDI name of a data source that is available by default through the OC4J data-sources.xml
file. Verify that the data source points to an appropriate database. For introductory information about data sources, see Chapter 4, "Data Sources Primer".
dbQuery
tag uses the database connection established by the surrounding dbOpen
tag. Also by default, the dbQuery
tag outputs its results as an HTML table. Other choices are JDBC result set, XML string, or XML DOM object.
dbOpen
start-tag, the database connection is automatically closed when the dbOpen
end-tag is reached.
For more information about the data-access tag library that is supplied with OC4J, refer to the Oracle Application Server Containers for J2EE JSP Tag Libraries and Utilities Reference.
JSP applications require some JAR files that are installed with OC4J:
ojsp.jar
(JSP container)
ojsputil.jar
(OC4J tag libraries and utilities)
xmlparserv2.jar
(XML parser)
xsu12.jar
(XML-SQL utility)
The tag handler class files and TLD files, including the tag handlers and TLD file (sqltaglib.tld
) for this example, are in ojsputil.jar
.
Note:
The library |
Archive sqltagquery.jsp
into the WAR and EAR files for the JSP primer samples, and deploy the EAR file using the Oracle Enterprise Manager deployment wizard. This is all described in "Creating and Deploying the JSP Primer Samples EAR File".
Assuming you specify a URL mapping of /jspprimer
, as described in "Deploy the EAR File", you can run sqltagquery.jsp
with a URL such as the following:
http://host:port/jspprimer/sqltagquery.jsp
This page produces output such as the following.
Three examples have been covered in this chapter: a simple JSP page, a page that calls a JavaBean, and a page that uses a tag library. For simplicity, we suggest that you package and deploy them in a single WAR file within an EAR file. This section shows all the required files for the examples and how you can package and deploy them.
Note: Web components, such as JSP pages and servlets, are archived in WAR files. An application consisting of only Web components can be deployed as a WAR file directly; however, the primary J2EE deployment vehicle is an EAR file. An EAR file can include a WAR file as well as other types of archive files for other types of components, such as EJBs. This chapter uses an EAR file. See "Creating and Deploying the Servlet Primer Samples WAR File" for an example using only a WAR file. |
Here is the structure of the EAR file for the examples in this chapter:
jsp-primer-samples.war META-INF/ application.xml manifest.mf
And here is the structure of the nested WAR file:
sqltagquery.jsp usebean.jsp welcomeuser.jsp WEB-INF/ web.xml classes/ beans/ NameBean.class
If you use an IDE for your development, the IDE can usually create the WAR and EAR files for you. Otherwise, you can create them manually using the Java JAR utility or the ant
utility.
However the standard application.xml
file is created (either manually or through an IDE), it must have a <web-uri>
entry that indicates the WAR file name, and a <context-root>
entry that indicates the context path portion of the URL. The DTD for the standard application.xml
file is according to the J2EE specification. Here is a sample file:
<?xml version="1.0" ?> <!DOCTYPE application (View Source for full doctype...)> <application> <display-name>OracleAS JAAS Provider Application: SimpleServlet</display-name> <module> <web> <web-uri>callerInfo-web.war</web-uri> <context-root>/callerInfo</context-root> </web> </module> </application>
Use Oracle Enterprise Manager, the Application Server Control, to deploy the examples. Select the Applications page. Click the Deploy EAR file button to start the deployment wizard.
"Deploying Applications" discusses the steps involved in deploying an application to OC4J. Only three of these steps are required to deploy the examples in this chapter:
In the Deploy Application: Select Application Page, specify the J2EE application and the application name. You can use the same name in both places. In this example, we use jsp-primer-samples
(here and in the application.xml
file).
See "Select Application" for information.
In the Deploy Application: URL Mapping for Web Modules Page, the servlet context path for your application must be specified. Typically, Oracle Enterprise Manager can pick this up from the <context-root>
element of the application.xml
file. For this discussion, assume /jspprimer
is the context path. This will be part of the URL you would use to invoke the application.
See "Provide The URL Mappings For All Web Modules" for information.
In the Deploy Application: Summary Page, you can look over the application summary and then click the Deploy button.
See "Deployment Review" for information.
|
Copyright © 2002, 2003 Oracle Corporation. All Rights Reserved. |
|