CMP 436 - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

CMP 436

Description:

In order to execute a servlet, a servlet container (engine) is ... BODY BGCOLOR='#FDF5E6' n' ' H1 Hello /H1 n' ' /BODY /HTML '); Servlets Introduction ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 20
Provided by: GJu
Category:
Tags: cmp | bgcolor

less

Transcript and Presenter's Notes

Title: CMP 436


1
CMP 436
  • Servlets Brief Introduction
  • Fall 2006
  • Department of Mathematics
  • and Computer Science
  • Lehman College, CUNY

2
What is a Servlet?
  • Servlets are Java programs that serve as an
    mediating layer between an HTTP request of a
    client and applications in the Web server.
  • A servlet is a dynamically loaded module that
    services requests from a Web server.
  • In order to execute a servlet, a servlet
    container (engine) is needed.
  • Clients can only invoke servlets via a servlet
    container. A servlet container is a key component
    in an application server.
  • The main responsibility of a servlet container is
    to accept and processes requests that invoke
    servlets.
  • A servlet runs entirely inside the a JVM (Java
    Virtual Machine) of a container.
  • Temporary (persistent) servlets are activated
    when clients request their services
  • Permanent servlets are active when their host
    servers are up.
  • Servlets are designated as temporary and
    permanent through configurations of the hosting
    servers.

3
Components and Containers
Java VM
GC
Classloader
Object
Code-levelsecurity
Other VMservices
Object
Object
4
Components and Containers
  • Components
  • One or more objects that implement a
    well-defined application service
  • Make use of component services
  • Container
  • Runtime entity that manages components and
    provides their services
  • Components need to be
  • Written within the contracts defined by the
    container
  • APIs plus some rules related to the component
    services
  • Deployed to containers
  • Describe the components
  • Deliver all the elements (classes, resources,
    etc.) that implement the components
  • Provide instructions on how to manage them
  • Assemble/package components into an application
    assembly

5
A Servlets Job
  • Read explicit data sent by client (form data).
  • Read implicit data sent by client (request
    headers).
  • Generate the results.
  • Send the explicit data back to client (HTML).
  • Send the implicit data to client (status codes
    and response headers).

6
Servlet Architecture (high level)
Web client
7
Servlet Life Cycle
  • The container loads and initializes the Servlet.
  • The Servlet handles client requests.
  • The container can remove the Servlet.
  • The servlet can remain loaded to handle
    additional requests
  • Incur startup costs only once, may consume server
    resources if active.
  • The javax.servlet.Servlet interface introduces
    the methods supporting communication between the
    Servlets and their container.
  • void init(ServletConfig config) to be executed
    when the Servlet starts running
  • void destroy()
  • void service(ServletRequest req,ServletResponse
    res) to be executed in response to client
    requests
  • ServletConfig getServletConfig()info about the
    servlet environment, provided by the host in init
  • String getServletInfo() information about the
    Servlet

8
Example HTTP Servlet Lifecycle
Instantiated (constructor calledby container)
Initialized init(ServletConfig)
Handle requests doGet(HttpServletRequest,
HttpServletResponse)doPost(HttpServletRequest,
HttpServletResponse)
App logichappens here
Destroyed destroy()
9
The Servlet Life Cycle (contd)
  • Init
  • Executed once when the Servlet is first loaded
  • Not called for each request.
  • Service
  • Called in a new thread by server for each
    request
  • Dispatches to doGet, doPost, etc
  • Do not override service().
  • The service method gives you automatic support
    for
  • HEAD requests,
  • OPTIONS requests, //cache, encoding, etc
  • TRACE requests.
  • doGet, doPost, doXxx
  • Handles GET, POST, doPut, doTrace, etc.
  • Override these to provide desired behaviour.
  • Destroy
  • Called when server deletes Servlet instance,
  • Not called after each request.

10
TOMCAT
  • A Java Servlet, Java ServerPage (JSP) container.
  • A reference implementation of Java servlet and
    JSP.
  • It is a light weight Servlet/JSP container, and
    public domain software.
  • Version 3.2 supports Servlet 2.2 and JSP 1.1
  • Version 4.0 supports Servlet 2.3 and JSP 1.2
  • Version 5.0 supports Servlet 2.4 and JSP 2.0
  • Version 5.5 requires J2SDK 5.0
  • Tomcat can be used as a
  • stand-alone container
  • for development and debugging
  • add-on to a web server
  • for deployment
  • supports Apache, IIS, etc.

