Oracle9iAS Containers for J2EE User's Guide Release 2 (9.0.2) Part Number A95880-01 |
|
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".
This section provides a quick overview of the following:
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! :-)
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:
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
.)
Hello.class
.
Hello.class
is executed as a servlet, using the JSP runtime library.
Hello
class accesses the database through JDBC or 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:
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.
This section shows you how to run the JSP example from "What Is JavaServer Pages Technology?", and assumes the following:
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.
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:
Submitting a name, such as Amy, updates the page, as shown in the next screen.
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:
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>
page
directive that imports the JavaBean class.
useBean
tag instantiates the JavaBean, specifying the package and class name and the instance name.
page
specifies that the JavaBean instance is accessible only from the JSP page where it was created.
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.
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; } }
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:
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.
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:
taglib
directive in your JSP code that specifies the location and name of the TLD file as well as the tag prefix to use in your code.
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.
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>
taglib
directive to specify the name and location of the TLD file for the SQL tag library; this must indicate where you placed the file. Alternatively, you can use a shortcut URI that you designate through taglib-uri
and taglib-location
specifications in the web.xml
file.
scott
with password tiger
to a database with SID orcl
through port 5521 of the system dbasehost
. Update the code to substitute an appropriate user name, password, and URL if you want to run the page.
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.
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
.
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.
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.
JspScopeListener
for event handling
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.
The SQLJ distribution documents and supplies a demo of a SQLJ-specific connection bean to support simplified connection management for SQLJ code in JSP pages.
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|