Learn JSP

JSP Tutorial

JSP stands for “JavaServer Pages” is considered as the server side technology which is used for the creation of dynamic web application and these web based applications are platform independent. JSP technology is the same as the Servlet technology and is called as the server side technology. JSP is also referred as an extension or successor of servlet as in JSP more functions are added which may include expression language etc. So every page of JSP is first converted into servlet before manipulating the request of any client. This is done by JSP container. The difference between JSP and servlet is that JSP inserts the Java code into the HTML tags whereas servlet adds the HTML tags into the java code.

In a JSP web page we have html and JSP tags. The JSP pages can be maintained easily as compared to servlet as in the JSP pages we have the facility to separate the designing and the development. In the JSP pages we have additional features also which may include expression language or custom tag etc. JSP is actually designed for the graphical user interface of certain application. Through JSP we can have the implementation of presentation layer for any application.

JSP is a server technology in which we have the java code into the HTML tags that is we use JSP to develop such type of web pages in which java code is inserted into the HTML tags. Therefore we can say that a JSP code is just similar to an HTML with an addition of the java code. An HTML file can be converted into JSP by simply changing the extension of the file from .html to.jsp. From the above discussion we can conclude that the only difference between HTML and JSP pages is that a JSP code has java code in addition to HTML tags and in an HTML page we only have the HTML tags. In a JSP page we can have both HTML and XML. This can only be possible by using the JSP actions and various commands.

 

In the end a JSP becomes a servlet:

A JSP page can be converted to servlet for the processing of the request of client as JSP is the extension of servlet. This can be done by using JSP container or web container. The JSP page is first translated to servlet then it is compiled into servlet class of java. Consider the following steps that are included while converting a JSP page into servlet:

  1. Initially we have the JSP page with jsp extension for example abc.jsp.
  2. This page that is abc.jsp is translated to java and now it has an extension called .java. This page now is abc_jsp.java.
  3. The abc_jsp.java is then compiled to java servlet class and the page becomes abc_jsp.class
  4. Now we can load abc_jsp.class as abc_jsp servlet.

In this way the JSP page is converted into servlet for the manipulation of the request of client.

 

Why JSP is preferred over servlet?

A JSP page can be a HTML page which is used for the dynamic development of web based applications. A dynamic content is like when we are including registration forms or check boxes or drop down menus in our web applications. There are a number of advantages of JSP over servlet:

  1. In a JSP page we can insert dynamic elements into the HTML and we do not have to add the HTML pages separately. And we do not need to insert additional files such as class files of java etc.
  2. In a JSP page we can have all the features of a servlet as JSP is the extension of servlet. These features may include custom tags, expression language and predefined tags in the JSP which makes the development of a JSP page easier.
  3. The execution is faster in JSP as compared to servlet.
  4. Dynamic web pages are easy to code as compared to servlet.
  5. When any change is made in a JSP page we do not need to re compile the page as these changes are be maintained or handled by the web container.
  6. We can also use the JSP pages in combination with servlet this will handle the business logic.

Advantages of JSP:

The following are some of the advantages of JSP:

  1. The JSP pages are easy to maintain and are easy to code also. The code of a JSP page is less because we have tags for example action tags, custom tags which reduces the code. Also we can have a number of objects in our JSP page.
  2. The programming language used to code a JSP page is Java. Java is a dynamic programming language which makes the JSP page portable and can be moved to other operating systems.
  3. There is no need of re compiling the JSP page if we have made changes in the page.
  4. Business logic and presentation logic can be separated.
  5. As there are dynamic elements therefore, performance of JSP pages is good.