Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Application Deployment & JDBC CSC 667, Spring 2004 Dr. Ilmi Yoon.

Similar presentations


Presentation on theme: "Web Application Deployment & JDBC CSC 667, Spring 2004 Dr. Ilmi Yoon."— Presentation transcript:

1 Web Application Deployment & JDBC CSC 667, Spring 2004 Dr. Ilmi Yoon

2 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.htmlhttp://www.onjava.com/pub/a/onjava/2001/0 3/15/tomcat.html http://www.onjava.com/pub/a/onjava/2001/0 4/19/tomcat.htmlhttp://www.onjava.com/pub/a/onjava/2001/0 4/19/tomcat.html

3 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/04- 24/667_files/frame.htm for tips for Ant, TogetherSoft, Tomcat install & deployment http://unicorn.sfsu.edu/~csc667/04- 24/667_files/frame.htm

4 Table 1. The Web Application Directory Structure DirectoryContains /onjava This is the root directory of the web application. All JSP and XHTML files are stored here. /onjava/WEB-INF 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 directory can be served directly to a client. /onjava/WEB-INF/classes This directory is where servlet and utility classes 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.

5 Installation & Setup 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/WEB- INF/classes –Windows set CLASSPATH=.;dir\servlet.jar;dir\jspengine.jar

6 Installation & Setup Compile and Install your servlets –Tomcat install_dir/webpages/WEB-INF/classes install_dir/classes install_dir/lib (non frequently changing classes) install_dir/webapps/ROOT/WEB- INF/classes (3.1) Invoking the servlets –http://host:port/servlet/Packagename.servetN ame use /servlet/ regardless the actual directory name –Register servlet

7 Calling Servlets From a Browser The URL for a servlet has the following general form, where servlet-name corresponds to the name you have given your servlet: http://machine-name:port/servlet/servlet-name 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

8 Web.xml – under WEB_INF The OnJava App 30 TestServlet com.onjava.TestServlet 1 name value The OnJava App 30 TestServlet com.onjava.TestServlet 1 name value

9 Packing the Web Application Web ARchive file (WAR) Command : jar cvf onjava.war. Now you can deploy your web application by simply distributing this file

10 Table 5. The Tomcat Directory Structure /bin /conf This directory contains the main configuration files for Tomcat. The two most important are the server.xml and the global web.xml. /serverThis directory contains the Tomcat Java Archive files. /libThis directory contains Java Archive files that Tomcat is dependent upon. /logsThis directory contains Tomcat's log files. /srcThis directory contains the source code used by the Tomcat server. Once Tomcat is released, it will probably contain interfaces and abstract classes only. /webappsAll web applications are deployed in this directory; it contains the WAR file. /work 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.

11 Steps Involved in Deploying a Web Application to Tomcat 1.Copy your WAR file to the TOMCAT_HOME/webapps directory. 2.Add a new Context entry to the TOMCAT_HOME/conf/server.xml file, setting the values for the path and docBase to the name of your web application. 3. If you look at the TOMCAT_HOME/webapps directory, you will see a new directory matching the name of your WAR file

12 JDBC Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational database –Structured Query Language JDBC –Java Database Connectivity –JDBC driver

13 Relational-Database Model Relational database –Table –Record –Field, column –Primary key Unique data SQL statement –Query –Record sets

14 Manipulating Databases with JDBC Connect to a database Query the database Display the results of the query

15 Connecting to and Querying a JDBC Data Source DisplayAuthors –Retrieves the entire authors table –Displays the data in a JTextArea

16 public class SQLGatewayServlet extends HttpServlet{ private Connection connection; public void init() throws ServletException{ try{ Class.forName("org.gjt.mm.mysql.Driver"); String dbURL = "jdbc:mysql://localhost/murach"; String username = "root"; String password = ""; connection = DriverManager.getConnection( dbURL, username, password); } Create Connection at Init()

17 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. " + i + " row(s) affected."; } statement.close(); } From JDBC Example at course web page

18 public void init() throws ServletException{ connectionPool = MurachPool.getInstance(); } 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); HttpSession session = request.getSession(); session.setAttribute("user", user); String message = "";

19 Processing Multiple ResultSets or Update Counts Execute the SQL statements Identify the result type –ResultSet s –Update counts Obtain result –getResultSet –getUpdateCount

20 JDBC 2.0 Optional Package javax.sql Package javax.sql –Included with Java 2 Enterprise Edition Interfaces in package javax.sql –DataSource –ConnectionPoolDataSource –PooledConnection –RowSet

21 Connection Pooling Database connection –Overhead in both time and resources Connection pools –Maintain may database connections –Shared between the application clients

22 Relational DB and SQL statements This section is self-study section

23 Relational-Database Model Relational-database structure of an Employee table.

24 Relational Database Overview: The books Database Sample books database –Four tables Authors, publishers, authorISBN and titles –Relationships among the tables

25 Relational Database Overview: The books Database

26 Relational Database Overview: The books Database (Cont.)

27

28 Relational Database Overview: The books Database

29 Relational Database Overview: The books Database (Cont.)

30 Fig. 8.11Table relationships in books.

31 Structured Query Language (SQL) SQL overview SQL keywords

32 Basic SELECT Query Simplest format of a SELECT query –SELECT * FROM tableName SELECT * FROM authors Select specific fields from a table –SELECT authorID, lastName FROM authors

33 WHERE Clause specify the selection criteria –SELECT fieldName1, fieldName2, … FROM tableName WHERE criteria SELECT title, editionNumber, copyright FROM titles WHERE copyright > 1999 WHERE clause condition operators –, =, =, <> –LIKE wildcard characters % and _

34 WHERE Clause (Cont.) SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE ‘D%’

35 WHERE Clause (Cont.) SELECT authorID, firstName, lastName FROM authors WHERE lastName LIKE ‘_i%’

36 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 ORDER BY multiple fields –ORDER BY field1 sortingOrder, field2 sortingOrder, … Combine the WHERE and ORDER BY clauses

37 ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName ASC

38 ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName DESC

39 ORDER BY Clause (Cont.) SELECT authorID, firstName, lastName FROM authors ORDER BY lastName, firstName

40 ORDER BY Clause (Cont.) SELECT isbn, title, editionNumber, copyright, price FROM titles WHERE title LIKE ‘%How to Program’ ORDER BY title ASC

41 Merging Data from Multiple Tables: Joining Join the tables –Merge data from multiple tables into a single view –SELECT fieldName1, fieldName2, … FROM table1, table2 WHERE table1.fieldName = table2.fieldName –SELECT firstName, lastName, isbn FROM authors, authorISBN WHERE authors.authorID = authorISBN.authorID ORDER BY lastName, firstName

42 Merging Data from Multiple Tables: Joining (Cont.)

43 INSERT INTO Statement Insert a new record into a table –INSERT INTO tableName ( fieldName1, …, fieldNameN ) VALUES ( value1, …, valueN ) INSERT INTO authors ( firstName, lastName ) VALUES ( ‘Sue’, ‘Smith’ )

44 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’

45 DELETE FROM Statement Remove data from a table –DELETE FROM tableName WHERE criteria DELETE FROM authors WHERE lastName = ‘Jones’ AND firstName = ‘Sue’


Download ppt "Web Application Deployment & JDBC CSC 667, Spring 2004 Dr. Ilmi Yoon."

Similar presentations


Ads by Google