
Java Server Pages (JSP) enables us to create a dynamic and platform independent method for building web based applications. JSP is a server-side programming technology and is an extension of Servlet.
It separates the view from the application logic and promotes a reusable-component model of programming.
The main purpose of JSP is to reduce the Java code in web applications as much as possible.
We can use JSP to design web application and it acts as a view part in MVC framework like Spring, and it shows the data on web browser.
Table of Contents
Advantages of JSP
There are following advantages of JSP
- It is an extension of Servlet so it has more feature than servlet.
- It is easy to do web design using JSP because it does not require much knowledge about Java.
- We can generate dynamic contents by using JSP.
- If you do some modification in JSP page then you do not need to compile every time hence development is fast.
- Maintenance is easy as we need to do minimum code as compared to Servlet
Lifecycle of Java Server Pages (JSP)
When you send request from client/web browser for JSP page then JSP container picks up the request, identifies the JSP page and performs life cycle actions.
- JSP Translation
- JSP Compilation
- Class Loading (The class loader loads class file)
- Instantiation (Object of generated servlet is created)
- Initialization (jspInit() invocation)
- Request Processing (_jspService() invocation)
- Destroy (jspDestroy() invocation)
Difference between JSP and Servlet in Java
As you can use both JSP and Servlet in web application but there are many differences between them which are as follows:
# | JSP | Servlet |
1 | JSP is a webpage scripting language. | Servlets are Java programs that are already compiled. |
2 | JSP is slower than Servlets because it passes through JSP life-cycle. | As servlet has a short response time so servlet is faster than JSP. |
3 | JSPs are easier to code, as here Java is coded in HTML. | Servlets are harder to code, as here we write HTML codes in Java. |
4 | JSP only accept HTTP requests. | Servlet can accept all protocol requests. |
5 | You can not override service() method in JSP. | But you can override service() method in Servlets. |
6 | Session management in JSP is automatically enabled. | But in Servlet, you have to enable it explicitly. |
7 | In JSP, you can import packages anywhere in the file. | But in servlets, you need to import all the packages at the top of the Servlets. |
8 | Easy and faster modifications in JSPs because you just need to refresh the pages. | But you need to reload, recompile, and then restart the servers hence modifications are time-consuming and challenging task. |
9 | JSP is used for presentation purpose. | But servlet is used for business processing. |
Difference between HTML and JSP(Java Server Pages)
You can use both JSP and HTML to build web applications. Both are scripting languages, JSP is a server-side scripting language while HTML is a client-side scripting language.
# | JSP | HTML |
1 | JSP is a server side programming technology. | HTML is a client side scripting language. |
2 | We use JSP to generate dynamic web page. | But we use HTML to generate static web page. |
3 | JSP is given by Sun Micro System. | HTML was created by Tim Berners-Lee. |
4 | Need JSP container to execute JSP code. | But you need HTML Interpreter to execute html code. |
5 | JSP is slower to load as it runs on server (Apache tomcat, glassfish). | HTML is fast as it runs on client (browser). |
6 | A JSP page may consist of HTML tags and JSP tags. | But HTML can only have html tags. |
7 | It allows to place custom tag or third party tag. | But it does not allow to place custom tag or third party tag. |
Finally, you got the basic knowledge about JSP and lifecycle of JSP and most importantly using this article you have learned difference between JSP and Servlet in Java.