Jakarta EE Programming/Servlet

A servlet is a Java class that runs on the server side that receives HTTP data and does a processing or more following the constraints of the HTTP protocol.

Description
We notice two methods in this class:  and. The first answers by HTTP to the reception of a GET request, the second to the reception of a POST request. As we want that in the both cases the servlet processes the request,  forwards to.

Deployment on Tomcat
So the  contains:
 * Put the compiled class with its package in the directory  in   of the working web directory (here:  )
 * Edit  in   by adding:

WEB-INF web.xml classes servlet Hello.class

Call of the servlet
http://localhost:999/webTest/Hello

Result in the browser: Hello World

Decomposition of the URL
[Protocol://][DNS]:[PORT]/[RootEntryDirectory]/[ServletName]

Information methods about the URL
Example in the servlet:

The related call: http://localhost:999/webTest/Hello localhost 999 /webTest /Hello GET null http://localhost:999/webTest/Hello 127.0.0.1 localhost 999 127.0.0.1 127.0.0.1

Request methods for adding headers
The HTTP headers adds complementary informations to the response.

Construction of the response body
Never use the both at the same time, or you will have an  exception.
 * allows to transfer text into the HTTP response body.
 * allows to transfer binary data into the HTTP response body.

Here is an example of binary sending: In this example, we use  to always get the same process in GET or POST.

Constructing the HTTP response
The status codes are splitted into 5 categories:
 * 1XX: informative
 * 2XX: success
 * 3XX: redirection
 * 4XX: client error
 * 5XX: server error

The details of these codes are available at http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html



...allow to set the error status of the page. As an example, we will generate a server error.

include
Inclusion of an external resource.

forward
Servlet result transfer to another resource. The destination resource:

Redirect
The server informs the browser to send a HTTP request to the given URL.

Filters
The filters add a process on a HTTP request before it is received by the servlet or on the HTTP response before it is returned.

Its roles are:


 * Access control
 * Access log
 * Data decompression
 * Decipherment
 * Data conversion

Several responses can be executed successively. The filter class must implement.

Events
The events inform about the application operations like the instantiation and the destruction of the objects.
 * Application-related events: Those triggered by the servlet context.
 * Session-related events : Those classes must implement,.

Servlet synchronization
This synchronization avoids the server to use two threads at the same time, for example, when two instances of the same class call a same method with two different clients. must be applied to the critical processes like counters.

Programmation JEE/Servlets