Java Applets and Servlets - PowerPoint PPT Presentation

About This Presentation
Title:

Java Applets and Servlets

Description:

There were many other programming language that had similar characteristics like ... could be heavy, doing all sorts of 'mundane work' behind the scene. Applets ... – PowerPoint PPT presentation

Number of Views:1191
Avg rating:3.0/5.0
Slides: 25
Provided by: dbKon
Category:

less

Transcript and Presenter's Notes

Title: Java Applets and Servlets


1
Java Applets and Servlets
  • ?????? ???
  • ? ? ?

2
Concepts
  • Java has been successful
  • Object-orientation, simplicity, portability,
    safety, dynamicity
  • There were many other programming language that
    had similar characteristics like Java but never
    had much success
  • Java was so special
  • Java came out with a killer application in the
    right place at the right time
  • applets, HotJava browser that demonstrated
    the utility of applets
  • Servlets and Applets
  • Servlets
  • are designed for Web servers
  • are either faceless or have a straight face(HTML)
  • could be heavy, doing all sorts of mundane work
    behind the scene
  • Applets
  • are designed for Web clients
  • can have all sorts of pretty faces(GUIs)
  • are light, particularly for use in network
    computers or devices
  • Both can be mobile mobile code, not mobile
    objects

3
Java Applets(1)
  • The benefit of using applets
  • The Web browser provides a universal client
    environment
  • The independent and platform-independent
    client-side components
  • A common API (the Applet API that we will discuss
    later)
  • The dynamic loading over the net
  • Security (the applet security model)
  • Applet programming mode
  • An applet must extend the java.applet.Applet
    class
  • ltAPPLET gt tag allows you to embed the applet in
    an HTML document
  • ltAPPLETgt tags major attributes
  • codebase, code, name, width, height, archive
  • You can use the PARAM element to supply initial
    values to the applet

4
Java Applets(2)
  • Applet life cycle
  • Once the class file of an applet is downloaded by
    the Web browser
  • An instance of the applet is automatically
    created
  • The applet will go through a life cycle,
    supported by its life-cycle methods, and these
    methods are automatically called by the Web
    browser
  • Life-cycle methods
  • init()
  • is called when the applet is first loaded or
    reloaded
  • start()
  • is called after initialization or stopping to
    re-start
  • stop()
  • is called when the user leaves the Web page where
    the applet is embedded
  • destroy()
  • is called after stopping
  • Another method (particularly for display)
  • paint(Graphics g) is for painting the screen

5
The Applet API(1)
  • The classes and interfaces in the Applet API are
    all part of the java.applet package
  • Applet class
  • The Applet class provides a standard interface
    for applets
  • All applets are subclasses of the Applet class
  • The Applet class extends the java.awt.Panel class
  • Constructors
  • Applet()
  • Methods
  • Life-cycle methods
  • init(), start(), stop(), destory(), isActive()
  • Methods to get and manipulate its data
  • getParameter(String name), getParameterInfo(),
    getAudioClip(java.net.URL url),
    getAudioClip(java.net.URL url, String name),
    play(java.net.URL url), play(java.net.URL url,
    String name), getImage(java.net.URL url),
    getImage(java.net.URL url, String name)

6
The Applet API(2)
  • Applet class
  • Methods (cont)
  • Methods to obtain information about this applet
    and to manipulate it
  • getAppletContext(), getAppletInfo(),
    getCodeBase(), getDocumentBase(), getLocale(),
    resize(java.awt.Dimension d), resize(int width,
    int height), setStub(AppletStub stub),
    showStatus(String msg)
  • AppletContext interface
  • The AppletContext interface gives applets access
    to information about their environment, the
    document containing the applets, and the other
    applets in the same document
  • Methods
  • getApplet(String name), getApplets(),
    getAudioClip(java.net.URL url),
    getImage(java.net.URL url), showDocument(java.net.
    URL url), showDocument(java.net.URL url, String
    target), showStatus(String msg)

7
The Applet API(3)
  • AppletStub interface
  • The stub serves as the interface between the
    applet and its browser environment
  • Methods
  • isActive(), getParameter(String name),
    getAppletContext(), getCodeBase(),
    getDocumentBase(), appletResize(int height, int
    weight)
  • AudioClip interface
  • The AudioClip interface is a simple abstraction
    for playing an audio clip
  • Methods
  • loop(), play(), stop()

