Back to: JSP Tutorials for Beginners and Professionals
Introduction to Java Server Pages (JSP)
In this article, I am going to give you a brief introduction to JSP (Java Server Pages). As part of this article, we are going to discuss the following pointers in detail.
- Overview of JSP.
- What is Java Server Pages?
- Problem with Servlet
- Why JSP pages are more advantageous than Servlet?
- Why Use JSP?
- Advantages and Disadvantages of JSP
- Features of JSP
- Creating a simple JSP Page
- Process of Execution
- How to run a simple JSP Page?
- Do I need to follow the directory structure to run a simple JSP?
- The Directory structure of JSP
- The architecture of a JSP Application
- What is a JSP container?
Overview of JSP (Java Server Pages):
The main purpose of the online applications in the Enterprise applications area is to get dynamic responses from the server machine. To design web applications on the server-side we may use Web Technologies like CGI, Servlets, JSP then on.
CGI is essentially a Process-based technology because it had been designed on the idea of C technology. If we deploy any CGI application at the server then for each request CGI container will generate a separate process. In the above context, if we send multiple numbers of requests to an equivalent CGI application then the CGI container has got to generate multiple numbers of processes at the server machine. To handle the multiple numbers of processes at a time server machine has got to consume a greater number of system resources, as a result, the performance of the server-side application will be reduced.
To overcome the above problems, we have to use Thread based technology at the server-side like servlets. In web application development, servlets are excellent at the time of devour the request and process the request but servlets aren’t good at the time of generating the dynamic response to the client.
Servlet may be a Thread based technology, if we deploy it at the server then the container will create a separate thread rather than the method for each request from the client. Due to this Thread based technology at the server-side, server-side application performance will be increased. In the case of the servlet, we are unable to separate both presentation logic and business logic. If we perform any modifications on servlets then we must perform recompilation and reloading. If we would like to style web applications by using servlets then we must require excellent knowledge of Java technology.
JSP may be a server-side technology provided by Sun Microsystems to style web applications so as to get a dynamic response. The main intention to introduce JSP technology is to scale back java code the maximum amount as possible in web applications. JSP technology may be a server-side technology, it had been designed on the idea of Servlet API and Java API. In web application development, we’ll utilize JSP technology to organize the view part or presentation part. JSP technology is extremely good at the time of generating a dynamic response to the client with an excellent look and feel. If we would like to style any web application with JSP technology then it’s not required to possess java knowledge. In the case of JSP technology, we are ready to separate presentation logic and business logic because to organize presentation logic we’ll use HTML tags, and to organize business logic we will use JSP tags separately. If we perform any modifications on JSP pages then it is not required to perform recompilation and reloading because JSP pages are auto-compiled and auto-loaded.
What is JSP?
The Java Server Pages 1.2 specification provides web developers with a framework to create applications containing dynamic web pages like HTML, DHTML, XHTML, and XML. A JSP page may be a text-based document containing static HTML and dynamic actions that describe the way to process a response to the client in a more powerful and versatile manner. Most of a JSP file is obvious HTML but it also has, interspersed with it, special JSP tags.
The web application is a collection of web resources programs having the capability to generate web pages. To develop these web resources programs, we need web technology. For every request given to the CGI program, one process is heavyweight consume more resources. Due to this CGI applications performance is very poor. For every request given to servlet / JSP pages, one thread will be created. These threads are lightweight to consume fewer resources. Due to this servlet / JSP application gives a very good performance.
JSP stands for Java Server Pages. JSP technology builds on Java. JSP or Java Server Pages was developed by Sun Microsystem. JSP technology is an object-oriented programming language and is predicated on Java Language. JSP is a part of JEE. JSP is a web technology used for developing dynamic websites. JSP is an API introduced by SUN Microsystems which is also used to develop web-based apps. JSP files can be located inside the project folder. The extension name of the JSP file should be .jsp. We no need to configure the JSP file inside the web.xml. We can directly use the JSP file name as a URL in the address bar. We have many advantages compared to servlets. Servlets take many steps to develop where JSP takes fewer steps to save the development time. JSP will divide our project into presentation logic and business logic. JSP will always contain presentation logic where servlets will contain business logic.
Problems with Servlet
In the initial days of Servlets, Sun Microsystems did not attract ASP programmers towards servlet because ASP supports tag-based programming that servlet doesn’t support. To work with Servlet strong knowledge is required. It is very difficult for an ASP programmer to learn Java.
To overcome the above problems Sun Microsystems has given a tag-based technology called JSP having all the features of Servlet. So, any programmer can use JSP without having strong knowledge of Java/Servlet. Below are the limitations of Servlet:
- It is not suitable for non-java programmers as strong knowledge in Java is required to work with Servlets.
- Placing HTML code in the Servlet program is a complex and error-prone process.
- Makes the programmer mix up presentation logic (HTML) and business logic (Java code).
- Any modification in the Servlet program will be reflecting only after recompilation of the Servlet program overloading of Web Application.
- Doesn’t gives implicit object request, response, ServletContext and etc. are not implicit object, they are container created object. So, we need to write additional code to access those objects.
- ServletConfig in web.xml is mandatory.
- Servlet is not good for business logic not good for persistence logic.
Why JSP pages are more advantageous than Servlet?
To overcome the problems of Servlets Sun Microsystems has given a tag-based technology called JSP having all the features of Servlet. So, any programmer can use JSP without having strong knowledge of Java/Servlet.
- It supports tag-based programming.
- It gives nine implicit objects.
- It allows us to write separate both business logic and presentation logic.
- Modifications done in the JSP program will be reflected automatically without reloading the web application.
- Strong Java knowledge is not required.
- Suitable for both java and non-java programmers.
- Configuration of JSP in web.xml file is optional.
- Allows us to use all the features of servlet because every JSP program internally and equivalent servlet program will be generated.
- Gives automatic exception handling supports.
- Every JSP program and equivalent servlet program will be generated internally using the page compilation process. Every server provides one JSP page compiler in the JSP container.
- Whenever the JSP program is executing it means JSP equivalent servlet is executing internally.
Note: In the initial day’s programmer have to use JSP as a complete alternate to Servlet. But now a day’s programmers are using both servlet and JSP technology together in web application development.
Advantages of JSP
- JSP pages are faster because they are pre-compiled.
- It is flexibles as it’s part of the Sun’s J2EE and can be used with other types of Java technologies, such as Servlet.
- It is a ‘fly by night’ technology as multiple vendors support it because of its specifications.
- It is easy to develop because HTML can be easily maintained with JSP. Relatively minimal programming skills required.
- JSP page code is not visible to the client, only generated HTML is visible.
- It is easy to read, write, and maintain JSP pages.
- It is easy to extend the JSP language by using pluggable, reusable tag libraries available.
Disadvantages of JSP
- It is difficult to trace errors that occurred in JSP pages because JSP pages are translated to servlets and compiled.
- It requires double the disk space to hold JSP pages.
- As JSP pages are to be compiled on the server it requires more time when accessed for the first time.
- The initial compilation produces a noticeable delay when accessing the JSP page for the first time.
- It is difficult to debug the JSP page.
JSP technology was designed to simplify the process of creating pages by separating web presentations from web content. JSP is a collection of HTML tags and JSP tags and Java code. Every JSP page can be placed in the root folder or WEB-INF. The JSP placed in the root folder is a public resource and this resource can access by the client directly. The JSP placed inside WEB-INF is a private resource, which can’t access directly by the client. Client access this resource using public URL defined inside web.xml.
<web-app> <servlet> <servlet-name>instanceName</servlet-name> <jsp-file>jspfilename</jsp-file> <init-param> <param-name>name</param-name> <param-value>value</param-value> </init-param> . . . </servlet> <servlet-mapping> <servlet-name>instancename</servlet-name> <url-pattern>/publicurl</url-pattern> </servlet-mapping> </web-app>
Features of JSP
- Simplifies the process of Development: It allows the programmer to insert the Java code directly into the JSP file, making the development process easier. Although JSP files are HTML files, they use special tags containing the java source code which provides a dynamic ability.
- Portability: The java feature of “write once, run anywhere” is applicable to JSP. JSP is platform-independent making it portable across any platform and therefore multi-platform.
- Independency of Layers: There is a clear separation between the presentation and implementation layer.
Creating a Simple JSP page
To create your first JSP page, write HTML code, and save it by the .jsp extension. Put the code in a folder and paste it into the web-apps directory in apache tomcat to run the JSP page.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% out.print(2 * 5); %> </body> </html>
How to run a simple JSP page?
Follow the below steps to execute the JSP page:
- Start the server
- Put the JSP file (index.jsp) in a folder and deploy it on the server.
- Visit the browser by the URL: http://localhost:8015/Demo/index.jsp
Process of Execution
To process a JSP file, we’d like a JSP engine that will be connected to an internet server or are often accommodated inside an internet server. Firstly when an internet browser seeks a JSP file through an URL from the online server, the online server recognizes the .jsp file extension within the URL requested by the browser and understands that the requested resource may be a Java Server Page. Then the online server passes the request to the JSP engine. Then the JSP page is translated to a Java class, which is again compiled into a servlet.
This translation and compilation phase occurs only when the JSP file is requested for the primary time. Now for every request, the request directly goes to the servlet byte code, which is already in memory. Thus when an invitation comes for a servlet, an init() method is named when the Servlet is first loaded into the virtual machine, to perform any global initialization that every request of the servlet will need. Then each request is sent to a service() method, where the response is put together. The servlet creates a replacement thread to run the service() method for every request. The request from the browser is converted into a Java object of type HttpservletRequest, which is passed to the Servlet alongside an HttpservletResponse object that’s wont to send the response back to the browser. The servlet code always performs the operations specified by the JSP elements within the .jsp file.
Do I need to follow the directory structure to run a simple JSP?
No, it is not required to follow the directory structure to run a simple JSP file if you don’t have class files or TLD files. For example, simply put JSP files in a folder and deploy that folder directly, it will be working fine. But if you are using Bean class, Servlet, or TLD file, then the directory structure is mandatory.
We can contain the JSP pages outside the WEB-INF folder or in any directory. The directory structure of the JSP page is exactly the same as Servlet.
What is JSP Container?
JSP container is a program that provides an implementation for JSP API which is responsible for running the Servlet. If we want to develop any web-based application, we have to use a servlet container, JSP container, HTTP server. Nowadays all these things are integrated into any JEE server.
JEE servers are available in the following two types:
1. Web Servers:
If any server contains only HttpServer, JSP container, servlet container we can call it web servers. Web Servers are used to only develop web-based applications but we cannot develop any EJB applications. Example: tomcat, resin, jws, etc…
2. Application Servers:
If any server contains HttpServer, JSP container, servlet container EJB applications, directory servers, etc. we can call it application servers. Simply we can say application servers completely implementing JEE API where web servers are partially implementing JEE API. Example: WebLogic, WebSphere, JBOSS, etc.
How does JSP and JSP containers function?
A JSP page is executed during a JSP container or a JSP engine, which is installed during a web server or during an application server. When a client asks for a JSP page the engine wraps up the request and delivers it to the JSP page alongside a response object. The JSP page processes the request and modifies the response object to include communication with the client. On getting the response, the container or the engine wraps up the responses from the JSP page and delivers them to the client. The underlying layer for a JSP is really a servlet implementation. The abstractions of the request and response are equivalent because of the ServletRequest and ServletResponse respectively. The corresponding objects are HttpservletRequest and HttpServletresponse if the protocol used here is HTTP.
The first time the engine intercepts an invitation for a JSP, it compiles this translation unit (the JSP page and other dependent files) into a category file that implements the servlet protocol. If the dependent files are other JSP’s they’re compiled into their own classes. The servlet class generated at the top of the interpretation process must extend a superclass that’s either –
- Specified the use of the extends attribute in the page directive, or
- Is a JSP container that implements javax.servlet.jsp.JspPage interface and provides some basic page-specific behavior.
Since most JSP pages use HTTP, their implementation classes must actually implement the javax.servlet.jsp.HttpJspPage interface, which may be a sub interface of javax.servlet.jsp.JspPage.
The javax.servlet.jsp.JspPage interface contains two methods:
- public void jspInit(): It is invoked when the JSP is initialized and the page authors are free to provide initialization of the JSP.
- public void jspDestroy(): It is invoked when the JSP is about to be destroyed by the container.
The javax.servlet.jsp.HttpJspPage interface contains one method:
- public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException: This method generated by the JSP container is invoked, whenever an invitation involves the JSP. The request is processed and therefore the JSP generates an appropriate response. This response is then taken by the container and passed it back to the client.
In the next article, I am going to discuss the JSP Environment Setup. Here, in this article, I try to give a brief introduction to JSP. I hope you enjoy this Introduction to JSP (Java Server Pages) article.