Updated March 14, 2023
Introduction to JSP Architecture
JSP is an acronym of Java Server Pages. So, before we learn about JSP Architecture let’s understand what is JSP all about. JSP is a technology that helps the software developers or programmers to create dynamic web pages based on HTML, XML or maybe with some other document types. For a basic understanding, we can consider that JSP is usually an HTML page or code that supports Java codes or statements too. It can also be said as JSP adds or embeds JAVA in the HTML page using JSP tags. JSP is basically used to help the developers develop the presentation layer of some enterprise level applications. It is mostly used to design websites in an improved way.
JSP was released in the year 1999 by the company called Sun Microsystems. JSP is usually similar to PHP and ASP programming languages but it uses the Java programming language.
Features of JSP
Some of the good features of JSP are:
- It supports both scripting and element-based dynamic page content.
- It allows developers to develop custom tag libraries to fulfil the application specific needs.
- We can use JSP by combining servlets that can manage business logic and model which is supported by Java servlet template engines.
- JSP pages are usually precompiled for the efficient server process.
- We can consider JSP as an integral part of J2EE. It’s a very good platform for Enterprise level applications.
- Not only HTML in the JSP program but also it allows us to embed or add some special code as well (which is called as scripting code) into HTML pages.
- In JSP, Java is the default scripting language but the JSP specification also allows other languages too such as JavaScript, VBScript, and Perl, etc.
Point to note is that, JSP can be considered as specification, not simply a product that indirectly means that the vendors can challenge with various applications or implementations which may lead to performance and quality improvement.
Examples of JSP
Following are some examples of JSP:
Example #1
Example of a simple JSP page code is written below:
Code:
<html>
<body bgcolor="green">
<% java.util.Date clock = new java.util.Date( ); %>
<% if (clock.getHours( ) < 12) { %>
<h1>Hi there, Good morning!</h1>
<% } else if (clock.getHours( ) < 18) { %>
<h1>Hi there, Good day!</h1>
<% } else { %>
<h1>Hi there, Good evening!</h1>
<% } %>
Welcome to the site, we are open 24/7.
</body>
</html>
Explanation: In the above code, the page will show different greetings messages to the users based on the local time of the day. As “Hi there, Good morning!” will be displayed is the local time of the system is before 12 PM, “Hi there, Good day!” if the time is between 12 PM to 6 PM and “Hi there, Good evening!” is the local time is after 6 PM.
Example #2
In the example, if the local time is 9 PM the page will be displayed as below:
Code:
<html>
<body bgcolor="green">
<h1>Hi there, Good evening!</h1>
Welcome to the site, we are open 24/7.
<body>
</html>
The web page of the above code will be shown as below:
Output:
JSP Architecture
Now, let’s talk about the JSP Architecture (how the JSP works). Before we proceed, let us be informed that we use Apache Tomcat server for JSP as Tomcat server has a JSP engine that is needed to process the JSP pages. Below we are discussing the flow of JSP request and response (architectural points) in an ordered manner.
- For a JSP file, the request is usually initiated by the client browser.
- Web Server (here, JSP Engine) loads the JSP file and translates the same to generate a Java Code which will be considered as Servlet.
- When the Servlet(Java Code) is generated, the JSP Engine compiles the Servlet and compilation errors are detected(if any) in this phase.
- After compilation, the Container loads the servlet class and executes it.
- After the execution, the JSP Engine sends the response back to the client.
Note: Translation and Compilation phase is processed only when
- The first request comes for the JSP file.
- The generated servlet is older than the JSP file and in this case, the JSP file is modified.
Below is a pictorial representation of JSP Architecture (request/response) for better understanding:
Below is the pictorial representation of a JSP page on how it’s served/processed through the server:
Basically, JSP (Java Server Pages) is part of a 3-tier architecture where a Server (usually referred to as Application Server or Web Server) supports the Java Server Pages (.jsp pages). This server acts as a mediator between the client system browser and a database as shown in the below pictorial diagram.
Conclusion
In the conclusion, we can say that JSP pages are basically high-level execution of the servlet that enables the programmers to embed Java codes in the HTML pages where JSP files are ultimately compiled into a servlet by the JSP Engine and that compiled servlet is used to serve the request by the Engine.
Usually, the JSP Engine checks if a servlet for a JSP file already exists and if the modified time on the JSP is older than the generated servlet. If the JSP is older then the JSP container assumes that the JSP is still unchanged and the generated servlet still matches the JSP contents. This way the process is more efficient than the other scripting languages (for eg. PHP, ASP, etc.) and hence faster. So, by the above discussion, we can say that the JSP page is just another way of writing a servlet without the need for high knowledge in Java programming where except for the translation phase or process, JSP is handled almost similar like other regular servlets.
Recommended Articles
This has been a guide to JSP Architecture. Here we discuss the introduction, Features, architecture, with examples and pictorial representation of JSP Architecture. You can also go through our other suggested articles to learn more –