What is a servlet?
A servlet is a Java class
that is used to extend the capabilities of servers that host
applications. Servlets can respond to requests and generate responses.
The base class for all servlets is javax.servlet.GenericServlet. This class defines a generic, protocol-independent servlet.
By far, the most common type of
servlet is an HTTP servlet. This type of servlet is used in handling
HTTP requests and generating HTTP responses. An HTTP servlet is a
class that extends the javax.servlet.http.HttpServlet class, which is a subclass of javax.servlet.GenericServlet.
A servlet must implement one or more methods to respond to specific HTTP requests. These methods are overridden from the parent HttpServlet class. As can be seen in the following table, these methods are named so that knowing which one to use is intuitive:
doGet(HttpServletRequest request, HttpServletResponse response)
doPost(HttpServletRequest request, HttpServletResponse response)
doPut(HttpServletRequest request, HttpServletResponse response)
doDelete(HttpServletRequest request, HttpServletResponse response)
Each of these methods take the same two parameters, namely an instance of a class implementing the javax.servlet.http.HttpServletRequest interface and an instance of a class implementing the javax.servlet.http.HttpServletResponse interface.
never call these methods directly. They are called automatically by the
application server whenever it receives the corresponding HTTP request.
Of the four methods listed previously, doGet() and doPost() are by far the most commonly used.
An HTTP GET request is
generated whenever a user types the servlet's URL in the browser, when a
user clicks on a link pointing to the servlet's URL, or when a user
submits an HTML form using the GET method, where the form's action
points to the servlet's URL. In any of these cases, the code inside the
servlet's doGet() method gets executed.
An HTTP POST request is
typically generated when a user submits an HTML form using the POST
method and an action pointing to the servlet's URL. In this case, the
servlet's code inside the doPost() method gets executed.
Writing our first servlet
In this section, we
will see how that servlet was developed, configured, and packaged.
The code for the servlet is as follows:
public class SimpleServlet extends HttpServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
PrintWriter printWriter = response.getWriter();
printWriter.println("If you are reading this, your application server is good to go!");
catch (IOException ioException)
As this servlet is meant to execute when a user enters its URL in the browser window, we need to override the doGet() method from the parent HttpServlet class. Like we explained previously, this method takes two parameters: an instance of a class implementing the javax.servlet.http.HttpServletRequest interface, and an instance of a class implementing the javax.servlet.http.HttpServletResponse interface.
Even though HttpServletRequest and HttpServletResponse
are interfaces, application developers don't typically write classes
implementing them. When control goes to a servlet from an HTTP request,
the application server (in our case, GlassFish) provides objects
implementing these interfaces.
The first thing our doGet() method does is set the content type for the HttpServletResponse
object to "text/html". If we forget to do this, the default content
type used is "text/plain", which means that the HTML tags used a couple
of lines down will be displayed on the browser, as opposed to them being
interpreted as HTML tags.
We then obtain an instance of java.io.PrintWriter by calling the HttpServletResponse.getWriter() method. We can then send text output to the browser by calling the PrintWriter.print() and PrintWriter.println() methods (the previous example uses println() exclusively). As we set the content type to "text/html", any HTML tags are properly interpreted by the browser.
Compiling the servlet
To compile the servlet, the Java library included with GlassFish must be in the CLASSPATH. This library is called javaee.jar and it can be found in the [glassfish installation directory]/glassfish/lib folder.
To compile from the command line using the javac compiler, a command like the following must be issued (all in one line):
Of course, these days very few developers compile code with the "raw" javac
compiler. Instead, either a graphical IDE or a command line build tool
such as Apache ANT or Apache Maven is used. Consult your IDE or build
tool documentation for information on how to add the javaee.jar library to its CLASSPATH.
javac -cp /opt/sges-v3/glassfish/lib/javaee.jar net/ensode/glassfishbook/simpleapp/SimpleServlet.java
Apache Maven is a build tool
similar to ANT. However, Maven offers a number of advantages over ANT,
including automatic download of dependencies and standard commands for
compilation and packaging of applications.
When using Maven, the code can
be compiled and packaged by issuing the following command in the
project's root directory (in this case, simpleapp): mvn package
Maven can be downloaded from http://maven.apache.org/.