• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Servlet 01
 

Servlet 01

on

  • 1,383 views

 

Statistics

Views

Total Views
1,383
Views on SlideShare
1,383
Embed Views
0

Actions

Likes
1
Downloads
73
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Servlet 01 Servlet 01 Presentation Transcript

      • Objectives
      • In this lesson, you will learn to:
      • Identify the characteristics of a Servlet
      • Create a Servlet
      • Deploy a Servlet
      • Need for Servlets
      • Servlets are required to:
        • Reduce the overhead on the server and network
        • To take care of processing data on the Web server
      • Servlets are:
        • Java programs that can be deployed on a Java enabled Web server
        • Used to extend the functionality of a Web server
        • Used to add dynamic content to Web pages
      • Characteristics of Servlets
      • Servlets are:
      • Efficient
        • Initialization code for a servlet is executed only once
      • Robust
        • Provide all powerful features of Java
      • Portable across Web servers
      • Persistent
        • Increase the performance of a system by preventing frequent disk access
      • Comparison between Servlets and Applets
      • Applets:
        • Are embedded in Web pages
        • Require the browser to be Java-enabled
        • Take a lot of time to be downloaded
      • Servlets:
        • Execute on the Web server, thus help overcome problems with download time
        • Do not require the browser to be Java-enabled
      • Comparison between Servlets and other Server-
      • Side Scripting Technologies
      • Common Gateway Interface (CGI) scripts, JSP, and ASP are alternatives to servlets
      • CGI Scripts:
        • Are programs written in C, C++, or Perl
        • Get executed in a server
        • Run in separate processes for each client access
        • Require the interpreter to be loaded on the server
      • A JSP file is automatically converted to a servlet before it is executed
      • Comparison between Servlets and other Server- Side Scriptings
      • Active Server Pages (ASP):
        • Is a server-side scripting language developed by Microsoft
        • Enables a developer to combine HTML and a scripting language in the same Web page
        • Are not compatible with all Web servers
      • Working of Servlets
      • Client browser passes requests to a servlet using the following methods:
      • GET
        • Uses a query string to send additional information to the server
        • Query string is displayed in the client browser
      • POST
        • Sends the data as packets to the server through a separate socket connection
        • Complete transaction is invisible to the client
        • Slower compared to the GET method
      • The javax.servlet Package
      • Hierarchy of classes that are used to create a servlet
      Class java.lang.Object Class javax.servlet.GenericServlet Class javax.servlet.HttpServlet Interface javax.servlet.Servlet Interface javax.servlet.ServletConfig Interface javax.io.Serializable
      • The javax.servlet Package (Contd.)
      • A brief description of the classes and interfaces
      Provides methods to process requests from the clients. HTTPServletRequest interface Provides a HTTP specific implementation of the Servlet interface. HTTPServlet class Description Class/Interface
    • Response to the client is sent in the form of a HTML page through an object of the HTTPServletResponse class. HTTPServlet Response interface Used to store the servlets startup configuration values and the initialization parameters. ServletConfig class
      • Life Cycle of a Servlet
      • Life cycle of a servlet:
      Client (Browser) init() service() destroy() Request Response
      • Life Cycle of a Servlet (Contd.)
      • The following table describes few methods that are used in creating a servlet:
      Receives all requests from clients, identifies the type of the request, and dispatches them to the doGet() or doPost() methods for processing. Servlet. service() Contains all initialization code for the servlet. Servlet.init (ServletConfig config) throws ServletException Functionality Method name
      • Life Cycle of a Servlet (Contd.)
      Executes only once when the servlet is removed from server. Servlet. destroy() Sets the type of content that is to be sent as response to the client browser. HTTPServlet Response. setContentType (String type) Returns a reference to a PrintWriter object. HTTPServlet Response. getWriter() Functionality Method name
      • Deploying a Servlet
      • A servlet can be deployed in:
        • Java Web Server (JWS)
        • JRun
        • Apache
        • Java 2 Enterprise Edition (J2EE) server
      • Identify the mechanism to record the
      • number of hits on a Web site
      • Client browsers that are used to access the Web site run on different machines
        • Hence, the client (browser) cannot keep track of the hit count data
      • This data has to be captured on the server’s side
        • Thus, a technique that can be used to write a server-side program is necessary to solve this problem
      • Identify the classes to be used
      • A class that extends the HttpServlet class must be coded to solve the problem
      • Identify the methods to be used
      • The init() method needs to be coded in the hitcountServlet class to initialize the hitcounter to zero
      • The doGet() method needs to be coded to increment the hit counter whenever a client browser makes a request for the site home page
      • Write and compile the servlet
      • Set the values of PATH and CLASSPATH variables
      • Code the servlet program
      • Compile the program by using the command javac hitcountServlet.java
      • Execute the servlet
      • Type http://127.0.0.1:8000/hitcountwebcontext/hitcounter in the address bar of the browser to execute the servlet
      • Problem for you….
      • In the URL that is provided, identify the Web context name and the alias name of the servlet
      • http://127.0.0.1:8000/name/execute
      • Summary
      • In this lesson, you learned that:
      • Servlets are server-side Java programs that can be deployed on a Web Server
      • The Servlet interface provides the basic framework for coding servlets
      • Servlets are :
        • Portable
        • Extensible
        • Persistent
        • Robust
      • Summary (Contd.)
      • The life cycle of a servlet is composed of the init() , service() , and the destroy() methods
      • Servlets can be deployed in the J2EE server and JWS
      • The deploytool command is used to deploy a servlet on a J2EE server