No Slide Title

Download Report

Transcript No Slide Title

Chapter 2 How to install and use Tomcat

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 1

Objectives Applied

  Install and configure Tomcat so it runs on your own PC.

Start Tomcat from a DOS prompt window, and shut it down from this window.

 Turn on servlet reloading so you don’t have to stop and restart Tomcat every time you want to change and test a servlet.

 Use your browser to enter a URL that accesses an HTML document, JSP, or servlet.

 If a port conflict occurs, change the port that’s used by Tomcat.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 2

Objectives (continued) Knowledge

 Describe two common errors that occur after you enter a URL into a browser.

 In general terms, describe the directory structure of a Web application that’s defined by the J2EE specfication.

 In general terms, describe the deployment descriptor of a web application.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 3

The Apache web site address www.apache.org

How to install Tomcat from the CD that comes with the book

1.

Put the CD that comes with this book into your CD drive, and navigate to the Tomcat 4.0 directory.

2.

Use a zip program such as WinZip to extract the files from the zip file named jakarta-tomcat-4.0.1. If you don’t have WinZip, you can get it free from

www.winzip.com

.

3.

If necessary, move the jakarta-tomcat directory to the C drive.

4.

Rename the jakarta-tomcat directory to tomcat.

5.

Copy the servlet.jar file from the C:\tomcat\common\lib directory to the C:\j2sdk1.4.0\jre\lib\ext directory.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 4

How to install Tomcat on your own PC

 The directory that holds the files for Tomcat is known as the

Tomcat home directory

. By default, this directory is jakarta tomcat-4.0.X.

 To save yourself typing, we recommend that you rename this directory to tomcat.

 The servlet.jar file contains the Java classes that you need for developing servlets. By copying this JAR file from the tomcat subdirectory to the SDK subdirectory, you make the classes available to the SDK.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 5

The file structure of Tomcat

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 6

The subdirectories of Tomcat Directory

bin classes common conf lib logs server webapps work

Description

The binary executables and scripts Unpacked classes that are available to all web applications Classes available to internal and web applications Configuration files JAR files that contain classes that are available to all web applications Log files Internal classes Web applications Temporary files and directories for Tomcat Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 7

The files of Tomcat

File

readme.txt

running.txt

Description

General information about Tomcat Instructions for installing, starting, and stopping Tomcat Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 8

Notes on Tomcat

 Since Tomcat is an open-source project, developers who work on Tomcat can download the source distribution for Tomcat, modify the source code, and build Tomcat from the source code.

 Developers who develop web applications only need to download and install the binary for Tomcat distribution.

 To make the classes within a JAR file available to Tomcat so you can run an application that uses them, you can put the JAR file in Tomcat’s common\lib directory.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 9

The catalina batch file opened for editing

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 10

How to set the JAVA_HOME environment variable

1.

2.

Use a text editor like Notepad or TextPad to open the catalina.bat

file located in the c:\tomcat\bin directory.

Scroll down to the section named “Save Environment Variables That May Change.” 3.

After the last Set command, enter

set JAVA_HOME=c:\j2sdk1.4.0

where c:\j2sdk1.4.0 is the directory that contains the SDK that’s installed on your system.

4.

Save your changes to the catalina.bat file.

5.

If necessary, stop and restart Tomcat.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 11

The Properties dialog box for the startup batch file

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 12

How to change the memory settings for the startup batch file

1.

Start the Windows Explorer and navigate to the bin directory of Tomcat.

2.

Right-click on the startup batch file and select the Properties option.

3.

From the Properties dialog box, select the Memory tab.

4.

Change the Initial Environment option from Auto to 4096.

5.

Click on the OK button.

How to change the memory settings for the shutdown batch file

 Follow the steps shown above, substituting shutdown for startup.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 13

The server.xml file

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 14

How to turn on servlet reloading

1.

Use the Windows Explorer to navigate to Tomcat’s conf directory and open the server.xml file in a text editor.

