|
Free Open Book
MySQL Cookbook |
16.4 Using Tomcat to Run Web Scripts16.4.1 ProblemYou want to run Java-based programs in a web environment. 16.4.2 SolutionWrite programs using JSP notation and execute them using a servlet container. 16.4.3 DiscussionAs described in Recipe 16.3, Apache can be used to run Perl, PHP, and Python scripts. For Java, a different approach is needed, because Apache doesn't serve JSP pages. Instead, we'll use Tomcat, a server designed for processing Java in a web environment. Apache and Tomcat are very different servers, but there is a familial relationship—Tomcat is part of the Jakarta Project, which is overseen by the Apache Software Foundation. This section provides an overview of JSP programming with Tomcat, but makes several assumptions:
I recognize that this is a lot to assume, because the use of JSP and Tomcat in the MySQL world is not so widespread as the use of our other languages with Apache. If you're unfamiliar with JSP or need instructions for installing Tomcat, Appendix B provides the necessary background information. Once you have Tomcat in place, you should install the following components so that you can work through the JSP examples in this book:
I'll discuss how to install these components, provide a brief overview of some of the JSTL tags, and then describe how to write the JSP equivalent of the MySQL show-tables script that was implemented in Recipe 16.3 using Perl, PHP, and Python. 16.4.4 Installing the mcb ApplicationWeb applications for Tomcat typically are packaged as WAR (web archive) files and installed under its webapps directory, which is roughly analogous to Apache's htdocs document root directory. The recipes distribution includes a sample application named mcb that you can use for trying the JSP examples described here. Look in the distribution's tomcat directory, where you will find a file named mcb.war. Copy that file to Tomcat's webapps directory. Here's an example installation procedure for Unix, assuming that the recipes distribution and Tomcat are located at /u/paul/recipes and /usr/local/jakarta-tomcat. The command to install mcb.war would look like this: % cp /u/paul/recipes/tomcat/mcb.war /usr/local/jakarta-tomcat/webapps For Windows, if the relevant directories are D:\recipes and D:\jakarta-tomcat, the command looks like this: C:\> copy D:\recipes\tomcat\mcb.war D:\jakarta-tomcat\webapps After copying the mcb.war file to the webapps directory, restart Tomcat. As distributed, Tomcat is configured by default to look for WAR files under webapps when it starts up and automatically unpack any that have not already been unpacked. This means that copying mcb.war to the webapps directory and restarting Tomcat should be enough to unpack the mcb application. When Tomcat finishes its startup sequence, look under webapps and you should see a new mcb directory under which are all the files contained in mcb.war. (If Tomcat doesn't unpack mcb.war automatically, see the sidebar Unpacking a WAR File Manually.) If you like, have a look around in the mcb directory at this point. It should contain several files that clients can request using a browser. There should also be a WEB-INF subdirectory, which is used for information that is private—that is, available for use by scripts in the mcb directory, but not directly accessible by clients. Next, verify that Tomcat can serve pages from the mcb application context by requesting some of them from your browser. The following URLs request in turn a static HTML page, a servlet, and a simple JSP page: http://tomcat.snake.net:8080/mcb/test.html http://tomcat.snake.net:8080/mcb/servlet/SimpleServlet http://tomcat.snake.net:8080/mcb/simple.jsp Adjust the hostname and port number in the URLs appropriately for your installation. 16.4.5 Installing the JDBC DriverThe JSP pages in the mcb application need a JDBC driver for connecting to the cookbook database. The following instructions describe how to install the MySQL Connector/J driver; the installation procedure for other drivers should be similar. To install MySQL Connector/J for use by Tomcat applications, place a copy of it in Tomcat's directory tree. Assuming that the driver is packaged as a JAR file (as is the case for MySQL Connector/J), there are three likely places under the Tomcat root directory where you can install it, depending on how visible you want the driver to be:
I recommend installing a copy of the driver in the common/lib directory. That gives it the most global visibility (it will be accessible both by Tomcat and by applications), and you'll need to install it only once. If you enable the driver only for the mcb application by placing a copy in mcb/WEB-INF/lib, but then develop other applications that use MySQL, you'll need to either copy the driver into those applications or move it to a more global location. Making the driver more globally accessible also is useful if you think it likely that at some point you'll elect to use JDBC-based session management or realm authentication. Those activities are handled by Tomcat itself above the application level, so Tomcat needs access to the driver to carry them out. Here's an example installation procedure for Unix, assuming that the MySQL Connector/J driver and Tomcat are located at /src/Java/mysql-connector-java-bin.jar and /usr/local/jakarta-tomcat. The command to install the driver would look like this: % cp /src/Java/mysql-connector-java-bin.jar /usr/local/jakarta-tomcat/common/lib For Windows, if the components are installed at D:\mysql-connector-java-bin.jar and D:\jakarta-tomcat, the command looks like this: C:\> copy D:\mysql-connector-java-bin.jar D:\jakarta-tomcat\common\lib After installing the driver, restart Tomcat and then request the following mcb application page to verify that Tomcat can find the JDBC driver properly: http://tomcat.snake.net:8080/mcb/jdbc_test.jsp You may need to edit jdbc_test.jsp first to change the connection parameters. 16.4.6 Installing the JSTL DistributionMost of the scripts that are part of the mcb sample application use JSTL, so it's necessary to install it or those scripts won't work. To install a tag library into an application context, copy the library's files into the proper locations under the application's WEB-INF directory. Generally, this means installing at least one JAR file and a tag library descriptor (TLD) file, and adding some tag library information to the application's web.xml file. JSTL actually consists of several tag sets, so there are there are several JAR files and TLD files. The following instructions describe how to install JSTL for use with the mcb application:
After installing JSTL, restart Tomcat and request the following mcb application page to verify that Tomcat can find the JSTL tags properly: http://tomcat.snake.net:8080/mcb/jstl_test.jsp 16.4.7 Writing JSP Pages with JSTLThis section discusses the syntax for some of the JSTL tags used most frequently by mcb JSP pages. The descriptions are very brief, and many of these tags have additional attributes that allow them to be used in ways other than those shown here. For more information, consult the JSTL specification (see Appendix C). A JSP page that uses JSTL must include a taglib directive for each tag set that the page uses. Examples in this book use the core and database tags, identified by the following taglib directives: <%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> The uri values should match the symbolic values that are listed in the web.xml <taglib> entries (see Recipe 16.4.6"). The prefix values indicate the initial string used in tag names to identify tags as part of a given tag library. JSTL tags are written in XML format, using a special syntax for tag attributes to include expressions. Within tag attributes, text is interpreted literally unless enclosed within ${...}, in which case it is interpreted as an expression to be evaluated. The following tags are part of the JSTL core tag set:
The JSTL database tags are used to issue queries and access their results:
The contents of a result set returned by <sql:query> are accessible several ways. Assuming that a result set is available through a variable rs, row i of the result can be accessed either as rs.rows[i] or as rs.rowsByIndex[i], where row number values begin at 0. The first form produces a row with columns that can be accessed by name. The second form produces a row with columns that can be accessed by column number (beginning with 0). For example, if a result set has columns named id and name, you can access the values for row three using column names like this: <c:out value="${rs.rows[2].id}" />
<c:out value="${rs.rows[2].name}" />
To use column numbers instead, do this: <c:out value="${rs.rowsByIndex[2][0]}" />
<c:out value="${rs.rowsByIndex[2][1]}" />
You can also use <c:forEach> as an iterator to loop through the rows in a result set. Iterate through rs.rows if you want to access column values by name: <c:forEach var="row" items="${rs.rows}">
id = <c:out value="${row.id}" />,
name = <c:out value="${row.name}" />
<br />
</c:forEach>
Iterate through rs.rowsByIndex to access column values by number: <c:forEach var="row" items="${rs.rowsByIndex}">
id = <c:out value="${row[0]}" />,
name = <c:out value="${row[1]}" />
<br />
</c:forEach>
The row count is available as rs.rowCount: Number of rows selected: <c:out value="${rs.rowCount}" />
Names of the columns in the result set are available using rs.columnNames: <c:forEach var="name" items="${rs.columnNames}">
<c:out value="${name}" />
<br />
</c:forEach>
16.4.8 Writing a MySQL Script using JSP and JSTLRecipe 16.3 shows how to write Perl, PHP, and Python versions of a script to display the names of the tables in the cookbook database. With the JSTL tags, we can write a JSP page that provides that information as follows: <%-- show_tables.jsp - Issue SHOW TABLES query, display results --%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/sql" prefix="sql" %>
<%@ include file="/WEB-INF/jstl-mcb-setup.inc" %>
<html>
<head>
<title>Tables in cookbook Database</title>
</head>
<body bgcolor="white">
<p>Tables in cookbook database:</p>
<sql:query var="rs" dataSource="${conn}">
SHOW TABLES
</sql:query>
<c:forEach var="row" items="${rs.rowsByIndex}">
<c:out value="${row[0]}" /><br />
</c:forEach>
</body>
</html>
The taglib directives identify which tag libraries the script needs, and the include directive pulls in the code that sets up a data source for accessing the cookbook database. The rest of the script generates the page content. This script should be installed in the mcb subdirectory of your Tomcat server's webapps directory, and you can invoke it as follows: http://tomcat.snake.net:8080/mcb/show_tables.jsp Like the PHP script shown in Recipe 16.3, the JSP script does not produce any Content-Type: header explicitly. The JSP engine produces a default header with a content type of text/html automatically.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |