Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Servlets in Apache Tomcat and BEA Systems' WebLogic Server - JavaWorld February 2001

Servlets in Apache Tomcat and BEA Systems' WebLogic Server - JavaWorld February 2001

Tutorial Details:

Servlets in Apache Tomcat and BEA Systems' WebLogic Server
Servlets in Apache Tomcat and BEA Systems' WebLogic Server
By: By Steven Gould
Deploy servlets and Web applications in two popular application servers
n " 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.
HTTP servlets
HTTP servlets extend the javax.servlet.http.HttpServlet class. This class extends the javax.servlet.GenericServlet class, which in turn implements javax.servlet.Servlet . The HttpServlet class overrides the service() method in such a way as to handle the different types of HTTP requests: DELETE, GET, OPTIONS, POST, PUT, and TRACE. For each of these request types the HttpServlet class provides a corresponding doXXX() method.
Although you can override the service() method in your servlet class, there is rarely any need to do so. More likely you'll want to override individual doXXX() methods. If you do override the service() method, be aware that the default doXXX() methods will be called only if you call super.service or invoke them directly.
For most applications you will want to override the doPost() and doGet() methods, since one of these usually handles data submitted by a user from an HTML FORM.
To summarize, when writing your HTTP servlets you should:
Import -- at a minimum -- the servlet classes:
javax.servlet.ServletException
javax.servlet.http.HttpServlet
javax.servlet.http.HttpServletRequest
javax.servlet.http.HttpServletResponse
Make the class public
Have the class extend HttpServlet
Override the appropriate doXXX() method(s) to implement your request/response logic
We illustrate these with a simple example below.
A sample servlet: RequestDetails
In the example below we illustrate a simple HTTP servlet. The first line simply defines what package the servlet belongs to. The next code block imports the classes used by this servlet. Then comes the servlet class definition. As you can see, the RequestDetails class extends HttpServlet .
The body of RequestDetails defines two methods: doGet() and doPost() . The doGet() method defines the primary functionality of this servlet. The doPost() method simply calls doGet() . The servlet therefore handles both get and post requests in the same way.
The doGet() method constructs an HTML page containing details of the HTTP request sent to the server. Note the method's first two lines. The first line sets the content type for the response. In general, you will be constructing an HTML page, in which case the content type should be set to text/html . The second line in the doGet() method obtains a reference to a PrintWriter output stream. All output to be returned to the client is then written to that output stream:
package org.stevengould.javaworld;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* This class provides a simple example of a servlet, and
* illustrates some of the information available from an
* HTTP request.
*/
public class RequestDetails extends HttpServlet
{
/**
* Handler for all GET requests. We simply dump out the
* request header information, followed by the body of
* the request.
* @param request the HTTP request submitted to the
* server for processing. It is this object that
* contains the details of the requested URL, and
* it is the details of this object that we
* output as a response.
* @param response the response object to be used to
* send a result back to the client.
* @exception IOException thrown if a communications
* error occurs.
* @exception ServletException if the GET request could
* could not be handled
*/
public void doGet( HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("Request Details Example");
out.println("");
out.println("");
out.println("

HTTP Request Header

");
out.println("");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements())
{
String name = (String)e.nextElement();
String value = request.getHeader(name);
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
}
out.println("
NameValue
"+name+""+value+"
");
out.println("

HTTP Request Information

");
out.println("");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println(" ");
out.println("
NameValue
Method:"+request.getMethod()+"
Request URI:"+request.getRequestURI()+"
Protocol:"+request.getProtocol()+"
PathInfo:"+request.getPathInfo()+"
Remote Address:"+request.getRemoteAddr()+"
");
out.println("
");
Date date = new Date();
out.println("

Page generated on "+date);
out.println("");
out.println("");
out.close();
}
/**
* For POST requests, we will simply perform the same
* operations as for GET requests. The best way to do this
* is to simply invoke the doGet() method with the appropriate
* parameters.
* @param request the HTTP request submitted to the server
* for processing. It is this object that contains
* the details of the requested URL, and it is the
* details of this object that we output as a
* response.
* @param response the response object to be used to send a
* result back to the client.
*/
public void doPost( HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException
{
doGet(request, response);
}
}
Compile the servlet
Since servlets use Java extension classes (classes that are not part of the core JDK) you must be sure to correctly set up your CLASSPATH before attempting to compile any servlet. The Java compiler needs to be able to find the javax.servlet.* packages and classes. Other than that, compilation proceeds just as for any other Java program:
javac RequestDetails.java
Create


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Servlets in Apache Tomcat and BEA Systems' WebLogic Server - JavaWorld February 2001

View Tutorial:
Servlets in Apache Tomcat and BEA Systems' WebLogic Server - JavaWorld February 2001

Related Tutorials:

Connect the enterprise with the JCA, Part 1
Connect the enterprise with the JCA, Part 1
 
Ilog JRules 4.0: Working by the rules
Ilog JRules 4.0: Working by the rules
 
Servlet 2.4: What's in store
Servlet 2.4: What's in store
 
The Java Web Services Tutorial
This tutorial is a beginner\'s guide to developing Web services and Web applications using the Java Web Services Developer Pack (Java WSDP).
 
Comparing The Performance of J2EE Servers
Performance ReportThe standardization of the application server, thanks to Sun\'s J2EE specifications, has spawned a wealth of implementations. There are offerings from big players such as Sun, IBM, BEA and Oracle as well as numerous offerings from low-co
 
Turn EJB components into Web services
Summary Web services have become the de facto standard for communication among applications. J2EE 1.4 allows stateless Enterprise JavaBeans (EJB) components to be exposed as Web services via a JAX-RPC (Java API for XML Remote Procedure Call) endpoint, al
 
Introduction to JSP
Introduction to JSP Introduction to JSP Java Server Pages or JSP for short is Sun's solution for developing dynamic web sites. JSP provide excellent server side scripting support for creating database driven web applications. JSP enable the
 
Internet & Intranets: Java Servlets
What are servlets? "Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to upd
 
JSP (JavaServer Pages) is a standard for combining Java and HTML to provide dynamic content in web pages.
With JSP, you embed Java code in HTML using special JSP tags similar to HTML tags. You install the JSP page, which has a .jsp extension, into the WebLogic Server document root, just as you would a static HTML page. When WebLogic Server serves a JSP page..
 
Networking our whiteboard with servlets.
Find out how to easily replace the RMI and sockets networking layers with servlets.
 
BEA WebLogic Portal JSP Tag Libraries
The BEA WebLogic Portal includes four JSP tag libraries that are used by the portal's JSP pages.
 
The JavaTM Web Services Tutorial
A beginner's guide to developing Web services and Web applications on the Java Web Services Developer Pack
 
High availability Tomcat Connect Tomcat servers to Apache and to each other to keep your site running
If you run only one instance of Tomcat, you lose requests/sessions whenever you upgrade or restart your site. In this article, author Graham King presents simple steps for connecting a pair (or more) of Tomcats to Apache using the JK2/AJP (Apache JServ Pr
 
What is Persistence Framework?
What is Persistence Framework? What is Persistence Framework? A persistence framework moves the program data in its most natural form (in memory objects) to and from a permanent data store the database. The persistence framework manages the
 
Application Servers Available in Market. Web Servers. J2EE server.
Application Servers Available in Market. Web Servers. J2EE server. Application Servers Available in Market Before we go into the grater details of the EJB let's look at some of the EJB Application Servers available in the market. Application
 
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial.
Introduction To Enterprise Java Bean(EJB). WebLogic 6.0 Tutorial. Welcome to EJB Section (Learn to Develop World Class Applications with Enterprise Java Beans) (Online WebLogic 6.0 Tutorial) Introduction To Enterprise Java Bean(EJB) Enterprise
 
Introduction To Enterprise Java Bean(EJB). Developing web component.
Introduction To Enterprise Java Bean(EJB). Developing web component. Developing web component Introduction To Java Beans J2EE specification defines the structure of a J2EE application. According to the specification J2EE application consists of
 
Downloading and Installing WebLogic server 6.0
Downloading and Installing WebLogic server 6.0 Downloading and Installing WebLogic server 6.0 You can download the BEA WebLogic Server for http://www.bea.com to test and run the examples described in this tutorial. BEA WebLogic Server™, is world
 
Accessing Database from servlets through JDBC!
Accessing Database from servlets through JDBC! Java Servlets J ava Servlets are server side components that provides a powerful mechanism for developing server side of web application. Earlier CGI was developed to provide server side capabilities
 
Struts Guide
Struts Guide Struts Guide This tutorial is extensive guide to the Struts Framework. In this tutorial you will learn how to develop robust application using Jakarta Struts Framework. This tutorial assumes that the reader is familiar with the web
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.