Object-Oriented Enterprise Application Development - PowerPoint PPT Presentation

About This Presentation
Title:

Object-Oriented Enterprise Application Development

Description:

... applications built on the familiar 'shopping cart' metaphor must use a different ... For non-HTTP based servlets, we inherit from the GenericServlet class. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 52
Provided by: christoph140
Category:

less

Transcript and Presenter's Notes

Title: Object-Oriented Enterprise Application Development


1
Object-Oriented Enterprise Application Development
  • Introduction to Servlets

2
Topics
  • During this class we will examine
  • Motivation
  • Require packages
  • Classes
  • Lifecycle

3
Web Applications
4
Development Approaches
  • In the past few years, many alternative
    technologies have been presented for web
    application development
  • CGI
  • Proprietary APIs
  • Server-Side JavaScript
  • Microsoft ASPs
  • Java Servlets

5
CGI(1 of 2)
  • In a CGI application, each client request
    executes a program on the server.
  • These are typically written in C, C or Perl.
  • Could be embedded within a web server and thus
    didn't require any new software.

6
CGI(2 of 2)
  • While this satisfies the requirement for dynamic
    content, there are potential problems
  • Starting a new process is expensive
  • Poorly written code is a significant security
    hazard.
  • Difficult to communicate between the CGI program
    and the client.

7
Proprietary APIs
  • Many vendors offered proprietary APIs
  • Netscape NSAPI
  • Microsoft ISAPI
  • O'Reilly WSAPI
  • Since they're proprietary, you're binding your
    architecture to a particular vendor.

8
Server-Side JavaScript
  • This is JavaScript that's placed into precompiled
    HTML pages and then executed on the server.
  • Improved performance.
  • Improved security.
  • Only a few products actually support this
    technique.

9
Microsoft ASP
  • This is similar to server-side JavaScript, but
    the code isn't precompiled.
  • This technique is tied to the Microsoft IIS
    application server or other products where it's
    an add-on module.

10
Enterprise Java
  • As opposed to investing in proprietary
    techniques, many vendors have started backing the
    use of enterprise Java.
  • Java has a well-defined API.
  • Java has been widely accepted by the industry for
    its portability.
  • This will be the focus of this course.

11
Servlet Background
12
Servlets Defined
  • Java servlets are precompiled Java programs that
    are executed on a server.
  • Servlets are installed in a web container that is
    responsible for locating, loading, and executing
    the servlet when an appropriate request is issued.

13
Essential Benefits
  • Because they're written in Java and executed on
    the server, servlets provide
  • Efficiency
  • Persistence
  • Portability
  • Robustness
  • Extensibility
  • Security

14
Efficiency(1 of 2)
  • Servlets are loaded and initialized only once
    when the servlet engine receives the first
    request for that servlet.
  • Servlet engines automatically multi-thread each
    servlet.
  • There is only a single instance of any given
    servlet in memory. Each request for that servlet
    is a new thread that shares the servlet instance.

15
Efficiency(2 of 2)
  • Many web servers load balance servlet requests
    across multiple physical servers to improve
    performance.
  • This results in better performance and fault
    tolerance.

16
Persistence(1 of 2)
  • Servlets can hold data that spans across the
    various requests received by various clients.
  • This can improve performance by treating each
    servlet as a singleton.
  • This persistent data is lost if the servlet
    engine unloads the servlet class from memory.

17
Persistence(2 of 2)
  • This persistence mechanism doesn't provide
    session persistence for each client.
  • This means that applications built on the
    familiar "shopping cart" metaphor must use a
    different approach to persist the data for a
    specific user of that application.
  • We'll address this technique during the next
    lecture.

18
Portability
  • Because the servlets are written in pure Java,
    they can be moved into any environment that
    supports servlets.
  • Always make sure you confirm the version of the
    JDK that you used to write your servlets as well
    as the version of the servlet specification.

19
Robustness
  • Because servlets are written in Java, they have
    access to the entire JDK.
  • This includes all inherent Java capabilities such
    as inheritance, polymorphism, and exception
    handling.

