This document gives a (rough and incomplete) overview of Servlets in Java EE application servers.
|
Note
|
The reader supposedly has basic knowledge of HTTP, Maven and application servers. (You are not obliged to use WildFly, though.) |
A servlet is a piece of software that, given some web request parameters, returns some response to the request. You, the web application developer, write the code that, given the request parameters, computes the response. That code gets embedded into the “web” container of your application server.
The web container is one of the containers provided by any Java EE compliant application server. The web container listens for web requests on some port and will call your servlet code whenever it detects a web request directed to your servlet. The Java servlet specification also defines how to tell the application server which web requests should be sent to your servlet code.
An application will typically contain many such servlets, configured to process different kinds of web requests.
In this document, web requests are considered to be HTTP requests. (The specification is more general but I doubt anybody would seriously consider writing non HTTP servlets in a Java EE application server.)
To start a Maven compatible web project:
-
create a new folder,
-
use this sample pom,
-
change the groupId and artifactId to name your new project,
-
create the default folders that Maven expects:
/src/main/javaand (optionally)/src/main/resources,/src/test/java,/src/test/resources -
import the project into Eclipse.
For you to define a new servlet that will be recognized as such by the container, you must define a new class that represents your servlet, and:
-
your class must extend
HttpServlet(a class provided by Java EE); -
your class must be annotated with
@WebServlet; -
your class overrides one method, depending on the request method you want it to handle.
The value of the @WebServlet annotation is the URI at which your servlet must be reached.
(You may define more than one servlet. The constraints described above are not the only way to define a Java EE compliant servlet.)
You then package your application as a WAR (Web ARchive format), and deploy it to your application server.
-
Do it first with no help from your IDE: use
mvn package, then move the resulting WAR file into the appropriate deployment folder. -
See here for automatic deployment using Eclipse.
Each Java EE application has a context root, the root URI at which your application is reachable on the server. If your application context root is MyContextRoot and your application is deployed on a server reachable at http://example.com, then your application is reachable at the http://example.com/MyContextRoot root URI. (The context root can be configured on your application server, by default it is usually the name of your WAR file without the extension. You will usually use localhost as a host name while developing.)
The web container will recognize your servlets at deployment time. When the server will receive an HTTP request targeted at your application (meaning: targeted at the corresponding context root), it will examine its target URI and dispatch it to the appropriate servlet, according to the values in the @WebServlet annotations. For example, a request targeted at http://example.com/MyContextRoot/sayHelloServlet will be sent to the application corresponding to the MyContextRoot context root, and inside this application, to the servlet annotated with @WebServlet("/sayHelloServlet").
The container will then instanciate your class and execute your code, under the method corresponding to the request method in the request (example: doGet for a GET request method). The container gives the method two Java EE objects as parameters, HttpServletRequest and HttpServletResponse.
-
The
HttpServletRequestobject represents the request message: your code can read from this object to obtain information in the request, such as which data is contained in the headers or the message body of the request. -
The
HttpServletResponseobject represents the response being constructed: your code uses this object to specify the response that the server must sent back to the client.
Observe that the container makes your life as web developer much simpler: you do not have to listen to ports, spawn threads to process incoming requests, decode and encode requests and responses on the wire, manage sockets, parse HTTP headers, and so on. The container does this for you. This is an example of the services that Java EE application servers provide to the developers. (Of course, other frameworks than Java EE provide this kind of services to developers in Java or other languages.)
-
Write (and test) a servlet that says: “Hello world” in plain text, as response to a GET request.
-
Log what you do in your servlet code: just use
System.out.println, the container will by default redirect the output to the log file. Check that it appears indeed in the log. -
Return “Hello world” in HTML instead of text:
<html><body><p>Hello world</p></body></html>. How can you make sure this will be interpreted as HTML and not plain text? What would be the difference for the user? -
Make sure that special characters are shown correctly. Test by sending rather “Éh salut !”.
-
A servlet that is able to add two numbers given in parameters of the GET request. (Hint: check the javadoc of
HttpServletRequest.) -
If one of the parameters is missing or is not a number or if multiple values are associated to a given parameter, return an error message. (Hint: check the javadoc of
HttpServletResponse.) -
Add a static HTML
index.htmlfile to your project, in the foldersrc/main/webapp. Maven will package it correctly in the WAR file (it will put it in the root of the archive) so that it is visible by your users. Browse tohttp://localhost/MyContextRoot/index.htmlto test it.
-
Servlet 4.0 (JSR 369) specification: PDF
-
Java Servlet Technology in the Java EE 8 Tutorial
Inversion of control Instead of you providing a main method as a starting point of your application, in Java EE, your code gets embedded into a container provided by the application server, and the container calls your code when adequate. This is an example of the pattern “Inversion of control”. The pattern is also called the Hollywood principle: “Don’t call us, we’ll call you”.