2.

Scroll down to the comment shown above and insert the following statement below it:

3.

Save the changes to the server.xml file 4.

If necessary, stop and restart Tomcat.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 15

What is servlet reloading

 If

servlet reloading

isn’t on, which is the default setting, you have to stop and restart Tomcat each time that you change one of the classes that’s in memory.

 If you turn servlet reloading on, Tomcat checks the modification dates of the classes in memory and automatically reloads the ones that have changed so you don’t have to stop and restart Tomcat each time a class changes.

 Although this is useful in development, it can cause performance problems in a production environment.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 16

  

What is the server.xml file

 The server.xml file is an XML file that controls how the Tomcat engine is configured.

Tomcat reads this file every time it starts to configure itself.

You can use a text editor to edit this file.

Then, you can stop and restart Tomcat to put the changes into effect.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 17

DOS commands for starting Tomcat

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 18

The console that Tomcat displays when it’s running

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 19

How to use the DOS Prompt window to start and stop Tomcat

1.

Open a DOS Prompt window and use the cd command to change the current directory to Tomcat’s bin directory.

2.

To start Tomcat, type “startup” and press the Enter key. To stop Tomcat, type “shutdown” and press the Enter key.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 20

What to do if Tomcat doesn’t start because it’s “out of environment space”

  First, make sure that you did the procedures in figure 2-4 correctly.

If you did and you’re using Windows 95, you can then set these variables this way: (1) click on the Title bar of the DOS Prompt window (2) select Properties from the shortcut menu (3) click on the Memory tab (4) change the Initial environment setting to 4096.

 After you close and reopen the DOS Prompt window, this should fix the problem.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 21

A web browser that displays a web page

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 22

The components of an HTTP URL

How to view a web page via an HTTP URL

1.

Start Tomcat.

2.

Start your web browser.

3.

In the Address text box (for the Internet Explorer) or Location text box (for Netscape), type a URL and press Enter.

How to specify the host and port in the URL

 When Tomcat is running on your local machine, you can use the “localhost” keyword to specify the host machine.

 The default port for Tomcat is 8080. If another application is already using this port, you can change the default port.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 23

A web browser that displays a directory listing

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 24

The Internet Explorer’s error page

Why this error may occur

 If the browser displays a message like this, the HTTP request isn’t connecting with a web server.

 To solve this problem, make sure that the Tomcat engine is running, and make sure that you’ve entered a valid URL.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 25

Tomcat’s default 404 error page Why this error may occur

 If the browser displays a Tomcat error page like this, Tomcat is receiving the HTTP request, but it can’t find the requested resource.

 To solve this problem, make sure that you’ve entered the path and filename of the URL correctly.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 26

The server.xml file

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 27

How to change the port that’s used by Tomcat

1.

Use the Windows Explorer to navigate to Tomcat’s conf directory and open the server.xml file in a text editor.

2.

Replace all instances of the current port, which is 8080 by default, to a four-digit number that’s greater than 1024 or to 80. To do this, you may want to use the Find and Replace feature of your text editor.

3.

Save the changes to the server.xml file.

4.

Stop and restart Tomcat.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 28

How to work with ports in Tomcat

 If you have a port conflict with another application, you can change the default port from 8080 to a four-digit number that’s greater than 1024.

 If you don’t enter a port when you specify a URL, your browser will use port 80. As a result, if you change Tomcat’s default port from 8080 to 80, you don’t need to enter a port when entering a URL in the browser. Then, assuming that there isn’t a port conflict, you can view a web page by entering a URL like this: http://localhost/examples/servlets Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 29

The index page for the webapps\murach directory

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 30

How to install the web applications that are on the book’s CD

1.

From the root directory of the CD, double-click on the file named Install.exe and respond to the dialog boxes that follow. This will install the source code and test files in the c:\murach\webapps directory.

2.

