DEVELOPING SERVLETS
2.1 Creating and Mapping a Servlet
How a Servlet Application works
Web container is responsible for managing execution of servlets and JSP pages for Java EE application.
When a request comes in for a servlet, the server hands the request to the Web Container
Web Container is responsible for instantiating the servlet or creating a new thread to handle the request. Its the job of Web Container to get the request and response to the servlet. The container creates multiple threads to process multiple requests to a single servlet.
Servlet don't have a main() method. Web Container manages the life cycle of a Servlet instance
How a Servlet works
User sends request for a servlet by clicking a link that has URL to a servlet
The container finds the servlet using deployment descriptor and creates two objects:
-HttpServletRequest
-HttpServletRespose
Then the container creates or allocates a thread for that request and calls the Servlet's service() method and passes the request, response objects as arguments
The service() method then decides which servlet method, doGet() or doPost() to call, based on HTTP Request Method(Get, Post etc) sent by the client.
Then the Servlet uses response object to write the response back to the client
After the service() method is completed the thread dies. And the request and response objets are ready for garbage collection
2.3 Validating Data
Validating data on the client
- For simple validation work, JAVASCRIPT code can be embedded into an HTML page to perform simple validation work client-side, before the form data is transmitted across the internet and alert the user to any problems.
Validating data on server
- Client Program-UserPass
- Servlet Program-Validation.java
2.2 Handling the Client Request
Handling Client Request form data :
- Whenever we want to send an input to a servlet that input must be passed through html form
- An html form is nothing but various controls are inherited to develop an application
-Every form will accept client data end it must send to a servlet which resides in server side
HTTP Get Method
GET Method
- The GET method sends the encoded user informationappended to the page request. The page and the encoded information are seperated by the ?(question mark) symbol
HTTP Post Method
POST Method
- A generally more reliable method of passing information to a backend program is the POST method. This packages the information in exactly the same way as GET method, but instead of sending it as a text string after a ?(question mark) in the URL it sends it as a seperate message. This message comes to the backend program in the form of the standard input which you can parse and use for you processing. Servlet handles this type of requests using doPost() method
Use forward request
INCLUDE()
- include() method includes the content of a resource in the response, the resource could be another Servlet, JSP or HTML file. Enables programmatic server-side includes
FORWARD()
- Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. Allows one servlet to do preliminary processing of a request and another resource to generate the response