8
Sample Data Access Applets(1)
  • Fg 12-1. JDBC Usage Scenario Java Applet(1)

9
Sample Data Access Applets(2)
10
Sample Data Access Applets(3)
11
Java Servlets(1)
  • The benefit of using servlets
  • The Web browser can provide a universal client
    environment
  • The independent and platform-independent
    server-side components
  • A common API
  • The dynamic loading, either locally or over the
    net
  • Security (the servlet security model)

12
Java Servlets(2)
  • Servlets can be used to generate dynamic HTML
    content
  • Faster than CGI
  • Servlets use only lightweight threads to handle
    client requests
  • Safe than CGI
  • Automatic memory management and the servlet
    security model
  • Fg 12-5. Dynamic HTML Generation Using HTTP
    Servlets

13
Java Servlets(3)
  • Servlet programming model
  • A servlet
  • must implement the javax.servlet.Servlet
    interface
  • or extend a class that implements Servlet
    interface, such as javax.servlet.GenericServlet
    or javax.servlet.http.HttpServlet
  • Servlets follow this request-service-response
    programming model
  • Request- represented by a java.servlet.ServletRequ
    est object
  • Service- performed by invoking the service()
    method
  • Response- represented by a java.servlet.ServletRes
    ponse object

14
Java Servlets(4)
  • Servlet life cycle
  • Once invoked, a servlet will go through a life
    cycle, supported by its life-cycle methods
  • These methods are automatically called by the Web
    server
  • Life-cycle methods
  • init() is called by the network service when it
    loads this servlet
  • service() is called after initialization
  • destroy() is called before unloading and is
    called only once
  • Two additional methods for HTTP servlets
  • doGet() for processing the HTTP GET method
  • doPost() for processing the HTTP POST method
  • These methods are called after initialization

15
Java Servlets(3)
  • Loading and invoking servlets
  • From the /servlets/directory
  • http//hostport/servlet/ltservlet_classgt
  • From a remote location
  • http//hostport/servlet/ltservlet_URLgt
  • By setting up Server Side Includes
  • By configuring the Admin Tool
  • By defining a Filter Chain
  • Server side includes
  • ltSERVLETgt tag allows you to embed a servlet in an
    HTML document, which must have the special file
    extension .shtml
  • ltSERVLETgt tags major attributes
  • codebase, code
  • You can use the PARAM element to supply initial
    values to the servlet

16
The Servlet API(1)
  • The classes and interfaces in the Servlet API are
    all part of the javax.servlet package
  • Servlet interface
  • All servlets implement the Servlet interface
  • Methods
  • Life-cycle methods
  • init(ServletConf config), service(ServletRequest
    req, ServletResponse resp), destroy()
  • Methods to obtain information about this servlet
  • getServletConfig(), getServletInfo()
  • ServletConfig interface
  • The ServletConfig interface is implemented by
    network services or servlets to pass
    configuration information to a servlet when it is
    first loaded
  • Methods
  • getInitParameter(String name), getInitParameterNam
    es(), getServletContext()

17
The Servlet API(2)
  • ServletContext interface
  • The ServletContext interface is implemented by
    network services
  • It gives servlets access to information about
    their environment and allows servlets to log
    significant events
  • Methods
  • getServlet(String name), getServlets(),
    getServerInfo(), getAttribute(String name),
    getRealPath(String path), getMimeType(String
    file), log(String msg)
  • ServletRequest interface
  • The servletRequest interface provides the means
    for getting information from the client to the
    servlet for a service request
  • Methods
  • Methods to obtain information on this request
  • getProtocol(), getScheme(), getParameterNames(),
    getParameter(String name), getParameterValues(Stri
    ng name), getAttribute(String name),
    getContentLength(), getContentType(),
    getInputStream()
  • Methods to obtain information on the sender and
    receiver
  • getRemoteAddr(), getRemoteHost(),
    getServerName(), getServerPort(),
    getRealPath(String path)

