Home arrow Java arrow Page 3 - How to Develop Servlets
JAVA

How to Develop Servlets


Servlets are Java classes that run on servers. In this article, the first of three parts, you will learn how to create, save and compile a servlet. This article is excerpted from chapter five of the book Murach's Java Servlets and JSP, written by Andrea Steelman and Joel Murach (Murach; ISBN: 1890774189).

Author Info:
By: Murach Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
September 14, 2006
TABLE OF CONTENTS:
  1. · How to Develop Servlets
  2. · The code for the EmailServlet class
  3. · How to create a servlet
  4. · How to save and compile a servlet

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
How to Develop Servlets - How to create a servlet
(Page 3 of 4 )

Now that you have a general idea of how servlets are coded, you’re ready to learn some specific skills for creating a servlet. To start, you need to know how to begin coding the class for a servlet.

How to code a servlet

Figure 5-3 shows the basic structure for a typical servlet that performs some processing and returns an HTML document to the browser. You can use this basic structure for all the servlets you write. For now, that’s all you need to get started, but you’ll learn another way to structure servlets in the next chapter.

Since most servlets are stored in a package, the first statement in this servlet specifies the package for the servlet. This package must correspond to the directory that the servlet is saved in. In the next figure, you’ll learn more about how this works.

The next three statements are the import statements that are required by all servlets. The javax.servlet.http package is required because it contains the HttpServletRequest and HttpServletResponse classes. The javax.servlet class is required because it contains the ServletException class. And the java.io class is required because it contains the IOException class.

After the first four statements, the class declaration provides the name for the servlet and indicates that it extends the HttpServlet class. Although in theory a servlet can extend the GenericServlet class, all servlets for web applications extend the HttpServlet class.

The doGet and doPost methods in this figure accept the same arguments and throw the same exceptions. Within these methods, you can use the methods of the request object to get incoming data, and you can use the methods of the response object to set outgoing data. In this structure, since the doPost method calls the doGet method, an HTTP request that uses the Post method will actually execute the doGet method of the servlet. This is a common programming practice that allows a servlet to use the same code to handle both the Get and Post methods of an HTTP request.

In the doGet method, the first statement calls the setContentType method of the response object. This sets the content type for the HTTP response that’s returned to the browser to “text/html,” which specifies that the servlet returns text or HTML. In chapter 15, you’ll learn how to return other types of data.

The second statement in the doGet method obtains a PrintWriter object named out from the response object. This object can be used to return character data to the client. Once you have this object, you can use one println statement or a series of print and println statements to return HTML or other text to the browser. However, as you learned in the last figure, you must code a backslash before any quotation marks that don’t start or end a string. Otherwise, the servlet won’t compile properly.

The basic structure of a servlet class

  package packageName;

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

  public class ServletName extends HttpServlet{

    public void doGet(HttpServletRequest request, 
                      HttpServletResponse response)
                      throws IOException, ServletException{

      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      //business processing

      out.println("response as HTML");
    }

    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response)
                       throws IOException, ServletException {
      doGet(request, response);
    }

  }

Description
  • All web-based servlets extend the HttpServlet class. To extend this class, the servlet must import the javax.servlet, javax.servlet.http, and java.io packages.
  • The doGet method overrides the doGet method of the HttpServlet class and processes all HTTP requests that use the Get method, and the doPost method overrides the doPost method of the HttpServlet class and processes all HTTP requests that use the Post method.

  • The doGet and doPost methods use two objects that are passed to it by the web server: (1) the HttpServletRequest object, or the request object, and (2) the HttpServletResponse object, or the response object.

  • The setContentType method of the response object sets the content type of the response that’s returned to the browser. Then, the getWriter method of the response object returns a PrintWriter object that can be used to send HTML to the browser.
  • Before you can create a PrintWriter object, you must set the content type. This allows the getWriter method to return a PrintWriter object that uses the proper content type.

Figure 5-3.  How to code a servlet


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials