Servlets in Apache Tomcat and BEA Systems' WebLogic Server
Deploy servlets and Web applications in two popular application servers
By Steven Gould, JavaWorld.com, 02/23/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
In "
Develop N-Tier Applications Using J2EE" (
JavaWorld, December 1, 2000), I gave an introduction to Java 2 Enterprise Edition (J2EE) and an overview of the technologies it includes.
In this article, we delve into a little more detail with one of the more widely used J2EE technologies: servlets. We begin
with a brief recap of servlet development fundamentals, then show how to build a Web application to house them. We discuss
the use of Web Application Archives (WARs), then illustrate deployment of the servlet and Web application in Apache Tomcat.
In addition, we'll look at deploying the same servlet and Web application in one of the most widely-used servlet containers
-- BEA's WebLogic Server.
Develop servlets
Servlets were designed to allow for extension of a server providing any service. Currently, however, only HTTP and JSP page
servlets are supported. In the future, a developer may be able to extend an FTP server or an SMTP server using servlets.
Generic servlets
A servlet extends a server's functionality by offering a specific service within a well-defined framework. It is a small piece
of Java code -- often just a single class -- that provides a specific service. For example, an HTTP servlet may provide a
bank customer with details of her recent deposits and withdrawals. Another HTTP servlet could allow a customer to view, and
even edit, his mailing address.
To deploy a servlet usually requires configuration of the hosting server application. When the server encounters a particular
type of request, it invokes the servlet, passing to it details about the request and a response object for returning the result.
All servlets implement the javax.servlet.Servlet interface either directly -- in the case of generic servlets -- or indirectly, in the case of HTTP or JSP servlets. The javax.servlet.Servlet interface's important methods include:
init(): defines any initialization code that should be executed when the servlet loads into memory.
service(): the main method called when the servlet receives a service request. It defines the bulk of the processing logic provided
by the servlet.
destroy(): defines any clean-up code required before removing the servlet from memory.
When the servlet container first loads a servlet it invokes the servlet's init() method to initialize the servlet. Then, as requests are made to execute the servlet, the servlet container repeatedly invokes
the servlet's service() method to provide the required service. Finally, when the servlet container no longer needs the servlet, it invokes the servlet's
destroy() method and unloads it from memory. Note that during the lifetime of a single servlet instance, the init() and destroy() methods will be invoked only once, whereas the service() method will be invoked many times -- once each time a request is made to execute the servlet.
JSP Page servlets are mostly of interest to implementers of JSP containers and are beyond the scope of this article. Rather,
we now go on to look specifically at HTTP servlets.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- My recent article, "Develop N-Tier Applications Using J2EE" (JavaWorld, December 1, 2000), gives an overview of the different technologies included within J2EE, as well as WebLogic Server's support
of those technologies
http://www.javaworld.com/javaworld/jw-12-2000/jw-1201-weblogic.html
- "What's New in Java Servlet API 2.2?" Jason Hunter (JavaWorld, October 1999) provides a good overview of Web applications and WARs
http://www.javaworld.com/jw-10-1999/jw-10-servletapi.html
- "Get the App Out," Chang Sau Sheong (JavaWorld, January 19, 2001) describes the ins and outs of J2EE assembly and deployment
http://www.javaworld.com/javaworld/jw-01-2001/jw-0119-j2eeassembly.html
- For more JavaWorld articles covering J2EE, visit the Server-Side Java section of our Topical Index
http://www.javaworld.com/javaworld/topicalindex/jw-ti-ssj.html
- For timely server-side tips and news, subscribe to our free Java in the Enterprise weekly newsletter (find it under the "Application Development Series" section)
http://reg.itworld.com/cgi-bin/subcontent12.cgi
- For answers to your pressing J2EE questions, check out the JavaWorld Server-Side Java discussion
http://www.itworld.com/jump/jw-0223-extremescale/forums.itworld.com/webx?14@@.ee6bdcf
- Talk about Web application servers in the Web Servers & Software discussion
http://www.itworld.com/jump/jw-0223-servletweblogic/forums.itworld.com/webx?230@@.ee6b50a!skip=-23
- Sign up for the JavaWorld This Week free weekly email newsletter to keep up with what's new at JavaWorld
http://www.idg.net/jw-subscribe
- The Servlet API (version 2.2) documentation describes all classes and methods available as part of the Servlet API
http://java.sun.com/products/servlet/2.2/javadoc/
- Sun's J2EE page contains the latest information and updates to the J2EE specifications
http://java.sun.com/j2ee/
- You'll also find Sun's J2EE Documentation page a useful source of J2EE documents, FAQs, and developer guides
http://java.sun.com/j2ee/docs.html
- Professional Java XML Programming with Servlets and JSP, Alexander Nakhimovsky and Tom Myers (Wrox Press, 1999) provides good practical coverage of using servlets and JSPs with XML
http://www.amazon.com/exec/obidos/ASIN/1861002858/javaworld
- The Apache Jakarta project houses the main Tomcat site, available at
http://jakarta.apache.org/tomcat/
- BEA Systems' WebLogic Server site offers extensive documentation on installing, configuring, administering, and developing
with WebLogic Server. It should be the first place you look for help with any WebLogic-related questions you have
http://www.weblogic.com
- BEA Systems' "Deploying and Configuring Web Applications" gives detailed instructions on deploying and configuring Web applications
using WebLogic Server 6.0
http://e-docs.bea.com/wls/docs60/////adminguide/config_web_app.html
- Sun's Java Developer Connection -- a must for every Java developer -- features great technical documentation, FAQs, white
papers, and more
http://developer.java.sun.com/developer