Tomcat and Servlets - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Tomcat and Servlets

Description:

Installing and Compiling Servlets ... Step 2 -- Compile the Java class ... Work -- intermediate files (such as compiled JSP files) during its work. ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 26
Provided by: hai104
Category:

less

Transcript and Presenter's Notes

Title: Tomcat and Servlets


1
Tomcat and Servlets
  • Haifeng Liu

2
Tutorial Overview
  • Tomcat Overview
  • Setting Up Tomcat
  • Running Tomcat
  • Installing and Running Servlets
  • FAQ?

3
Tomcat Overview
  • Tomcat is the servlet container
  • History
  • ASF JServ performance
  • Sun Servlet Engine -- specification
  • Requirements and Quality Goals
  • Strict adherence to Suns JSP/Servlet
    specification, Interoperability, Modifiability,
    Performance, Scalability, High-Availability,
    Security,

--- Jakarta Tomcat
4
Setting Up Tomcat on CDF
  • Status Installed with user privileges in
  • /u/csc309h/lib/jakarta-tomcat-5.0.28
  • Step 1 go to website, read instructions
  • http//www.cdf.toronto.edu/radu/csc309/guide/tomc
    at.html
  • Step 2 download the tar file tomcat.tar.gz
  • Step 3 untar it (tar xvz tomcat.tar.gz) and
    copy the files into a working directory which is
    refered as
  • e.g. /csc309/
  • Inside tomcat
  • Bin, conf, logs, webapps, work

5
Start Tomcat Server
  • Step 1 -- cd /csc309/tomcat/bin
  • Step 2 -- Run the script start.sh (may need to
    chmod ax .sh ) ./start.sh
  • Step 3 -- Enter the port number. Tomcat will use
    3 ports, the number entered and the next two
    (e.g., if you are assigned port 32000, tomcat
    will use 32000, 32001, 32002).
  • Step 4 check /bin/ps -ef grep your_user_id
  • Step 5 view http//localhostyour_port_number

6
(No Transcript)
7
Stop Tomcat Server
  • To stop your server, run bin/stop.sh.
  • Always remember to stop your server before
    logging off CDF.

8
Installing and Compiling Servlets
  • Step 1 -- Download the sample files, untar it,
    put into webapps
  • Step 2 -- Compile the Java class
  • Include the following jar file in your CLASSPATH
    /u/csc309h/lib/tomcat-5.0.27/common/lib/servlet-ap
    i.jar. setenv CLASSPATH CLASSPATH
    /u/csc309h/lib/tomcat-5.0.27/common/lib/servlet-ap
    i.jar
  • cd /csc309/tomcat/webapps/csc309/WEB-INF/classes
  • Compile the servlet javac HelloWorld.java
  • Step 3 -- Start Tomcat
  • Step 4 Start Browser http//127.0.0.1yourPort
    Number/csc309/servlet/HelloWorld

9
HelloWorld Interface
10
Adding a Servlet to a Web Application
  • Step 1 Download PrintEnv.java and copy it to
    /csc309/tomcat/webapps/csc309/WEB-INF/classes
  • Step 2 -- Compile PrintEnv.java
  • Step 3 Add the following entries to the
    application descriptor located at
    /csc309/tomcat/webapps/csc309/WEB-INF/web.xml.
  • ltservletgt
  • ltservlet-namegtPrintEnvlt/servlet-namegt
  • ltservlet-classgtPrintEnvlt/servlet-classgt
  • lt/servletgt
  • ltservlet-mappinggt
  • ltservlet-namegtPrintEnvlt/servlet-namegt
  • lturl-patterngt/servlet/PrintEnvlt/url-patterngt
  • lt/servlet-mappinggt
  • Step 4 -- Restart Tomcat
  • Step 5 Open browser to http//127.0.0.1yourPort
    Number/csc309/servlet/PrintEnv

11
PrintEnv Interface
12
Install Tomcat at homeSoftware Prerequisite
  • Tomcat Server http//jakarta.apache.org/tomcat/
  • Eclipse 3.X http//www.eclipse.org/downloads/in
    dex.php
  • Sysdeo Eclipse tomcat Launcher plugin
    --http//www.sysdeo.com/eclipse/tomcatPlugin.html

