Advance Guide to Servlet Models

Servlet Models

Servlet Models


Two servlet Models are available:

1) Single Thread Model
2) Multi-Thread Model

Difference btw STM and MTM

1) Single Thread Model

  • To follow STM your java servlet class has to implement an interface called SingleThreadModel(Marker Interface).
  • In the case of STM, the servlet instance will be created for every incoming request. i.e, Multiple Instance will be created for STM.
  • It is not recommendable to use STM because multiple instances of the servlet will be reused, so no use of maintaining many instances for a long time. This may cause performance/memory wastage problem.
Note: STM is deprecated in the current version.

2) Multi-Thread Model

  • By default, container follow MultiThreadModel
  • Only one servlet instance will be created and the same instance will provide the service to every incoming request.
  • It is recommendable, because of no problem.

HttpServletRequest and HttpServletResponse

Http request: Request you are sending to a server using Http protocol.
HttpRequest contains two parts:

  • Http Request Header
  • Http Request Body

 Http Response:  Response which you are delivering to the client using Http protocol. It also contains two parts:

  • Http Response Header
  • Http Response Body
    Container is responsible for creating HttpServletRequest and HttpServletResponse objects.

HttpServletRequest contains the following information:

1. Request Parameters

2 Request Headers

3. Request Cookies

4. other information.

HttpServletResponse contains the following information

  • Response Headers
  • Response Stream

RequestDispatcher: It is an interface available in Javax.servlet. you can access the RequestDispatcher object by using the following method using ServletRequest or ServletContext object.

RequestDispatcher getRequestDispatcher(String resource): A resource can be either JSP, HTML, or URL of another servlet.

It has two methods as follows:

  • void forward(request, response)
  • void include(request, response)

Forward() Method: Forward() method is used to forward the request from a Servlet to Servlet or Servlet to JSP or JSP to Servlet or JSP to JSP. Once the request is forwarded to another component, then control will not be returned back.

Include() Method: Include() method is used to include the response of one servlet or JSP in another servlet or JSP and after evaluating the request control will return back. We can have more than one include() statement in one servlet or JSP but we can have only one forward() method in one servlet or JSP. After using forward you cannot use request and response object processing statements.

Http Methods

These are 7 Http methods which can be used to send the request.
  1. GET()
  2. POST()
  3. PUT()
  4. DELETE()
  5. HEAD()
  6. TRACE()
  7. OPTIONS()
You can specify the required Http Method as follows:

<form action=”..” method=”XX”>

</form>

You can collect the form action and http method as follows:

String action= request.getRequestURI();
String method=request.getMethod();
Default http method is GET()

Difference between GET and POST Method.

GET () method:

We can see request data in the URL

  1. By using Http method get() data will be attached to the Url as a query string.
  2. By using Http method get() you can send an only limited amount of data to browser limitation.
  3. Data will be visible at URL, not secured.
POST () method: 
  1. By using Http method post() data will be placed in the Http request body.
  2. By using Http method post() you can send an unlimited amount of data.
  3. You can send the data in a secure way.

MODEL-I Architecture

With Model 1 Architecture, One component is responsible for doing the following tasks.

  1. Controlling the request processing (Controller logic)
  2. Interacting with Database or External System(Persistence logic/Business Logic)
  3. Sending the response to the client(Presentation logic)

Consider, the requirement that same persistence Logic/ Business Logic, you need to modify all the Servlet. This is given you maintenance problem.
Consider, the requirement that same presentation logic is written in all the servlets(may be presentation logic related to a header, menu, footer, and formatting style). This gives the code duplication problem. In the future, if you want to change presentation Logic, you need to modify all the servlets. This gives you a maintenance problem.

Model-II or MVC(Model View Controler) Architecture

With Model 2 or MVC Architecture. These 3 tasks are distributed at 3 separate components called Model, View, and Controller. This clean Separation of Responsibilities avoids the code duplication and maintenance problem.

Conclusion:

In this article, we learned regarding the servlet model, RequestDispatcher, Http method, and Servlet example. In today’s web application Model-II (MVC) architecture is used to build the web application. Please, feel free to leave a comment in the below comment box, if you have any doubt regarding the topic or you want to share any information about the topic. Happy Learning!

READ NOW :- Servlet Lifecycle

Leave a Reply

Your email address will not be published. Required fields are marked *