BEA Logo BEA WebLogic Server Release 6.0

  Corporate Info  |  News  |  Solutions  |  Products  |  Partners  |  Services  |  Events  |  Download  |  How To Buy

   Programming WebLogic HTTP Servlets:   Previous topic   |   Next topic   |   Contents   |  Index

 

Introduction to Programming

 

This section introduces basic HTTP servlet programming. The following topics are discussed:

Writing a Basic HTTP Servlet

The section provides a procedure for writing a basic HTTP servlet. A complete code example (the HelloWorldServlet) illustrating these steps is included at the end of this section. Additional information about using various J2EE and Weblogic Server services, such as JDBC, RMI, and JMS, in your servlet are discussed later in this document.

  1. Import the appropriate packages and classes, including the following:

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;

  2. Extend javax.servlet.http.HttpServlet. For example:

    public class HelloWorldServlet extends HttpServlet{

  3. Implement a service() method. The main function of a servlet is to accept an HTTP request from a Web Browser, and return an HTTP response. This work is done by the service() method of your servlet. Service methods include response objects used to create output and request objects used to receive data from the client.

    You may have seen other servlet examples implement the doPost() and/or doGet() methods. These methods reply only to POST or GET requests; if you want to handle all request types from a single method, your servlet can simply implement the service() method. (However, if you choose to implement the service() method, you will not be able to implement the doPost() or doGet() methods, unless you call super.service() at the beginning of the service() method.) The HTTP servlet specification describes other methods used to handle other request types, but all of these methods are collectively referred to as service methods.

    All the service methods take the same parameter arguments. An HttpServletRequest provides information about the request, and an HttpServletResponse, is used by your servlet to reply to the HTTP client.

    public void service(HttpServletRequest req,
    HttpServletResponse res) throws IOException
    {

  4. Set the content type, as follows:

    res.setContentType("text/html");

  5. Get a reference to a java.io.PrintWriter object to use for output, as follows:

    PrintWriter out = res.getWriter();

  6. Create some HTML using the println() method on the PrintWriter object, as shown in the following example:

    out.println("<html><head><title>Hello World!</title></head>");
    out.println("<body><h1>Hello World!</h1></body></html>");
    }
    }

  7. Compile the servlet, as follows

    1. Set up a development environment shell with the correct classpath and path settings.

    2. From the directory containing the Java source code for your servlet, compile your servlet into the WEB-INF/classes directory of the Web Application that contains your servlet. For example:

      javac -d /myWebApplication/WEB-INF/classes myServlet.java

  8. Deploy the servlet as part of a Web Application hosted on WebLogic Server. For an overview of servlet deployment, see Administration and Configuration

  9. Call the servlet from a browser.

    The URL you use to call a servlet is determined by: (a) the name of the Web Application containing the servlet and (b) the name of the servlet as mapped in the deployment descriptor of the Web Application. Request parameters can also be included in the URL used to call a servlet.

    Generally the URL for a servlet conforms to the following:

    http://host:port/webApplicationName/mappedServletName?parameter

    The components of the URL are defined as follows:

Advanced Features

The preceding steps create a basic servlet. You will probably also want to use some of the more advanced features of servlets, which are described briefly below:

Complete HelloWorldServlet Example

This section provides the complete Java source code for the example used in the preceding procedure. The example is a simple servlet that provides a response to an HTTP request. Later in this document, this example is expanded to illustrate how to use HTTP parameters, cookies, and session tracking.

Listing 2-1 HelloWorldServlet.java


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class HelloWorldServlet extends HttpServlet {
public void service(HttpServletRequest req,
HttpServletResponse res)
throws IOException
{
// Must set the content type first
res.setContentType("text/html");
// Now obtain a PrintWriter to insert HTML into
PrintWriter out = res.getWriter();

    out.println("<html><head><title>" +
"Hello World!</title></head>");
out.println("<body><h1>Hello World!</h1></body></html>");
}
}


You can find the source code and instructions for compiling and running all the examples used in this document in the samples/examples/servlets directory of your WebLogic Server distribution.

 

Back to Top