Pressing the Submit button loads the following JavaServer Page:
Example 8.11 Updating a Database with JSTL and JDBC
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<html>
<head>
<title>JSP Examples: JSTL Demo</title>
</head>
<%-- update data --%>
<%
/* SQL code to append parameters to CLASS table */
String strSQL="INSERT INTO sashelp.class SET " + "name=\""+request.getParameter("name")+"\","+
"sex=\""+request.getParameter("sex")+"\","+
"age="+request.getParameter("age")+","+
"height="+request.getParameter("height")+","+
"weight="+request.getParameter("weight");
%>
<sql:setDataSource var="class"
driver="com.sas.rio.MVADriver"
url="jdbc:sasiom://hunding:8591"
Chapter 8 Java Servlets and JavaServer Pages 163
user="sasdemo"
password="sasuser"/>
<sql:update dataSource="${class}">
<%=strSQL %>
</sql:update>
<body style="font-family: verdana, tahoma, arial;">
<p>The class file has been updated.</p>
</body>
</html>
The page uses the sql:update tag to execute the constructed SQL code. Note that for a change of pace, the program uses the IOM JDBC driver instead of the SAS/SHARE driver. When the page is displayed in a browser window, all it says is “The class file has been updated.” By now, you should be able to combine the two JSTL examples to create a dandy interactive Web site for your application.
Web Archive Files
The examples shown so far have all been extremely simple. In the real world, most Web applications include multiple servlets and/or JavaServer Pages, usually linked to a package of classes. In order to make it easier to deploy complex applications, Sun has introduced the concept of Web Application Archive (WAR) files. According to the Java Servlet Specification 2.2, a Web application can be a collection of JSP, servlets, HTML, Java classes, and other resources; see http://java.sun.com/j2ee/tutorial/1_3-
fcs/doc/WCC3.html for a tutorial. A WAR file is essentially just a regular JAR file containing all of the components necessary to deploy a given Web application.
A Web application can be run from a WAR file directly, or from an unpacked directory with the same structure. The top level of the directory is the document root of the application. An example of a document root using Tomcat would be
$TOMCAT_HOME/webapps. As shown in Display 8.2, the webapps directory should contain a subdirectory called WEB-INF, which in turn can contain some or all of the following files and directories:
web.xml – the required Web application deployment descriptor (see Example 8.3
tag library descriptor files (see the following section)
a classes directory that contains any needed servlets, utility classes, and other components
(optional) a lib directory that contains JAR archives of libraries (tag libraries and any utility libraries called by server-side classes)
It is also possible to create package directories in either the document root or the classes directory (see the Sun WebApp tutorial). Note that this is pretty much the default Tomcat structure described above; the WAR file mechanism is just a way to package the contents in a fashion analogous to the familiar ZIP or UNIX TAR files.
Building a Web Archive File
It is possible to create the file manually using the JAR utility supplied with the Java SDK (see http://access1.sun.com/techarticles/simple.WAR.html for an example). The
following Windows batch command will create a small file called BBU.war that contains the JavaServer Page from Example 8.4:
C:\j2sdk1.4.2_04\bin\jar cvf BBU.war HelloWorld.jsp
The three options to the jar command are:
c for create file
v for verbose
f for the name of the file to create
As discussed in the section on servlets, in order to deploy a WAR file that contains a servlet, it is first necessary to create a web.xml application descriptor file. The structure of the deployment descriptor file is fairly complex and will not be described in detail in this book. A good introduction is the Sun tutorial cited previously, as well as various other online sources.3
There are several other ways to create a WAR file. Sun suggests using the War task of the Ant portable build tool, which is available from the Apache Software foundation at http://ant.apache.org/. Also, as will be explained in Chapter 9, SAS AppDev Studio includes a component, the webAF Package Wizard, that can easily create the appropriate content from a webAF project.
Web Application Manager
Once the WAR file has been created, it must be uploaded to the server. You can, of course, copy it manually to the server if you have Write permissions to the appropriate directory. However, Tomcat includes an administration utility called the Web Application Manager that handles this quite simply via the HTML PUT command to port 8080. Enter the URL http://<server-name>:8080/manager/html into your favorite browser window.
You should be prompted for the administrator user name and password. If you have forgotten or do not know the values specified when Tomcat was initially installed, these are set in the configuration file $CATALINA_HOME/conf/tomcat-users.xml, as shown in the following sample:
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager"/>
<role rolename="admin"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="role1" password="tomcat" roles="role1"/>
<user username="both" password="tomcat"
roles="tomcat,role1"/>
<user username="admin" password="system"
roles="manager,admin"/>
</tomcat-users>
3James Goodwill, “Using Tomcat: Java Web Applications,” O'Reilly & Associates, Inc., 2000-2002.
http://www.onjava.com/pub/a/onjava/2001/03/15/tomcat.html.
Chapter 8 Java Servlets and JavaServer Pages 165
Also, be careful typing the URL; it’s not manager.html, but manager/html for the Manager application. Something like the following window should appear: