Skip to Main Content
Java EE 6 Pocket Guide
book

Java EE 6 Pocket Guide

by Arun Gupta
September 2012
Intermediate to advanced content levelIntermediate to advanced
210 pages
3h 59m
English
O'Reilly Media, Inc.
Content preview from Java EE 6 Pocket Guide

Chapter 3. Servlets

Servlets are defined as JSR 315, and the complete specification can be downloaded from http://jcp.org/aboutJava/communityprocess/final/jsr315/index.html.

A servlet is a web component hosted in a servlet container and generates dynamic content. The web clients interact with a servlet using a request/response pattern. The servlet container is responsible for the lifecycle of the servlet, receives requests and sends responses, and performs any other encoding/decoding required as part of that.

Servlets

A servlet is defined using the @WebServlet annotation on a POJO, and must extend the javax.servlet.http.HttpServlet class.

Here is a sample servlet definition:

@WebServlet("/account")
public class AccountServlet 
  extends javax.servlet.http.HttpServlet {
  //. . .
}

The fully qualified class name is the default servlet name, and may be overridden using the name attribute of the annotation. The servlet may be deployed at multiple URLs:

@WebServlet(urlPatterns={"/account", "/accountServlet"})
public class AccountServlet 
  extends javax.servlet.http.HttpServlet {
  //. . .
}

The @WebInitParam can be used to specify an initialization parameter:

@WebServlet(urlPatterns="/account",
            initParams={
               @WebInitParam(name="type", value="checking")
                       }
           )
public class AccountServlet 
  extends javax.servlet.http.HttpServlet {
  //. . .
}

The Servlet interface has one doXXX method to handle each of HTTP GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE requests. Typically the developer is concerned with overriding ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java EE 7 Essentials

Java EE 7 Essentials

Arun Gupta
Beginning Java EE 7

Beginning Java EE 7

Antonio Goncalves
The Java EE 6 Tutorial: Advanced Topics, Fourth Edition

The Java EE 6 Tutorial: Advanced Topics, Fourth Edition

Eric Jendrock, Ricardo Cervera-Navarro, Ian Evans, Devika Gollapudi, Kim Haase, William Markito, Chinmayee Srivathsa

Publisher Resources

ISBN: 9781449338329Errata