Servlet and JSP…

Ajit Kumar Singh
5 min readDec 7, 2020

--

Servlet JSP technologies are the backbone of Java EE programming. A Web application contains an application’s resources, such as servlets, JavaServer Pages (JSPs), JSP tag libraries, and any static resources such as HTML pages and image files. In this article, I am discussing details about Servlet and JSP, Their uses, benefits, and difference.

Servlet

A servlet is a Java class that is used to extend the capabilities of servers that host applications accessed by means of a request-response model. It generates dynamic content and interacts with the client through Request and Response.

Servlet technology is robust and scalable because of the java language.

Though servlets can respond to many types of requests, they generally implement web containers for hosting any website on a web server.

Properties of Servlets :

  • Servlets work on the server-side.
  • Servlets are capable of handling complex requests obtained from the webserver.

Servlet works on HTTP i.e Hypertext Transfer Protocol. HTTP is a protocol that clients and servers on the web to communicate. HTTP is a stateless protocol that supports only one request per connection. This means that with HTTP the clients connect to the server to send one request and then disconnect. This mechanism allows more users to connect to a given server over a period of time.

The HTTP request can be made using a variety of methods, but the ones which we use widely are the GET method and the Post method.

Get method sent data in the header body which is display in the URL so it is not secured also restricted to the limited data transfer and it can be bookmarked.

Post method sent data in the request body It is completely secured and Supports a large amount of data transfer and it can not be bookmarked.

Use of Servlet

  1. Used to Create dynamic Web pages that use HTML forms to get client requests and provide HTML pages that respond to those requests.
  2. Have access to a variety of APIs and features by using servlets running in WebLogic Server. For example Session tracking, JDBC drivers, JavaBeans.

Session tracking allows a web site to track a user’s progress across multiple Web pages.

JDBC drivers provide basic database access.

Servlets can use Enterprise JavaBeans (EJB) to encapsulate sessions, data from databases, and other functionality.

3. Servlets can forward a request to another servlet or other resource.

4. Servlet support java messaging service which allows servlets to exchange messages with other servlets and Java programs.

Servlet Life Cycle

1) Start: Execution of servlet begins.

2) init(): It is called when the servlet is first loaded. This method lets you initialize the servlet.

3) Initialized void service(): The purpose of this method is to serve a request. You can call it as many times as you like.

4) Handling Request: Java application must be first determined what code is needed to execute the request URL to provide a response. To destroy the servlet Void destroy method is used at the end of the servlet life cycle.

5) destroy(): To destroy the servlet Void destroy method is used at the end of the servlet life cycle.

6) End Servlet lifecycle finishes.

7): Stop: Servlet stops executing.

Some Advantages of Servlet

  • Servlet loads only one copy of it into JVM
  • It saves time to respond to the first request which increases response time.
  • It uses a standard API that is supported by numerous web servers.
  • we can access the large set of APIs that are available for the Java platform.
  • The web container makes threads for handling more than one request to the servlet.
  • Easy communication and coordination between two or more servlet to make web applications.
  • Servlet containers support many other features like sessions, resource management, persistence, security, etc.

JavaServer Pages(JSP)

Java Server Pages is a server-side programming technology that enables the creation of a dynamic, platform-independent method for building Web-based applications it combines Java with HTML to provide dynamic content for Web pages.

JSPs are web pages coded with an extended HTML that makes it possible to embed Java code in a web page. JSPs can call custom Java classes, called a tag. It enables us to separate the dynamic content of a Web page from its presentation.

Use of JSP

  • Combine Java with HTML to provide dynamic content for Web pages.
  • Call custom Java classes, called taglib, using HTML-like tags.
  • Embed Java code directly into your HTML pages.
  • Separate the dynamic content of a Web page from its presentation.

JSP Life Cycle

  1. The first translation of the JSP page into a servlet.
  2. Compilation of JSP page into _jsp.java
  3. _jsp.java is converted to class file _jsp.class.
  4. The object of the servlet is created.
  5. _jspinit() method is invoked by the container.
  6. _jspservice() method is invoked by the container.
  7. _jspDestroy() method invoked by the container.

Some advantages of JSP

  • We can write the Java code into the HTML.
  • It enables to separation presentation layer from the business logic layer in the web application.
  • easy to show as well as process the information.
  • We can also make use of exception handling of java into JSP.
  • JSP includes the feature of multithreading of java.
  • The performance and scalability of JSP are good because JSP allows the embedding of dynamic elements in HTML pages.
  • we can easily connect with JSP with the MySQL database.

Difference between JSP And Servlet

  • Servlets run faster than JSP because JSP takes time to convert its code to the servlet.
  • JSP allows us to write java code in HTML It’s easier to code in JSP compared to servlets.
  • In JSP we can easily build custom tags that can directly call Java beans. But in the servlet, there is no custom tag writing facility.
  • Servlet is a java code while JSP is an HTML-based code that allows us to write Java code in HTML.
  • In MVC architecture, the servlet works as a controller while JSP works as a view for displaying output.
  • Servlet can accept all protocol requests, including HTTP while JSP can only accept HTTP requests.
  • Modification in the Servlet file is time-consuming due to reloading, recompiling, and restarting the server. While JSP modification is fast, as we just need to click on the refresh button.

Conclusion

In this article, I discuss the details about Servlet and JSP, their lifecycles, uses of servlet and JSP, benefits, and difference. That's it for now.

--

--