Java Servlet - PowerPoint PPT Presentation

About This Presentation
Title:

Java Servlet

Description:

An Introduction Web Engineering – PowerPoint PPT presentation

Number of Views:2949
Slides: 20
Provided by: inam12
Tags:

less

Transcript and Presenter's Notes

Title: Java Servlet


1
Java Servlets
  • Web Engineering

Faraz Hashmi, BSIT-5th Eve, 3011
2
Servlet
  • Servlets are protocol- and platform-independent
    server side components, written in Java, which
    dynamically extend Java-enabled servers.
  • They have become more and more popular as they
    benefit from all the advantages of the Java
    programming language in particular Architecture
    and Platform Independence and the ability to
    build robust and secure applications.

3
Servlet
  • A Servlet, in simple terms, is a Java program
    running under a web server taking a 'request'
    object as an input and responding back by a
    'response' object. Typically a web browser will
    send the request in HTTP format. The Servlet
    container will convert that into a request
    object. Similarly the response object - populated
    by the Servlet is converted into an HTTP response
    by the Servlet container.
  • This mechanism makes the browser - web server
    interaction very easy. Java Servlets are more
    efficient, easier to use, more powerful, more
    portable, and cheaper than traditional CGI and
    many alternative CGI-like technologies.
  • (More importantly, Servlet developers tend to get
    paid more than Perl programmers)

4
Servlet Mechanism
  • A servlet is a Java class and thus needs to be
    executed in a Java Virtual Machine by a service
    called a servlet engine. The servlet engine loads
    the servlet before it can be used. The servlet
    then stays loaded until it is unloaded or the
    servlet engine is shut down.

5
Servlet Mechanism
JVM
Request
Client/ Browser
Server
Servlet
Response
Extends the functionality of the server
by Generating HTML pages dynamically
6
Servlet Types
  • Servlets are based on two main packages
  • javax.servlet
  • javax.servlet.http
  • GenericServlet
  • For writing protocol independent servlets
  • HttpServlet
  • Extends from GenericServlet class
  • Adds functionality for writing HTTP specific
    servlets

7
Servlet class hierarchy
Object
GenericServlet
ServletResponse
ServerRequest
javax.servlet
HttpServletRequest
HttpServlet
HttpServletResponse
javax.servlet.http
8
Software Requirements
  • To use Java servlets, following softwares will be
    needed
  • J2SE
  • Additional J2EE based libraries for servlets such
    as servlet-api.jar, jsp-api.jar. You can
    download these APIs separately but they are also
    available with the web server youll be using.
  • A capable servlet web engine (web server)

9
Environment Setup
  • Steps
  • Download the Apache Tomcat Server
  • Install Tomcat
  • Set the JAVA_HOME variable
  • Set the CATALINA_HOME variable
  • Set the CLASSPATH variable
  • Test the Server

10
Servlet Advantages Disadvantages
  • The advantage of Servlets is,
  • Portability
  • o Portable across operating systems and across
    web servers
  • Power
  • o Harness the full power of the core Java
    APIs networking and URL access, multithreading,
    image manipulation, data compression, JDBC,
    object serialization, internationalization
  • Efficiency Endurance
  • o Memory resident, so invocation highly
    efficientno process to spawn or interpreter to
    invoke

11
Servlet Advantages Disadvantages
  • Safety
  • o Support safe programming since inherit
    Javas strong type safety, exception-handling
    mechanism
  • Elegance
  • o Code is clean, object-oriented, modular, and
    simple (i.e.. Session tracking, cookie)
  • Integration
  • o Tightly integrated with the servertranslate
    file paths, perform logging, check authorization,
    and MIME type mapping

12
Servlet Advantages Disadvantages
  • The disadvantage of Servlets is,
  • Web Administrator will need to learn how to
    install and maintain Java Servlets
  • Tedious uses of out.println() statements
  • o Can be remedied by using Java Server Page
    (JSP)

13
Types of HTTP Requests
  • Get
  • Post
  • Delete
  • Options
  • Put
  • Trace

14
How HTTP Sends Request
  • Some HTTP request types
  • Get -- Attr/Val pairs attached after ? of URL
  • E.g. http//www.gmail.com/register?name
    ali
  • Post -- Attr/Val pairs attatched with the
    request body

Client
Server
15
HTTP Request Example
Request parameters etc.
16
Servlet Model
17
Servlet Life Cycle
  • Initialize
  • Service
  • Destroy

18
HelloWorld Servlet
  • import java.io.
  • import javax.servlet.
  • import javax.servlet.http.
  • public class HelloWorldServlet extends
    HttpServlet
  • public void doGet(HttpServletRequest request,
  • HttpServletResponse
    response)
  • throws ServletException,
    IOException
  • PrintWriter out response.getWriter()
  • out.println(Hello World!)

19
Web.xml
  • lt?xml version"1.0" encoding"ISO-8859-1"?gt
  • ltweb-appgt
  • ltservletgt
  • ltservlet-namegtHelloWorldServletlt/servlet-n
    amegt
  • ltservlet-classgtHelloWorldServletlt/servlet-
    classgt
  • lt/servletgt
  • ltservlet-mappinggt
  • ltservlet-namegtHelloWorldServletlt/servlet-n
    amegt
  • lturl-patterngt/myfirstservletlt/url-patterngt
  • lt/servlet-mappinggt
  • lt/web-appgt
Write a Comment
User Comments (0)
About PowerShow.com