Kategoriak: All - request - response - validation

arabera Boonjun Ang 6 years ago

213

DEVELOPING SERVLETS

Servlets are a key component in Java-based web applications, acting as the middle layer between client requests and server responses. They operate without a main() method, with their lifecycle managed by the Web Container.

DEVELOPING SERVLETS

DEVELOPING SERVLETS

2.2 Handling the Client Request

Use forward request
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
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
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
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
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

2.3 Validating Data

Validating data on server - Client Program-UserPass - Servlet Program-Validation.java
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.

2.1 Creating and Mapping a Servlet

How a Servlet works
After the service() method is completed the thread dies. And the request and response objets are ready for garbage collection
Then the Servlet uses response object to write the response back to the client
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 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 container finds the servlet using deployment descriptor and creates two objects: -HttpServletRequest -HttpServletRespose
User sends request for a servlet by clicking a link that has URL to a servlet
How a Servlet Application works
Servlet don't have a main() method. Web Container manages the life cycle of a Servlet instance
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.
When a request comes in for a servlet, the server hands the request to the Web Container
Web container is responsible for managing execution of servlets and JSP pages for Java EE application.