Вы находитесь на странице: 1из 9

Tomcat Setup Notes - Webserver-on-a-stick

V2.7 yellow in cmd & ...

Contents
Tomcat Setup Notes - Webserver-on-a-stick ..................................................................1 Contents...............................................................................................................................1 Versions...............................................................................................................................2 Summary of Steps in the development process of Servlets, HTML and JSPs....................2 What is Tomcat?..................................................................................................................2 What is a Web application?.................................................................................................2 Copy the web server (Tomcat) to your USB drive..............................................................3 What if I want to work from home?.....................................................................................3 Basic DOS commands.........................................................................................................4 Test the web server..............................................................................................................5 Setting up Tomcat for your own pages................................................................................6 How to test an html file? .....................................................................................................7 How to test a jsp file? ..........................................................................................................7 How to test a servlet? ..........................................................................................................7 What text editor should I use to create my files?.................................................................7 Exercise ...............................................................................................................................8 Additional notes:..................................................................................................................9 Create or copy or locate a database (later Week 6?)............................................................9 What does the database store information on?....................................................................9 How many tables are there?.................................................................................................9 What does each table represent?..........................................................................................9 What does each field within each table represent?..............................................................9 Have a look at the connections file for the db. (later week 6?)...........................................9

68385593.doc

Page 1 of 9

Versions
Tomcat 6 JDK 1.6 Access 2003

Summary of Steps in the development process of Servlets, HTML and JSPs


1. Place .htm or .jsp files in webapps/yourdir.

Place .java files in c:/mywebserver/webapps/yourdir/WEB-INF/classes


2. Compile java servlet files using DOS terminal window:

javac NameOfFile.java
3. Test using

http://localhost/mydir/file.htm or http://localhost/mydir/file.jsp or for servlets http://localhost/yourdir/servlet/NameOfFile

What is Tomcat?
Tomcat is an open source, Java-based Web application container that runs servlet and JSP Web applications. Tomcat is supported and maintained under the Apache-Jakarta Project http://jakarta.apache.org/tomcat/index.html subproject by volunteers from the open source Java community. Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and Java Server Pages technologies. Tomcat can act as a simple standalone web server for web applications using HTML, JSP and Java Servlets.

What is a Web application?


A web application is basically a web site that can interact with the user and can make changes in a database. It can consist of static HTML pages, Servlets and JSPs. Functions of Tomcat: When the web server gets a request for a servlet, the webserver gives the servlet the HTTP request and response and it calls the servlets methods (we will learn these methods next week). Tomcat has a number of other functions including: controlling of the life and death of the servlets, multithreading (creating a new java thread for every servlet request it receives), supporting declarative security through the so-called deployment descriptor (more on this later), and translating jsp files into servlets.

68385593.doc

Page 2 of 9

We will be using a version of Tomcat I have edited from a Marty Hall download. It will contain the following folder structure that will enable you to develop and deploy your web applications. webapps

week1

week2

cwk

WEB-INF

*.html

*.jsp

classes

web.xml

*.class

Figure 1 Figure 1 illustrates the folder structure that you will find in your USB deployment environment. All static .html files and .jsp files must be placed in the webapps folder. These should be organised in subfolders of the webapps folder. The file web.xml must be in the WEB-INF folder. This file is the deployment descriptor file that can be used to define the mapping between the logical name used for a request and the actual servlet .class file. The servlet class files must be placed in the classes folder, subfolder of WEBINF.

Copy the web server (Tomcat) to your USB drive.


(After unzipping and then deleting the zip) Copy the complete web server subdirectory to the root of your USB drive this will take a few minutes. Leave the directory name as is it will then be easier to follow these notes. The web server you can download from the website

What if I want to work from home?


