anchor.diff:  In this document, web requests are considered to be HTTP requests. [small]#(The specification is more general but I doubt anybody would seriously consider writing non HTTP servlets in a Java EE application server.)#
   
   == Servlets in Java EE
  +
  +=== My first web project
  +To start a Maven compatible web project, create a new folder, use https://github.com/oliviercailloux/samples/blob/master/JavaEE-Servlets/pom.xml[this] sample pom, then import the project into Eclipse.
   
   === My first servlet
   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:
  
positive.code: (You may define more than one servlet. [small]#The constraints described above are not the only way to define a Java EE compliant servlet.#)
  
  === Packaging and deploying the application
  You then package your application as a WAR (Web ARchive format), and deploy it to your application server. (Exact procedures to do this depend on your IDE for the WAR and on your application server for the deployment, see link:App%20servers%20from%20Eclipse.adoc[here] for 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 typically the name of your WAR file without the extension.)
  
  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")`.
  
  === Execution
  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, http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html[`HttpServletRequest`] and http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletResponse.html[`HttpServletResponse`]. 
  
  * The `HttpServletRequest` object 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 `HttpServletResponse` object represents the response being constructed: your code uses this object to specify the response that the server must sent back to the client.
  
negative.code: == Test servlets from command line
  Once you have written your servlet, you want to easily test it. See link:HTTP.adoc#curl[HTTP].
  
  If you use Glassfish, some useful debugging tips are link:Glassfish.adoc[there].
  
  == Your turn
  * Write (and test) a servlet that says: “Hello, world” in plain text, as response to a GET request.
  * A servlet that is able to add two numbers given in parameters of the GET request. (Hint: check the javadoc of `HttpServletRequest`.)
  * 
  == Refs
  * Servlet 4.0 (JSR 369) specification: http://download.oracle.com/otn-pub/jcp/servlet-4-final-spec/servlet-4_0_FINAL.pdf[PDF]
  * https://javaee.github.io/tutorial/servlets.html[Java Servlet Technology] in the Java EE 8 Tutorial
  
  == Supplementary details
  
  