Beruflich Dokumente
Kultur Dokumente
Web Application
With the release of the Java Servlet Specification 2.2 Web Application is a collection of servlets, html pages, classes, and other resources that can be bundled and run on multiple containers from multiple vendors Each web application has one and only one ServletContext http://www.onjava.com/pub/a/onjava/2001/0 3/15/tomcat.html http://www.onjava.com/pub/a/onjava/2001/0 4/19/tomcat.html
Deployment
Deployment descriptor (web.xml) Web applications can be changed without stopping the server With a standardized deployment comes standardized tools Check http://unicorn.sfsu.edu/~csc667/0424/667_files/frame.htm for tips for Ant, TogetherSoft, Tomcat install & deployment
Directory
Contains
/onjava
This is the root directory of the web application. All JSP and XHTML files are stored here.
/onjava/WEB-INF
/onjava/WEB-INF/classes
This directory contains all resources related to the application that are not in the document root of the application. This is where your web application deployment descriptor is located. Note that the WEB-INF directory is not part of the public document. No files contained in this This directory is where servlet and utility classes directory can be served directly to a client. are located.
/onjava/WEB-INF/lib
This directory contains Java Archive files that the web application depends upon. For example, this is where you would place a JAR file that contained a JDBC driver.
Update CLASSPATH
Identify the Classes (jsp.jar, jspengine.jar, servlet.jar, jasper.jar) to the java Compiler Unix
CLASSPATH=${TOMCAT_HOME}/webserver.jar CLASSPATH=${CLASSPATH}:${TOMCAT_HOME}/webserver.jar CLASSPATH=${CLASSPATH}:${TOMCAT_HOME}/lib/servlet/jar CLASSPATH=${CLASSPATH}:${TOMCAT_HOME}/lib/jsper.jar CLASSPATH=${CLASSPATH}:${TOMCAT_HOME}/examples/WEBINF/classes
Windows
set CLASSPATH=.;dir\servlet.jar;dir\jspengine.jar
http://host:port/servlet/Packagename.servetN ame use /servlet/ regardless the actual directory name Register servlet
Servlet URLs can contain queries, such as for HTTP GET requests. For example, if the servlet's name is bookdetails; the URL for the servlet to GET and display all the information about the bookstore's featured book is: http://localhost:8080/servlet/bookdetails?bookId= 203
<web-app> <display-name>The OnJava App</display-name> <sessiontimeout>30</session-timeout> <servlet> <servlet-name>TestServlet</servlet-name> <servletclass>com.onjava.TestServlet</servlet-class> <load-on-startup>1</load-on-startup> <initparam> <param-name>name</param-name> <param-value>value</param-value> </initparam> </servlet> </web-app>
/bin
This directory contains the main configuration files for Tomcat. The two most important are the server.xml and the global web.xml. This directory contains the Tomcat Java Archive files. This directory contains Java Archive files that Tomcat is dependent upon. This directory contains Tomcat's log files. This directory contains the source code used by the Tomcat server. Once Tomcat is released, it will probably contain interfaces and abstract classes only. All web applications are deployed in this directory; it contains the WAR file. This is the directory in which Tomcat will place all servlets that are generated from JSPs. If you want to see exactly how a particular JSP is interpreted, look in this directory.
/webapps /work
JDBC
Database
DBMS SQL
Collection of data Database management system Storing and organizing data Relational database Structured Query Language Java Database Connectivity JDBC driver
JDBC
Relational-Database Model
Relational database
Table Record Field, column Primary key
Unique data
SQL statement
Query Record sets
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String sqlStatement = request.getParameter("sqlStatement"); String message = "";
try{
Statement statement = connection.createStatement(); sqlStatement = sqlStatement.trim(); String sqlType = sqlStatement.substring(0, 6); if (sqlType.equalsIgnoreCase("select")){ ResultSet resultSet = statement.executeQuery(sqlStatement); // create a string that contains a HTML-formatted result set message = SQLUtil.getHtmlRows(resultSet); } else { int i = statement.executeUpdate(sqlStatement); if (i == 0) // this is a DDL statement message = "The statement executed successfully."; else // this is an INSERT, UPDATE, or DELETE statement message = "The statement executed successfully.<br>" + i + " row(s) affected."; } statement.close();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ Connection connection = connectionPool.getConnection(); String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String emailAddress = request.getParameter("emailAddress"); User user = new User(firstName, lastName, emailAddress);
Obtain result
getResultSet getUpdateCount
Package javax.sql
Included with Java 2 Enterprise Edition
Connection Pooling
Database connection
Overhead in both time and resources
Connection pools
Maintain may database connections Shared between the application clients
Relational-Database Model
Numb Name er
23603 24568 Row /Rec ord 34589 35761 47132 78321 Prima ry key Jones Kerwin Larson M yers Neum ann Step he ns
Depa t rtmen
413 413 642 611 413 611 Column/ Fie ld
Sala ry
1100 2000 1800 1400 9000 8500
Loca tion
New Jersey New Jersey Los Ang ele s Orla nd o New Jersey Orla nd o
Field authorID
2 Paul Deitel 3 Tem Nieto 4 Sean Santry Fig. 8.4 Da ta fro m the authors ta b le o f books.
publisherID 1
Copyright year of the book (a string). Publishers ID number (an integer). This value must correspond to an ID number in the publishers table. imageFile Name of the file containing the books cover image (a string). price Suggested retail price of the book (a real number). [Note: The prices shown in this book are for example purposes only.] Fig. 8.9 titles ta b le fro m books.
au SBN thorI a ID uthor isbn pu ers blish publi D sherI p herNa ublis me
title s isbn ti tle e nNumb ditio er copy right publi D sherI imag eFile pr ice
WHERE Clause
specify the selection criteria
firstName Harvey
lastName Deitel
authorID 3
firstName Tem
lastName Nieto
Fig. 8.16 T he o nly a utho r fro m the authors ta b le w ho se la st na m e c o nta ins i a s the se c o nd le tte r.
ORDER BY Clause
Optional ORDER BY clause
SELECT fieldName1, fieldName2, FROM tableName ORDER BY field ASC SELECT fieldName1, fieldName2, FROM tableName ORDER BY field DESC
firstName Paul
lastName Deitel
Harvey Deitel Tem Nieto Sean Santry Autho rs fro m ta b le authors in a sc e nd ing o rd e r b y lastName.
Tem Nieto Paul Deitel Harvey Deitel Autho rs fro m ta b le authors in d e sc e nd ing o rd e r b y lastName.
2 Paul Deitel 3 Tem Nieto 4 Sean Santry Fig. 8.19 Autho rs fro m ta b le authors in a sc e nd ing o rd e r b y lastName a nd b y firstName.
C How to Program 2 1994 49.95 C How to Program 3 2001 69.95 C++ How to Program 2 1998 49.95 C++ How to Program 3 2001 69.95 Internet and World Wide Web How to 1 2000 69.95 Program 0130284181 Perl How to Program 1 2001 69.95 0134569555 Visual Basic 6 How to Program 1 1999 69.95 0130284173 XML How to Program 1 2001 69.95 013028419x e-Business and e-Commerce How to 1 2001 69.95 Program Fig. 8.20 Bo o ks fro m ta b le titles w ho se title e nd s w ith How to Program in a sc e nd ing o rd e r b y title.
Harvey Deitel 0130829277 Paul Deitel 0138993947 Harvey Deitel 0130852473 Paul Deitel 0130125075 Harvey Deitel 0138993947 Paul Deitel 0130856118 Harvey Deitel 0130125075 Paul Deitel 0130161438 Harvey Deitel 0130856118 Paul Deitel 013028419x Harvey Deitel 0130161438 Paul Deitel 0139163050 Harvey Deitel 013028419x Paul Deitel 0135289106 Harvey Deitel 0139163050 Paul Deitel 0130895717 Harvey Deitel 0135289106 Paul Deitel 0132261197 Harvey Deitel 0130895717 Paul Deitel 0130895725 Harvey Deitel 0132261197 Tem Nieto 0130284181 Harvey Deitel 0130895725 Tem Nieto 0130284173 Paul Deitel 0130895601 Tem Nieto 0130829293 Paul Deitel 0130284181 Tem Nieto 0134569555 Paul Deitel 0130284173 Tem Nieto 0130856118 Paul Deitel 0130829293 Tem Nieto 0130161438 Paul Deitel 0134569555 Tem Nieto 013028419x Paul Deitel 0130829277 Sean Santry 0130895601 Fig. 8.21 A utho rs a nd the IS BN num b e rs fo r the b o o ks the y ha ve w ritte n in a sc e nd ing o rd e r b y lastName a nd firstName.
Paul Deitel Tem Nieto Sean Santry Sue Smith T b le Authors a fte r a n INSERT INTO o p e ra tio n to a d d a re c o rd . a
UPDATE Statement
Modify data in a table
UPDATE tableName SET fieldName1 = value1, , fieldNameN = valueN WHERE criteria
UPDATE authors
SET lastName = Jones WHERE lastName = Smith AND firstName = Sue
authorID 1 2 3 4 5 Fig. 8.23 firstName Harvey lastName Deitel
Paul Deitel Tem Nieto Sean Santry Sue Jones T b le authors a fte r a n UPDATE o p e ra tio n to c ha ng e a re c o rd . a
Paul Deitel Tem Nieto Sean Santry T b le authors a fte r a DELETE o p e ra tio n to re m o ve a re c o rd . a