20
Extensibility
  • Like all other classes, we can design servlets
    that are base classes that provide basic
    functionality.
  • We can then inherit from these base servlets to
    provide additional functionality.
  • This allows the use of relevant design patterns
    such as template method or command.

21
Security
  • Because servlets are executed on the server, the
    client never gains access to the code.
  • Servlets can also take advantage of the security
    manager and cryptography classes.
  • There is also code security a servlet that
    terminates abnormally usually won't crash the
    servlet engine.

22
Servlet Lifecycle
23
Servlet Lifecycle(1 of 4)
  • All servlets follow the same lifecycle.
  • The init() method is called when the servlet
    class is first loaded by the servlet engine.
  • The service() method is called each time the
    servlet is requested by a client.
  • The destroy() method is called just before the
    servlet engine unloads the servlet class from
    memory.

24
Servlet Lifecycle(2 of 4)
  • The init() method is where a servlet's life
    begins.
  • This method is only called when the servlet class
    is loaded by the servlet engine.
  • If you override this method be sure to call the
    parent class' init() method.
  • This makes the init() method useful for doing
    one-time servlet configuration.

25
Servlet Lifecycle(3 of 4)
  • The service() method is where a servlet spends
    most of its life.
  • This method is invoked one (1) time for each
    client request made to the servlet.
  • This method typically acts as a dispatcher by
    interrogating the client's request and acting on
    the data it finds there.

26
Servlet Lifecycle(4 of 4)
  • The destroy() method is where a servlet's life
    ends.
  • This method is only called when the servlet class
    is unloaded by the servlet engine.
  • If you override this method be sure to call the
    parent class' destroy() method.
  • This makes the destroy() method useful for doing
    one-time servlet cleanup.

27
Servlets Java
28
Required Packages(1 of 2)
  • Servlets are included in J2EE or in the J2SDK as
    an additional download.
  • The are two (2) packages required for servlets
  • javax.servlet.
  • javax.servlet.http.

29
Required Packages(2 of 2)
  • The javax.servlet package provides the basic
    interfaces that must be implemented and extended
    by all servlets.
  • The javax.servlet.http package provides the key
    classes used to construct servlet-based
    applications using the HTTP protocol.

30
Servlets
  • At the heart of each servlet is the Servlet
    interface containing the following methods
  • destroy
  • getServletConfig
  • getServletInfo
  • init
  • service

31
Generic Servlets
  • For non-HTTP based servlets, we inherit from the
    GenericServlet class.
  • GenericServlets provide default implementations
    for the init() and destroy() methods. However,
    servlets inherited from this class must implement
    the service() method.

32
Generic Requests
  • When a request is issued to the servlet, we must
    be able to process that request.
  • This may require the parsing of arguments or
    other complex actions specific to our
    implementation.
  • In the generic case this means that we need to
    construct a class that implements the
    ServletRequest interface.

33
Generic Responses
  • Once the servlet has completed its work, it needs
    to send any results back to the client who made
    the original request.
  • In the generic case this means that we need to
    construct a class that implements the
    ServletResponse interface.

34
Generic Dispatchers
  • Sometimes we may want to delegate a request made
    to one servlet to an entirely different servlet.
  • This is accomplished using a class that
    implements the RequestDispatcher interface.

35
HTTP Servlets
  • For HTTP based servlets, we inherit from the
    HttpServlet class.
  • This class provides a default service() method
    that we won't usually override.
  • This method knows how to parse requests made
    using the HTTP protocol.

36
Sample Code HelloWorld(1 of 2)
  1. import java.io.
  2. import javax.servlet.
  3. import javax.servlet.http.
  4. public class HelloWorld extends HttpServlet
  5. public void doGet(HttpServletRequest rqst,
    HttpServletResponse resp) throws
    IOException, ServletException
  6. resp.setContentType("text/html")
  7. PrintWriter out new PrintWriter(
    resp.getOutputStream() )

