logo

JSP Tutorial


Show

JSP technology is used to make web applications the same as Servlet technology did. It may be a concept as an extension to Servlet as it gives extra capability than servlet which includes expression language, JSTL, etc.

A JSP web page includes HTML tags and JSP tags. The JSP pages are less difficult to preserve than Servlet due to the fact we can separate designing and development. It gives a few extra capabilities which include Expression Language, Custom Tags, etc.

Why to Learn JSP?

JavaServer Pages frequently serve the same reasons as programs executed using the Common Gateway Interface (CGI). But JSP provides some benefits in differentiation with the CGI.

  • Performance is outstandingly one step ahead because JSP permits embedding Dynamic components in HTML Pages itself rather than having individual CGI files.
  • JSP are consistently compiled once before they are processed by the server dissimilarly CGI/Perl which needs the server to load an interpreter and the aim script every time the page is requested.
  • JavaServer Pages are constructed on top of the Java Servlets API, same as Servlets, JSP also has an approach to all the powerful Enterprise Java APIs, together with JDBC, JNDI, EJB, JAXP, etc.
  • JSP pages can be made in use in mergers with servlets that manage the business logic, the model carried on by Java servlet template engines.

Finally, JSP is an essential part of Java EE, a fully completed platform for enterprise-class applications. This indicates that JSP can play a part in the simplest applications to the most complex and demanding.

Advantages of JSP over Servlet

Some benefits of JSP over Servlet are given below check now:

1. Extension To Servlet

JSP technology is the add-on to Servlet technology. We can make use of all the attributes of the Servlet in JSP. In addition to this, we can also make use of implicit objects, already defined tags, expression language, and Custom tags in JSP, and it makes JSP development simple.

2. Can be Maintained Easily

JSP can be simply handled because we can easily distinguish our business logic from presentation logic. In Servlet technology, we combine our business logic with the presentation logic.

3. Early Development: Don’t need to recompile and redeploy

If the JSP page is changed and updated, we don't want to recompile and redeploy the project. The Servlet code wants to be upgraded and recompiled if we have to edit the look and feel of the application.

4. Not much code like Servlet

Many tags can be used in JSP such as action tags, JSTL, custom tags, etc. that bring down the code. Additionally, we can make use of EL, implicit objects, etc.

Life Cycle of JSP Page

Some Phases which follow JSP pages:

  • Translation of JSP Page
  • Compilation of JSP Page
  • Classloading (the classloader loads class file)
  • Instantiation (Object of the Generated Servlet is created).
  • Initialization ( the container invokes jspInit() method).
  • Request processing ( the container invokes _jspService() method).
  • Destroy ( the container invokes jspDestroy() method).

Note: jsplnit(), _jspService() and jspdestroy() are the lifecycle ways of JSP

As depicted withinside the above diagram, the JSP web page is translated into Servlet via way of means of the assist of JSP translator. The JSP translator is part of the net server that is chargeable for translating the JSP web page into Servlet. After that, the Servlet web page is compiled using the compiler and transformed into the magnificence file. Moreover, all of the techniques that Class in Servlet are finished on JSP later like initialization, committing reaction to the browser, and destruction.

Creating a Simple JSP page

To create the initial JSP page, you have to write down some HTML code which is given below, and save it by .jsp extension.

And then saved this file as index.jsp. Submit it in a folder and paste the folder in the web-apps directory in apache tomcat to process the JSP page.

index.jsp

JSP where we are making use of the scriptlet tag to put Java code in the JSP page we have created an example let's see. The Scriptlet tag will be learned later.

<html>  
<body>  
<% out.print(2*5); %>  
</body>  
</html>  

It will give 10 on the browser.

How to run a simple JSP Page?

Follow the below-given steps to implement this JSP page:

  • Start the server
  • Put the JSP file in a folder and deploy it on the server
  • Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example, http://localhost:8888/myapplication/index.jsp

Do you have to follow the directory structure to process a simple JSP?

No, there is no need for directory structure if you don't have class files or TLD files. For instance, submit JSP files in a folder straight forward and deploy that folder. It will start processing easily. However, if you are making use of Bean class, Servlet, or TLD file, the directory structure is needed.

The Directory structure of JSP

The directory structure of the JSP page is identical to Servlet. We hold on the JSP page exteriorly the WEB-INF folder or in any directory.