13
Debugging Servlet
  • Compile the servlet in debug mode
  • Javac -g HelloWorld.java
  • Set the environment variable JPDA_ADDRESS to your
    assigned port number 1000.
  • setenv JPDA_ADDRESS 31901 echo JPDA_ADDRESS
  • JPDA_ADDRESS31901 export JPDA_ADDRESS
  • Start Tomcat in debugging mode using the
    catalina.sh script.
  • catalina.sh jpda start
  • Start the NerBeans IDE by executing the following
    script /u/csc309h/lib/NetBeans3.6/bin/runide.sh

14
Debugging Servlet Contd
  • Under the Debug menu, click on "Start Session"
    and then on "Attach..., set Host and Port
    number. If everything went well you should see
    the following messages on the "Debugger Console"
    tab in the lower left corner of the window
    Connecting to localhost33000 Connection
    established
  • Select the "Runtime" tab on the top left corner
    of the window. Click on "Debugger" and then again
    on "Classes." You should see a list of all the
    classes that are currently loaded into Tomcat.
  • Point your browser to http//127.0.0.1yourPortNum
    ber/csc309/servlet/HelloWorld
  • Go back to NetBeans, the class HelloWorld should
    have been added to the list of available classes.
  • Double click on HelloWorld.

15
NetBeans Interface
16
Set Netbeans
17
(No Transcript)
18
Q and A
19
Tomcat System
20
Directory Structure
  • Bin Startup/shutdown scripts and other useful
    files.
  • Conf Configuration files, including
    modules.xml, server.xml, and a number of
    apps-ltnamegt.xml.
  • Logs event log file for each day
  • Webapps web application files
  • Work -- intermediate files (such as compiled JSP
    files) during its work. If you delete this
    directory while Tomcat is running you will not be
    able to execute JSP pages!

21
Sysdeo Eclipse Tomcat Launcher plugin
  • Starting, stopping and restarting Tomcat 4.x,
    5.0.x, 3.3
  • Registering Tomcat process to Eclipse debugger
  • Creating a WAR project (wizard can update
    server.xml file)
  • Adding Java Projects to Tomcat classpath
  • Setting Tomcat JVM parameters, classpath and
    bootclasspath
  • Exporting a Tomcat project to a WAR File
  • Choosing Tomcat configuration file
  • Capability to use a special Tomcat classloader to
    have classes in several java projects loaded at
    the same classloader level than classes in a
    Tomcat project, see readmeDevLoader.html (Thanks
    Martin Kahr)

22
Setup Tomcat
  • Enable tomcat
  • Go to the menu "Window-gt Preferences"
  • go to "Tomcat" and select your Tomcat version
  • adjust the field "Tomcat Home" to point to the
    install directory
  • Add a new user
  • scroll down the menu point "Tomcat" and click the
    item "Tomcat Manager App"
  • add a username and a password
  • click on "Add user to tomcat-users.xml"
  • Test Start Tomcat Server

23
HelloWorld Example
  • Open a new project
  • select "Tomcat Project"..., click Next button
  • call our new project "Hello World"
  • adjust URI http//localhost8080/HelloWorld/hello
  • create a new class named HelloServlet in the
    directory WEB-INF/src
  • create the file web.xml in the directory WEB-INF
    (Note not in WEB-INF/src!!!)
  • Start browser "http//localhost8080/HelloWorld/he
    llo"

24
HelloServlet
  • import java.io.
  • import javax.servlet.http.
  • import javax.servlet.
  • public class HelloServlet extends HttpServlet
    public void doGet (HttpServletRequest
    req,HttpServletResponse res) throws
    ServletException, IOException PrintWriter out
    res.getWriter() out.println("Hello, Brave new
    World!") out.close()

25
Web.xml
  • lt!DOCTYPE web-app PUBLIC'-//Sun Microsystems,
    Inc.//DTD Web Application 2.3//EN''http//java.su
    n.com/dtd/web-app_2_3.dtd'gt
  • ltweb-appgtltservletgt ltservlet-namegthellolt/servlet-
    namegt ltservlet-classgtHelloServletlt/servlet-classgt
    lt/servletgt
  • ltservlet-mappinggt ltservlet-namegthellolt/servle
    t-namegt lturl-patterngt/hellolt/url-patterngtlt/servl
    et-mappinggt
  • lt/web-appgt
Write a Comment
User Comments (0)
About PowerShow.com