J2EE - JSP ( Java Server Pages )

What is JavaServer Pages?

JavaServer Pages (JSP) is a technology for developing web content that support dynamic content in HTML pages. by making use of JSP tags. A JavaServer Pages element could be a form of Java servlet that’s designed to satisfy the role of a program for a Java internet application. internet developers write JSPs as text files that mix HTML or XHTML code, XML parts, and embedded JSP actions and commands. Using JSP, we are able to collect input from users through online page forms, gift records from a information or another supply, and build web content dynamically. JSP tags are often used for a range of functions, like retrieving data from a information or registering user preferences, accessing JavaBeans parts, passing management between pages and sharing data between requests, pages etc.

Why Use JSP?

JavaServer Pages usually serve a similar purpose as programs enforced victimisation the Common entree Interface (CGI). However JSP provide many blessings compared with the CGI. Performance is considerably higher as a result of JSP permits embedding Dynamic parts in HTML Pages itself rather than having a separate CGI files. JSP area unit forever compiled before it’s processed by the server not like CGI/Perl which needs the server to load AN interpreter and therefore the target script on every occasion the page is requested. JavaServer Pages area unit designed on high of the Java Servlets API, thus like Servlets, JSP conjointly has access to all or any the powerful Enterprise Java Apis, as well as JDBC, JNDI, EJB, JAXP etc. JSP pages are often employed in combination with servlets that handle the business logic, the model supported by Java servlet guide engines. Finally, JSP is AN integral a part of Java electrical engineering, a whole platform for enterprise category applications. this suggests that JSP will play an area within the simplest applications to the foremost advanced and tightened.

Advantage of JSP over Servlet

1) Extension to Servlet

JSP is the extension to servlet technology. We can use all the features of servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy.

2) Easy to maintain

JSP can be easily managed because we can easily separate our business logic with presentation logic. In servlet technology, we mix our business logic with the presentation logic.

3) Fast Development: No need to recompile and redeploy

If JSP page is modified, we don't need to recompile and redeploy the project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application.

4) Less code than Servlet

In JSP, we can use a lot of tags such as action tags, jstl, custom tags etc. that reduces the code. Moreover, we can use EL, implicit objects etc.

Life cycle of a JSP Page

The JSP pages follows these phases:

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

jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP. As depicted in the above diagram, JSP page is translated into servlet with the help of JSP translator. The JSP translator is a part of webserver that is responsible to translate the JSP page into servlet. After that Servlet page is compiled by the compiler and gets converted into the class file. Moreover, all the processes that happens in servlet is performed on JSP later like initialization, committing response to the browser and destroy.

Creating a simple JSP Page

To create the first jsp page, write some html code as given below, and save it by .jsp extension. We have save this file as test1.jsp. Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the jsp page.

Directory structure of JSP

The directory structure of JSP page is same as servlet. We contains the jsp page outside the WEB-INF folder or in any directory.

How to run a simple JSP Page ?

Create new project inside webapps
webapps --> basic-sciptlets (New Project).

Program 1:
Develop test1.jsp in webapps ---> basic-sciptlets ---> test1.jsp

Start the Server, Open the Web browser, and type "http://localhost:7070/basic-sciptlets/test.jsp"

JSP Tags:

We cannot keep java code directly in JSP. In JSP, java code can be written inside the jsp page using the scriptlet tag.

There are 3 types of scripting elements :

The scripting elements provides the ability to insert java code inside the
jsp. There are three types of scripting elements:

1. Scriptlet tag
2. Expression tag
3. Declaration tag