You need to make sure you have the Java Development Kit installed on your home PC. It can be downloaded from the Sun web site (http://java.sun.com/javase/downloads/index.jsp). The Sun free tutorial can also help in getting Java up and running, which you should really have done in your first year!

68385593.doc

Page 3 of 9

(http://java.sun.com/docs/books/tutorial/) You will also need to edit go_north.bat to reflect your home directories which will almost certainly be different from the labs. I suggest you do as I do and keep two batch files one called go_north for work at the University and one called go_home for working at home.

Basic DOS commands

History of commands can be recalled by and dir --- lists your files cd dirname --- changes directory (you can use wildcards *) cd .. changes to one level up from your current position

First set the environment variables up at the University this has been done for you in a batch file called go_north in the bin directory. Its on the USB drive - it will be e: here at North Below is an actual session in the labs. (Your drive may not be e: ) You will need to open up a DOS prompt one way of doing this is Start ->Run -> Cmd
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\>e: E:\>cd mywebserver E:\mywebserver>cd bin E:\mywebserver\bin>go_north E:\>set PATH="C:\Program Files\Java\jdk1.6.0_02\bin";C:\WINDOWS\system32;C:\WIND OWS;C:\WINDOWS\system32\wbem;C:\WINDOWS\system32\nls;C:\WINDOWS\syst em32\nls\eng lish;c:\program files\intel\dmix;c:\program files\quicktime\qtsystem\;c:\Program Files\Hummingbird\Connectivity\10.00\Accessories\;;c:\program files\novell\zenw orks\;C:\Program Files\MATLAB\R2007a\bin;C:\Program Files\MATLAB\R2007a\bin\win3 2;Z:.; E:\>set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_02 E:\>set CLASSPATH=.;e:\mywebserver\lib\servletapi.jar;e:\mywebserver\lib\jsp-ap i.jar;e:\mywebserver\lib\el-api.jar;e:\Servlets+JSP;..;..\..

68385593.doc

Page 4 of 9

E:\mywebserver\bin>startup Using CATALINA_BASE: E:\mywebserver Using CATALINA_HOME: E:\mywebserver Using CATALINA_TMPDIR: E:\mywebserver\temp Using JRE_HOME: C:\Program Files\Java\jdk1.6.0_02 E:\mywebserver\bin>shutdown Using CATALINA_BASE: E:\mywebserver Using CATALINA_HOME: E:\mywebserver Using CATALINA_TMPDIR: E:\mywebserver\temp Using JRE_HOME: C:\Program Files\Java\jdk1.6.0_02 E:\mywebserver\bin> You are now ready to test pages in the browser.

Test the web server


Open up Firefox or Internet Explorer. Type in the address of the running web server (Tomcat) http://localhost/mywebsite (you should not need to type in a port (:8080) I have edited a configuration file to use default port 80. (Thanks to Marty Hall) You should see the default home page of mywebsite

68385593.doc

Page 5 of 9

See if you can locate the index.jsp source file for this page on the web server. It will be under webapps\mywebsite Now try shutting down the web server. At the MSDOS prompt (the same one that you used to start the web server) type shutdown.

Setting up Tomcat for your own pages


Now create a subdirectory on Tomcat where you will store your files. This should be under the webapps directory I have called it week1 in the following notes. Also create a subdirectory in this week1 folder called week1\WEB-INF\classes. You will need to do this when you create servlets. Note the backslash \ 68385593.doc Page 6 of 9

You may need to restart the web server this is necessary so that it will see the new directory. Now create a file using notepad and save it in the directory youve just created maybe call it test.htm or test.jsp. This file can contain just the following <html> I am a page called Test jsp </html> Try putting the address of this file in the browser address bar and verify that it appears http://localhost/week1/test.jsp - note the forward slash / Good youve created your own web site with one page! This is not trivial - well done!

How to test an html file?


First download the file examples.zip. After unzipping the file you will find in the folder the file hello.html. Put the file hello.html in the week1 folder. Test hello.html.

How to test a jsp file?


This is very similar to what you did before. Put the files simple.jsp and HelloWorld.jsp to the week1 folder on the webserver. Test the jsp. Note: Be careful with the names of the files. These are case-sensitive. And no spaces!

How to test a servlet?


put the file HelloWorld.java in the classes folder Change the current directory to classes Compile HelloWorld.java by typing javac HelloWorld.java After compilation finishes you will see that there is a file HelloWorld.class Test this servlet in the browser Note the difference that in the URL servlet is in the singular and that the compiled servlets reside in the classes subdirectory

What text editor should I use to create my files?


You can use any text editor such as notepad or PFE or Dreamweaver.

68385593.doc

Page 7 of 9

Exercise
Copy the following java code below in a new file that you create with the text editor of your choice and save it as FirstServlet.java in webapps\week1\WEB-INF\classes. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); //PrintWriter out = response.getWriter(); out.println("<HTML>\n" + "<HEAD><TITLE>First Servlet</TITLE></HEAD>\n" + "<BODY >\n" + "<H1>My first Servlet</H1>\n" + "</BODY></HTML>"); } } Compile FirstServlet.java. You will see the following message: % javac FirstServlet.java FirstServlet.java:12: cannot find symbol symbol : variable out location: class FirstServlet out.println("<HTML>\n" + ^ 1 error You need to correct the error. The message above says that: on line 12: cannot find symbol symbol : variable out. The reason for that is that we have the line PrintWriter out = response.getWriter(); commented out. Uncomment this line and save the file FirstServlet.java. Compile FirstServelt.java. It should compile without errors. Now test the servlet

68385593.doc

Page 8 of 9

Additional notes:
If you feel that you need to refresh your java skills you can use the following lab session exercises kindly provided by James King: http://www.city.londonmet.ac.uk/~cambridg/db2010/java_lab1.doc http://www.city.londonmet.ac.uk/~cambridg/db2010/java_lab2.doc http://www.city.londonmet.ac.uk/~cambridg/db2010/java_lab3.doc

Create or copy or locate a database (later Week 6?)


The test database is in the module zip file on the website You need to understand the structure of this database or subsequent work on the database will mean little. What does the database store information on? How many tables are there? What does each table represent? What does each field within each table represent?

Have a look at the connections file for the db. (later week 6?)
The web pages which will be in Java will need to talk to the database we will be using the JDBC-ODBC bridge driver When you've finished installing and testing the Tomcat server and setting up the database driver you can try the JSP exercises.

68385593.doc

Page 9 of 9

Вам также может понравиться