Home arrow Java & J2EE arrow Page 2 - Overview of Java Web Technologies, Part 1

Methods - Java

Today, Budi walks us through a refresher and brief overview of server JSP programming. Today's portion covers Servlet technologies, including servlets and Tomcat. This excerpt comes from chapter one of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983-7, 2004).

  1. Overview of Java Web Technologies, Part 1
  2. Methods
  3. Creating a Servlet Directory Structure
  4. Reviewing Deployment Descriptors
  5. Servlet Mapping
  6. Defining Context Parameters
  7. Retrieving Context Parameters
  8. Listening to Application Events
  9. Packaging and Deploying a Web Application
By: McGraw-Hill/Osborne
Rating: starstarstarstarstar / 25
March 01, 2004

print this article



The init Method

The init method is called the first time the servlet is invoked. It is called exactly once to indicate to the servlet that the servlet is being placed into service. You override this method to write initialization code that needs to be executed only once, such as initializing values, loading database drivers, and so on.

The following is the methodís signature:

public void init(ServletConfig config) throws ServletException

The service Method

The service method is called by the servlet container each time the servlet is invoked. For each incoming HTTP request, the servlet container creates a request object (that implements javax.servlet.ServletRequest or javax.servlet.http.HttpServletRequest) and a response object (that implements javax.servlet.ServletResponse or javax.servlet.http.HttpServletResponse) and passes both objects to the servletís service method. The request object encapsulates information parsed from the HTTP request that the servlet can use, such as the request URI, request headers and cookies, and request parameters. The servlet uses the response object to send responses to the Web client.

The service method has the following signature:

public void service(ServletRequest request, ServletResponse response)throws ServletException, java.io.IOException

TIP: If youíre interested in knowing how a servlet container creates request and response objects, or how it works in general, read How Tomcat Works, available from www.brainysoftware.com.

The destroy Method

The servlet container calls the destroy method before removing a servlet instance from service. This normally happens when the servlet container is shut down or the servlet container needs some free memory. You use this method to clean up any resources (such as memory, file handles, and threads) that are being held and make sure that any persistent state is synchronized with the servletís current state in memory. The following is the destroy methodís signature:

public void destroy()

Buy this book now!Remember: This is part one of the first chapter of JavaServer Faces Programming, by Budi Kurniawan (McGraw-Hill/Osborne, ISBN 0-07-222983). Stay tuned for part 2 of "Overviews of Java Web Technologies," where we learn about JSP, JavaBeans, and Model 2. 
Buy this book!

>>> More Java & J2EE Articles          >>> More By McGraw-Hill/Osborne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: