# 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.

## The servlets
### Example of servlet
#### Description

We notice two methods in this class: doGet and doPost. 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, doPost forwards to doGet.

### Deployment on Tomcat

* Put the compiled class with its package in the directory classes in WEB-INF of the working web directory (here: webTest)
* Edit web.xml in WEB-INF by adding:
So the WEB-INF contains:

### Call of the servlet

http://localhost:999/webTest/Hello
Result in the browser:

### 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

### Request methods for adding headers

The HTTP headers adds complementary informations to the response.

### Construction of the response body

* (PrintWriter)getWriter allows to transfer text into the HTTP response body.
* (ServletOutputStream)getOutputStream allows to transfer binary data into the HTTP response body.
Never use the both at the same time, or you will have an IllegalStateException exception.
Here is an example of binary sending:
In this example, we use processRequest(request, response) 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
* setStatus(int rc)
* sendError(int sc, String message)
...allow to set the error status of the page. As an example, we will generate a server error.

## Other ressources
### RequestDispatcher
#### include

Inclusion of an external resource.

#### forward

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

### Redirect

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

#### redirect
## 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 javax.servlet.Filter.

## 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 HttpSessionListener, HttpSessionAttributeListener.

## 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. synchronized(this) must be applied to the critical processes like counters.