Testing a Java Servlet using Tomcat

fortunabrontideInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

94 εμφανίσεις

Calif
ornia State Polytechnic University, Pomona

Computer Information Systems Department

Spring 2003 CIS 424
-

Advanced Java Programming Dr. Stumpf


fortunabrontide_c291402f
-
158d
-
45f6
-
96a5
-
92514b824f94.doc

version 1.0
11/13/13


page
1

of
5

Testing a Java Servlet using Tomcat


1.

Java Policy


There is a file called
java.policy

that permits Applets to do certain things. This is
called leaving the sandbox. Even thought it looks like a text file, it will fail if
modified in a text editor. I have

put in three lines that seem to fix the problems
we will run into. You must use my policy file.


This file is substituted for two Java policies in your system. One is in the j2 SDK
in the root, and the other is in the run time j2 SDK in Program Files di
rectory.
The location is


C:
\
j2sdk1.4.0
\
jre
\
lib
\
security and C:
\
Program Files
\
Java
\
j2re1.4.0
\
jre
\
lib
\
security


After substituting these files, you will need to start the system.


2.

Tomcat 1.4.


There are many versions of Tomcat available. The version I

have on the
download site is 4.0. This version which I have used for six months has never
failed me. It installs and sets up everything for you. It even tells you it found the
j2 SDK for you.


Also it allows one to test Java Servlets easily on your own
machine.


The one problem is that Tomcat must be restarted each time a new Servlet is
added. HTML pages with jars do not need it restarted.


3.

Servlet Jar


If you were to load a Servlet in Eclipse, it will have compile errors as the standard
of the j2 S
DK does not support Servlets. However, Tomcat includes a Servlet Jar
which I also have on my web page. By adding this external jar to the Project
properties, the problem goes away.


A screen shot of the Project properties is shown below. Notice the exte
rnal jar
called servlet.jar in
C
\
:eclipse

folder. To add, just click on
Add External JARs
.



Calif
ornia State Polytechnic University, Pomona

Computer Information Systems Department

Spring 2003 CIS 424
-

Advanced Java Programming Dr. Stumpf


fortunabrontide_c291402f
-
158d
-
45f6
-
96a5
-
92514b824f94.doc

version 1.0
11/13/13


page
2

of
5



This will only enable compilation of a Servlet.. Eclipse at present does not
support debugging with Tomcat. It however runs great with Tomcat.


4.

Exporting Se
rvlets to Tomcat.


Go to the Eclipse workspace and copying the package folder and place it in the
Tomcat classes folder under examples.


A sample of such a folder in Tomcat is shown below. The example shows a
folder named
user042
. You will use the folder

called
examples

which is already
present.




Calif
ornia State Polytechnic University, Pomona

Computer Information Systems Department

Spring 2003 CIS 424
-

Advanced Java Programming Dr. Stumpf


fortunabrontide_c291402f
-
158d
-
45f6
-
96a5
-
92514b824f94.doc

version 1.0
11/13/13


page
3

of
5




Note I have a folder called user042. It has four packages in it. The eesprogram4
example is from my fall quarter 2003 CIS 424 class. The Java Bean supports a
JSP page that shows the database so the stude
nts can tell if they corrected wrote to
the database.


5.

Use of MS Access for a database.


You must have MS Access installed. Download the database from your
instructor’s web site. You must then make an ODBC connection to this database
suing the ODBC Data

Sources in the control panel.


Calif
ornia State Polytechnic University, Pomona

Computer Information Systems Department

Spring 2003 CIS 424
-

Advanced Java Programming Dr. Stumpf


fortunabrontide_c291402f
-
158d
-
45f6
-
96a5
-
92514b824f94.doc

version 1.0
11/13/13


page
4

of
5




Then you must open the data source and click add.




The n you must fill in the data souce name.


Calif
ornia State Polytechnic University, Pomona

Computer Information Systems Department

Spring 2003 CIS 424
-

Advanced Java Programming Dr. Stumpf


fortunabrontide_c291402f
-
158d
-
45f6
-
96a5
-
92514b824f94.doc

version 1.0
11/13/13


page
5

of
5



Use the name payroll. The example uses paycheck. This completes the
assignment of a DSN (Data Source Name).



6.

Once
the Servlet is moved to the Tomcat directory and the data source name is
specified, one may then test the Servlet. First Open Tomcat. Second be sure the
host address is:


http://localhost:8080/examples/servlet/xxxprogram4.PayCheckServlet


or


http://localhost:8080/examples/servlet/xxxprogram4.PayCheckStringServlet


depending on which

Servlet you are using. The Servlet is obtained from the
instructor’s web site. Third after it is running, run the program in Eclipse.



7.

A word about debugging. It is wise to in break points in the create method and
follow it closely each step. Also
look at the Tomcat console window. Errors such
as security mean the policy file is not correct. Errors such as EOF (End Of File)
mean the number of elements writing and reads do not agree. A lot of
information is available in the Tomcat console.