11
Tomcat Installation
  • http//www.coreservlets.com/Apache-Tomcat-Tutorial
    /
  • Install the JDK
  • http//java.sun.com/javase/downloads/index.jsp
  • Make sure JDK 1.4, 1.5, or Java SE 5.0 (JDK 5.0),
    or Java EE 5 SDK (bundled with Java 5)
  • PATH environment variable must include binary
    folders (e.g., INSTALL_ROOT_DIRECTORY/jdk1.5.0/bin
    )
  • Try java version and javac help to make sure
    this
  • Set environment variable
  • JAVA_HOME the root directory of JDK
  • On Windows
  • Control Panel System Advanced Environment
    Variables
  • (e.g., JAVA_HOMEC\j2sdk1.5.0
  • set JAVA_HOMEC\j2sdk1.5.0)
  • On Linux (bash /.bash_profile)
  • (e.g., export JAVA_HOME/usr/local/j2sdk1.5.0)
  • Install Tomcat
  • http//jakarta.apache.org/tomcat
  • Current version 5.5.17
  • apache-tomcat-5.5.17.zip
  • apache-tomcat-5.5.17.tar.gz

12
  • A new subdirectory will be created
  • (e.g., C\Tomact-5.5, HOME/Tomcat-5.5)
  • This directory is referred to as Tomcat root or
    Tomcat home.
  • Subdirectories of Tomcat home
  • bin scripts to start/shutdown Tomcat etc.
  • conf server configuration files
  • common class/jar files available to Tomcat and
    web apps
  • classes unpacked classes
  • lib jar files
  • shared class/jar files available to all web apps
  • classes unpacked classes
  • lib jar files
  • logs log files
  • server class/jar files for Tomcat internal use
    only
  • webapps web applications (include ROOT, examples)
  • work temporary working directory

13
Compiling/Invoking Servlets
  • Set your CLASSPATH
  • Servlet JAR file (e.g., tomcat_home/common/lib/se
    rvlet-api.jar (Servlet API).
  • Put your servlet classes in proper location
  • Locations vary from server to server. e.g.,
  • A servlet at tomcat_home/webapps/AppDir/WEB-INF/c
    lasses/HelloServlet.class can be reached through
    the URI
  • http//hostport/AppDir/servlet/HelloServlet or
  • http//hostport/AppDir/HelloServlet depending on
    servlet-mapping
  • A servlet at tomcat_home/webapps/AppDir/WEB-INF/c
    lasses/package/HelloServlet.class can be reached
    through the URI
  • http//hostport/AppDir/servlet/HelloServlet/packa
    ge.HelloServlet
  • Invoke your Servlets
  • http//host8080/AppDir/servlet/ServletName
  • Custom URL-to-servlet mapping (via web.xml).

14
Package Abstraction
Servlet
Generic Servlet
HttpServlet
YourOwnServlet
15
Servlet Directory
  • import java.io.
  • import javax.servlet.
  • import javax.servlet.http.
  • public class HelloServlet extends HttpServlet
  • public void doGet(HttpServletRequest request,
  • HttpServletResponse response)
  • throws ServletException, IOException
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • String docType
  • "lt!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML
    4.0 "
  • "Transitional//EN\"gt\n"
  • out.println(docType
  • "ltHTMLgt\n"
  • "ltHEADgtltTITLEgtHellolt/TITLEgtlt/HEADgt
    \n"
  • "ltBODY BGCOLOR\"FDF5E6\"gt\n"
  • "ltH1gtHellolt/H1gt\n"
  • "lt/BODYgtlt/HTMLgt")

16
Compiling
  • javac classpath LIB/servlet-api.jar
  • Hellox.java
  • Or setup CLASSPATH
  • Or add additional lib option if you use IDE
    (e.g., JBuilderX)
  • When you change the Servlet, usually it may not
    be enough just to compile it.
  • You may need to stop tomcat and restart tomcat to
    make tomcat reload the Servlet and not use the
    old version stored in memory.

17
Directory Structure
18
Directory Structure (cont.)
19
web.xml
  • lt!DOCTYPE web-app
  • PUBLIC "-//Sun Microsystems, Inc.//DTD Web
    Application 2.3//EN"
  • "http//java.sun.com/dtd/web-app_2_3.dtd"gt
  • ltweb-appgt
  • ltservletgt
  • ltservlet-namegtServlet1lt/servlet-namegt
  • ltservlet-classgtHelloServletlt/servlet-classgt
  • lt/servletgt
  • ltservletgt
  • ltservlet-namegtServlet2lt/servlet-namegt
  • ltservlet-classgtcoreservlets.HelloServlet2lt/ser
    vlet-classgt
  • lt/servletgt
  • ltservletgt
  • ltservlet-namegtServlet3lt/servlet-namegt
  • ltservlet-classgtcoreservlets.HelloServlet3lt/ser
    vlet-classgt
  • lt/servletgt
  • ltservlet-mappinggt
  • ltservlet-namegtServlet1lt/servlet-namegt
Write a Comment
User Comments (0)
About PowerShow.com