18
The Servlet API(3)
  • ServletResponse interface
  • The ServletResponse interface provides the means
    for sending response data from the servlet to the
    client
  • Methods
  • setContentLength(int len), setContentType(Striing
    type), getOutputStream()
  • GenericServlet class
  • The GenericServlet class is an abstract class
    that simplifies writing servlets
  • It implements the Servlet and ServletConfig
    interfaces
  • Constructors
  • GenericServlet()
  • Methods
  • Life-cycle methods
  • init(ServletConf config), service(ServletRequest
    req, ServletResponse resp), destroy()
  • Methods to obtain information about this servlet
  • getInitParameter(String name), getInitParameterNam
    es(), getServletContext(), getServletConfig(),
    getServletInfo()
  • Additional methods
  • log(String msg)

19
The Servlet API(4)
  • ServletInputStream class
  • The ServletInputStream class is an abstract class
    for reading servlet request data
  • It extends the java.io.InputStream class
  • Constructors
  • ServletInputStream()
  • Methods
  • readLine(byte b, int off, int len)
  • ServletOutputStream class
  • The StervletOutputStream class is an abstract
    class for writing servlet response data
  • It extends the java.io.OutputStream class
  • Constructor
  • ServletOutputStream()
  • Methods
  • print(int i), print(long l), print(float f),
    print(double d), print(char c), print(String s),
    print(boolean b), println(), println(int i),
    println(long l), println(float f), println(double
    d), println(char c), println(String s),
    println(boolean b)

20
The HTTP Servlet API(1)
  • The classes and interfaces in the HTTP servlet
    API are all part of the javax.servlet.http
    package
  • HttpServlet class
  • The HttpServlet class is an abstract class that
    simplifies writing HTTP 1.0 servlets
  • It extends the GenericServlet class and provides
    a framework for handling the HTTP protocol
  • Constructors
  • HttpServlet()
  • Methods
  • service(HttpServletRequest req,
    HttpServletResponse resp), service(ServletReqeust
    req, ServletResponse resp), doGet(HttpSErvletReque
    st req, HttpServletResponse resp),
    doPost(HttpServletRequest req, HttpServletResponse
    resp), getLastModified(HttpServletRequest req)
  • HttpUtils class
  • The HttpUtils class contains utility methods
    useful to HTTP servlets
  • Methods
  • getRequestURL(HttpServletRequest req),
    parsePostData(int ServletInputStream),
    parseQueryString(String s)
  • Constructors
  • HttpUtils()

21
The HTTP Servlet API(2)
  • HttpServletRequest interface
  • The HttpServletRequest intreface extends the
    ServletRequest interface and represents an HTTP
    servlet request
  • Methods to obtain information on this request
  • getAuthType(), getHeaderNames(), getHeader(String
    name), getDateHeader(String name),
    getIntHeader(String name), getMethod(),
    getQueryString()
  • Methods to obtain information on the sender and
    receiver
  • getRemoteUser(), getRequestURI(),
    getServletPath(), getPathInfo(),
    getPathTranslated()
  • HttpServletResponse interface
  • The HttpServletResponse interface extends the
    ServletResponse interface and represents an HTTP
    servlet response
  • Class constants
  • SC_OK(int), SC_CREATED(int), SC_ACCEPTED(int),
    SC_NO_CONTENT(int), SC_MOVED_PERMANENTLY(int),
    SC_MOVED_TEMPORARILY(int),.
  • Methods
  • containsHeader(String name), setHeader(String
    name, String value), setDateHeader(String name,
    long value), setIntHeader(String name, int
    value), setStatus(int sc), setStatus(int sc,
    String msg), sendError(int sc), sendError(int sc,
    String msg), sendRedirect(String location)

22
Sample Data Access Servlets(1)
23
Sample Data Access Servlets(2)
24
Conclusion
  • Java applets
  • Easy to use
  • More sophisticated GUIs than HTML pages for data
    presentation
  • More programming effort, a fat client, and
    performance problems
  • The importance of Java applets for data access on
    the Web client is diminishing
  • The lack of uniform and in-time support among Web
    browser vendors
  • The restricted communication channel between
    applets and HTML pages and performance issues
  • Java servlets
  • Easy to use
  • HTML pages for display or only limited GUI
    support
  • Much less programming effort, only a thin client,
    and fewer performance problems
  • The importance of Java servlets for client/server
    data access is increasing
  • Java servlets will also be indispensable when
    using reusable server components from the Web
Write a Comment
User Comments (0)
About PowerShow.com