37
Sample Code HelloWorld (2 of 2)
  1. out.println("ltHTMLgt")
  2. out.println("ltHEADgt")
  3. out.println("ltTITLEgt")
  4. out.println("HelloWorld")
  5. out.println("lt/TITLEgt")
  6. out.println("lt/HEADgt")
  7. out.println("ltBODYgt")
  8. out.println("ltPgtHello, World!lt/Pgt")
  9. out.println("lt/BODYgt")
  10. out.println("lt/HTMLgt")
  11. out.close()

38
HTTP Servlet Anatomy(1 of 2)
  • The HelloWorld servlet illustrates some of the
    key servlet elements.
  • The servlet extends the HttpServlet class.
  • The doGet() method accepts two (2) arguments and
    throws two (2) exceptions.

39
HTTP Servlet Anatomy(2 of 2)
  • But how is doGet() invoked?
  • The answer lies in the default implementation of
    the service() method within the HttpServlet
    class.
  • When a GET request is received, the service()
    method automatically invokes the doGet() method.

40
Initialization Parameters
41
Justification
  • Sometimes we want to initialize a servlet when
    it's first loaded by the servlet engine.
  • For instance, we might want to load some
    configuration settings before allowing processing
    to continue.
  • This can be useful for testing when you don't
    want to configure a web page to initiate the test.

42
Technique
  • We can embed such values in the application's
    web.xml file.
  • We access these values within the servlet's
    init() method when it is first loaded.
  • To access these initialization parameters we use
    the getInitParameter() method on the
    ServletConfig reference passed to the init()
    method.

43
Sample Code Message(1 of 2)
  1. import java.io.
  2. import javax.servlet.
  3. import javax.servlet.http.
  4. public class Message extends HttpServlet
  5. private String msg null
  6. public void init( ServletConfig cfg ) throws
    ServletException
  7. super.init( cfg )
  8. msg cfg.getInitParameter("msg")
  9. if ( msg null)
  10. msg "no.message.specified"

44
Sample Code Message(2 of 2)
  • resp.setContentType("text/html")
  • PrintWriter out new PrintWriter(
    resp.getOutputStream() )
  • out.println("ltHTMLgtltHEADgtltTITLEgt")
  • out.println("Message")
  • out.println("lt/TITLEgtlt/HEADgt")
  • out.println("ltBODYgt")
  • out.println("ltPgt" msg "lt/Pgt")
  • out.println("lt/BODYgtlt/HTMLgt")
  • out.close()

45
Servlet Pitfalls
46
Local Data Members(1 of 3)
  • In the Message servlet, I declared a local data
    member called msg.
  • With regular classes, this data member would be
    specific to each object.
  • For servlets, this data member is shared across
    all requests made to the Message servlet.

47
Local Data Members(2 of 3)
  • This means that we can't use local data members
    to store user- or session-specific data.
  • Each new access to that data may result in the
    value being changed. This change will affect all
    users of that servlet.
  • However, such data members are useful if the
    value in question is meant to be a constant.

48
Local Data Members(3 of 3)
  • To pass the various data members between methods
    within the servlet, we are forced to adopt the
    approach of using an aggregate class.
  • This could be something as simple as using a Java
    Collection class.
  • We could also devise our own custom class.

49
Review
  • During this class we have discussed
  • Motivation
  • Require packages
  • Classes
  • Lifecycle

50
Resources
  • Developing Java ServletsJames Goodwill, Sam's
    Publishing, 1999.ISBN 0-672-31600-5
  • Core Servlets and JavaServer PagesMarty Hall,
    Prentice-Hall, Inc., 2000.ISBN 0-13-089340-4
  • Java 2 Platform, Enterprise EditionB. Shannon,
    et al., Addison-Wesley, 2000.ISBN 0-201-70456-0

51
Coming Attractions
  • Next week we'll look at servlets in greater depth
    including how to invoke them from an HTML page
    and how to implement the "shopping cart"
    metaphor.
  • Please read Chapters 3 9 in your text.
Write a Comment
User Comments (0)
About PowerShow.com