Servlet Basics
Concepts
- Overview
- Basic Servlet Structure
- A Servlet That Generates Plain Text
- A Servlet That Generates HTML
- The Servlet Life Cycle
- Servlet Debugging
Servet Overview
- Read the explicit data sent by the client.
- This is what is typed into the location field in a browser, or a link that is clicked on, or an uploaded file, etc. Whatever you intend to send to the server.
- Read the implicit HTTP request data sent by the client.
- The HTTP Request Header records.
- Generate the results.
- The servlet does some work like talk to a database, work with the HTTP session, access collections, create JavaBeans, etc.
- Send the explicit data.
- There are many ways that a web application can return something to a browser.
- What is returned could be an HTML page but also other things like audio, video, or images.
- Send the implicit data.
- The HTTP Response Headers
Basic Servlet Structure
-
Import these packages:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*;
-
Subclass HttpServlet
public class TrivialServlet extends HttpServlet {
-
Annotate the servlet
@WebServlet( name = "trivialServlet", urlPatterns = { "/trivial", "/simple" } )
Note
The annotation above only requires one urlPattern. The use of two patterns above is shown only as an example.
-
Implement a method with this signature:
// The main servlet method public void doGet(HttpServletRequest request, HttpServletResponse response throws ServletException, IOException {
-
The
doGet()
method is where all the work is done.- Another look at TrivialServlet.java
A Servlet That Generates Plain Text
- A simple servlet that outputs plain text.
- View SimpleTextServlet
- Download SimpleTextServlet.java (Right-click and Save Link As...)
A Servlet That Generates HTML
- A simple servlet that outputs html.
- View SimpleHtmlServlet
- Download SimpleHtmlServlet.java (Right-click and Save Link As...)
The Servlet Life Cycle
- Only one instance of a servlet is loaded into the servlet engine! That one servlet instance will handle all the requests for that servlet. Even there are many simultaneous requests the one instance will handle them all.
init()
method
- Runs only once before the servlet is available to the browser.
- Is used to initialize the servlet.
- Is used for general initializations
- Is used for initializations controlled by initialization parameters.
public void init() throws ServletException { //Initialization code }
doGet()
and doPost()
methods
- Contain the real work in a servlet.
- Are called based on the HTTP request. If the request is a GET then
doGet()
is called. If it is a POST thendoPost()
is called. - Called once for every request from a browser.
-
Are handed the Request and Response objects for processing.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //The real heart of java web programming here! }
-
Servlets run in a multi-threaded environment. (More on this coming up!)
destroy()
method
- Is used to shutdown the resources started up in the
init()
method. - Is called once when the servlet engine is shutting down.
- Is not required and is often not used.
public void destroy() { //Code to shut things down }