Servlet and JSP (Java Server Page) are the programs that run on java-enabled web servers or application servers. Though both of them extend the functionalities of the web server or application server but, they do differ in many ways. Servlets are simple java programs that developers design to handle the request from the web browser and generate a dynamic response for the same. On the other hand, JSP is a successor of the servlet, as it is also used to generate a dynamic response for the request generated by the web browser.
So, what’s the difference? The basic difference from the coding perspective is that you encapsulate HTML code inside java code in servlet. On the contrary, in JSP, you encapsulate java code inside the HTML.
Servlets are more complex to write as compared to JSP. But servlets execute faster than JSP. Although Servlets and JSP are quite complex technicalities of java, we will try to explore both of them, and finally, we will identify the differences between them.
Content: Servlet and JSP
Comparison Chart
Basis for Comparison | Servlet | JSP |
---|---|---|
Basic | Servlet is a Java code | JSP is HTML-based code |
Execution | Servlets are directly executed on the web server | JSP is first translated to Servlets and then executed on the web server |
Execution Speed | Servlets execute faster | JSP executes comparatively slower |
Code | Servlets are difficult to code | JSPs are easy to code |
Handling Request | Accepts all protocol requests | Accepts HTTP requests only |
Packages | The developers can import packages only at the top of the servlet program | The developers can import packages anywhere in the program, i.e. top, bottom or end |
MVC Architecture | In MVC architecture, servlets paly the role of controller | In MVC architecture, servlets play the role of view for displaying output |
Usefulness | Servlets are useful when there is more data processing | JSPs are useful when there is less or no data processing |
Custom Tags | Servlets do not allow us to create custom tags. | JSP allows us to create custom tags with the use of JSP API |
Life Cycle Method | init(), service(), and destroy() | jspInit(), _jspService(), jspDestroy() |
Override | In servlets, you can override the service() method | In JSP, you can not override the service() method |
Session Management | In servlets, the session management is, by default, disabled. It is the responsibility of the JS user to enable it during the session | In JSPs, session management is enabled explicitly |
Business and Presentation Logic | In Servlet, you have to encode both business logic and presentation logic in a single servlet file | In JSP, you can distinguish between business logic and presentation logic with the help of JavaBeans |
Modification | Implementing modification in servlets is a somewhat time-consuming process | Implementation modification is less time consuming |
Javascript on the client side | In servlet, there is no method to execute Javascript on the client side | In JSP, Javascript can be executed on client-side using client validation |
What is Servlet?
Servlet is a pure java program deployed on the server side of a web connection. Servlets accept the request from the client, i.e. the web browser process it and generate a dynamic web page as a response to the client request. Let us try to understand Servlet with the help of the code.
import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Test extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType(“text/html”); Out.println(“<html>”); Out.println(“<body>”); Out.println(“<h1>”); Out.println(“Welcome to Server-side scripting:” +n); Out.println(“</h1>”); Out.println(“</body>”); Out.println(“</html>”); Out.close(); } }
The servlets execute on a java-based server such as Apache Tomcat or Glassfish. The execution of the above servlet will result in the web page shown below:
If we consider the MVC architecture that divides any application into three logical components model, view and controller, then the servlets work as controllers in it.
Servlets significantly perform better than applets and CGI. They are platform-independent as they are written in java. The servlets are secure as the java security manager is present on the server and secures all the resources on the server. In fact, the servlets are free to communicate with applets, databases and other software.
Life Cycle of Servlet
The life cycle of a servlet involves three methods init(), service(), and destroy(). Let us understand the life cycle of a servlet with a scenario and see where the server invokes these methods in the life cycle of the servlet.
- A user enters a URL into a web browser for which it generates an HTTP request to the appropriate server.
- On receiving the request, the web server maps this request to a particular servlet.
- The servlet is identified and loaded to address the space of the server.
- The web server then invokes the servlet’s init() method to initialize some of the elements of the servlet before serving any request. The server invokes the init() method only once during the life cycle of a servlet.
- After initializing the servlets, the server invokes the service() method of the servlet. The service() method processes the HTTP request and generates a corresponding HTTP response. The server invokes the service() method to process every HTTP request.
- When the servlet is no longer needed, the server invokes destroy() method to release all the resources engaged by the servlet.
What is JSP?
Java server pages (JSP) is a server-side scripting language. JSP generates a dynamic webpage as a response to the request from a web browser. The web page that JSP generates incorporates two types of content static content, i.e. the HTML code and dynamic content, i.e. the result of Java code embedded in JSP. Although a JSP file includes HTML and Java, the file is saved by the “.jsp” extension. Let us understand JSP with the help of JSP page shown below:
<%@page ContentType = “text/html” pageEncoding = “UTF-8”%> <%@page import = “java.io.*, java.until.*” %> <html> <head> <title> JSP Page </title> </head> <body> <h1> <% Out.println(“Welcome to Server-side scripting:” +n); %> </h1> </body> </html>
The JSP page above will result in the web page shown below. The HTML code in JSP takes care of the presentation of the webpage generated, whereas the java code in JSP looks for the business logic. So, in MVC architecture, JSP works as a view that displays the output generated.
The JSP file is always deployed on the server side and can be executed only on the java enabled servers such as Apache Tomcat or Glassfish.
Life Cycle of JSP
The life cycle of JSP represents various stages of JSP, right from its creation to its destruction. The life cycle of JSP is the same as that of a servlet with the addition of one or more phases. We can divide the life cycle of JSP into two phases:
- Translation and Compilation Phase
- Execution Phase
Translation and Compilation Phase
Like a servlet, the JSP page cannot directly respond to a client’s (web browser) request. Initially, the server has to translate the JSP page to an equivalent servlet with the help of a JSP translator.
- The server parses the JPS page to the XML view even before the translation starts.
- The XML view is validated tag by tag to identify if any syntactic error is present. If an error occurs, the server suspends the translation until the corrections are made.
- Once errors are corrected, the translation starts.
- When the JSP page is requested for the first time.
- To implement any kind of modification.
- If an equivalent servlet file/class file of that JSP page gets deleted for any reason.
So, what is the process of translating JSP in an equivalent servlet?
Consider that we have to translate a Test.jsp file. Let’s look into the translation steps:
- Initially, the JSP translator translates Test.jsp file into an equivalent servlet Test_jsp.java.
- Now the Test_jsp.java file is compiled to generate a class file Test_jsp.class. However, the class file is only generated when the JSP page is translated for the first time or to accommodate modifications, if any are made.
- The generated class file is loaded into the web container, and the container produces it whenever any client requests for the same JSP page.
- The web container creates an instance or object of the class file Test_jsp.class.
Execution Phase
- Once the instance is created, the jspInit() method is invoked to initialize a few elements before servicing any request. The jspInit() method is invoked only once during the life cycle of the JSP page. The jspInit() method can be overridden by the page author during the development of the JSP page.
- After initialization, the client request is served using _JspService() method; the service method is invoked frequently for every request of the same JSP page. The underscore in _JspService() method indicates that this method cannot be overridden.
- The web container invokes the jspDestroy() method when the servlet instance is not required anymore. Just to end the life cycle of the concerned JSP page.
Key Differences Between Servlets and JSP
- Servlet is a java program. However, JSP is an HTML-based code.
- Servlets are directly executed on the web server as soon as the request from the web browser arrives. On the other hand, to respond to the request arriving from the web browser JSP file is first translated to the servlet, and then it executes on the webserver to generate the required response.
- As servlets directly execute on a web server, so they are faster. On the contrary, the JSP requires translation before the execution they are comparatively slower.
- Servlets are quite complex to code, as java instructions encapsulate the HTML code several times. However, the JSP are easy to code as there is a kind of separation between HTML code and java code.
- Servlets are capable of handling all types of protocol requests, including HTTP. On the other hand, JSP handles HTTP requests only.
- If we talk about servlets, the packages must be imported at the top of the servlet program. However, in the case of JSP, the packages can be imported at any moment in the program, i.e. top, bottom, and end.
- In MVC architecture (we can divide an application into three main logical components model, view and controller), servlets play the role of controller, whereas JSP plays the role of view to display the output.
- Servlets are useful when there is more data processing. On the contrary, JSPs are useful when there is less or no data processing.
- Servlets do not allow us to create custom tags, whereas, with JSP, we can create custom tags using JSP API.
- The life cycle methods of servlets are init(), service(), and destroy(). On the other hand, the lifecycle methods of JSP are jspInit(), _jspService(), jspDestroy().
- In servlets, a developer can override the service method, whereas in JSP developer can not override the service() method.
- In servlets, the session management is, by default, disabled, and it is the responsibility of the JS user to enable it during the session. However, in JSP, session management is, by default, enabled.
- In servlets, there is no means to separate business and presentation logic. On the contrary, JSP separates business and presentation logic using JavaBeans.
- Implementing modification in servlets is quite time-consuming because it involves reloading, recompiling and restarting the server. On the other hand, implementing modification in JSP is not time-consuming as to see the modification, the user just has to click on the refresh button of the browser.
- In servlet, there is no method to execute the Javascript on the client side. However, Javascript can be executed on the client side in JSP using client validation.
Conclusion
So this is all about servlets, JSP and the differences between them.
Leave a Reply