Copy the c:\murach\webapps\murach directory and all its subdirectories to c:\tomcat\webapps.

3.

Copy the c:\murach\webapps\musicStore directory and all its subdirectories to c:\tomcat\webapps.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 31

How to view and run the web applications that are on the book’s CD

 To view the index page for all applications stored under the murach directory, start Tomcat and access http://localhost:8080/murach from your browser.

 To view the index page for the musicStore directory, start Tomcat and access http://localhost:8080/musicStore from your browser.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 32

The directory structure for a web application named musicStore

c:\tomcat webapps musicStore (

the document root directory for the application

) admin

(JSP and HTML files for the admin section)

cart

(JSP and HTML files for the cart section)

download

(JSP and HTML files for the download section)

WEB-INF

(contains the web.xml file for the application)

classes music admin (

servlets for the admin section

) business

(Java classes for business objects)

cart (

servlets for the cart section

) data

(Java classes for database access)

download

(servlets for the download section)

etc

(data files and other miscellaneous files)

lib

(JAR files that contain Java classes)

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 33

A summary of the directories and files for a web application Directory

document root \WEB-INF

Description

The

document root directory

is the root directory for a web application. This directory typically contains the index file for the application. In addition, this directory or its subdirectories contain the HTML and JSP files for the application.

This directory contains a file named web.xml. It can be used to configure the servlets and other components that make up the application.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 34

A summary of the directories and files for a web application (continued) Directory

\WEB-INF\classes \WEB-INF\lib

Description

This directory and its subdirectories contain the servlets and other Java classes for your application that aren’t compressed into JAR files. If you’re using Java packages, each package must be stored in a subdirectory that has the same name as the package.

This directory contains any JAR files that contain Java classes that are needed by this web application, but not by other web applications.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 35

How to use the directories and files in a web application

 A Java web application is a hierarchy of directories and files in a standard layout defined by the J2EE specification.

 To organize your Java class files, you can store them in

packages

.

A package of classes must be stored within a subdirectory of the WEB-INF\classes directory.

 If you need to make classes within a JAR file available to more than one web application, you can put the JAR file in Tomcat’s webapps\common\lib directory.

 If you need to make classes within a JAR file available to a single web application, you can put them in the WEB-INF\lib directory for that application.

 You can use the WEB-INF\etc directory to store any files that you don’t want users to be able to access.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 36

A web.xml file EmailResponse email.EmailServlet filename UserEmail.txt

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 37

What is the web.xml file

 Every web application requires a web.xml file in the WEB-INF directory. This file is known as the

deployment descriptor

for the web application.

What the web.xml file can do

 Provide an alias for a servlet class so you can call a servlet using a different name.

 Enable servlet mapping so you can call a servlet using any URL or URL pattern.

 Define initialization parameters for a servlet or the entire application.

  Define error pages for an entire application.

Provide security constraints to restrict access to certain web pages and servlets.

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 38

The directory structure for the book applications

c:\tomcat webapps murach (

the document root directory for the book applications

) email4

(JSP and HTML files for chapter 4)

email5

(JSP and HTML files for chapter 5)

email6

(JSP and HTML files for chapter 6)

cart7

(JSP and HTML files for the cart application in chapter 7)

download7

(JSP and HTML files for the download application in chapter 7)

styles

(style sheets for HTML and JSP pages)

index.htm

(index page for the book applications)

WEB-INF

(contains the web.xml file for the applications in chapters 3-15)

classes business

(Java classes for business objects)

data (

Java classes for IO and database access

) email5 (

servlets for chapter 5

) email6

(servlets for chapter 6)

cart7

(servlets for the cart application in chapter 7)

download7

(servlets for the download application in chapter 7)

util

(Java helper classes)

etc

(data files and other miscellaneous files)

lib

(JAR file for the MySQL database driver)

Java Servlest and JSPCH02 © 2003, Mike Murach & Associates, Inc.

Slide 39