Java Server Pages(JSP)
JSP full form of JSP is Java Server Pages, which is a server-side programming language. It is used to create web -applications similar to servlet technology with some more functionalities.
- It is used to create dynamic web content.
- In this, Java code can be inserted in HTML/ XML pages or both.
- JSP has access to the entire API of JAVA.
There are many advantages of JSP as compared to Servlet:
- Extension to Servlet: we can use all the features of the servlet as well as many extra features of JSP like predefined tags, expression language, custom tags, etc.
- Easy to Maintain: It is easy to maintain because we can separate design and development. In servlet, both are in mixed.
- No need to recompile and redeploy: If the JSP page is modified we don't need to recompile and redeploy but The Servlet code needs to be updated and recompiled if we have to change the look and feel of the application.
- Less code than servlet: We can use many tags to reduce code.
The disadvantage of JSP:
- Difficult to debug for errors.
- Its output is HTML which lacks features.
Difference between Servlet and JSP:
The lifestyle of 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).
jsplnit(), _jspService() and jspDestroy() are the life cycle methods of jsp.
As depicted in the above diagram, the JSP page is translated into Servlet with the help of a JSP translator. The JSP translator is a part of the webserver which is responsible for translating the JSP page into Servlet, then the Servlet page is compiled by the compiler and gets converted into the class file.
How to create a JSP page:
First, create the HTML page and save it with it .jsp. Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the JSP page.
How to run the 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/jspproject/index.jsp
In JSP there are many different tags available so that code can be reduced:
- Declaration tag: It is used to declare variables.
Syntax:
<%! Dec var %>
Example :
<%! int variable=10 %>
2. Java Scriplets: It allows us to add any number of JAVA code, variables, and expressions.
Syntax:
<%! java code %>
3. JSP Expression: It evaluates and converts the expression to a string.
Syntax:
<%= expression %>
4. Java Comment: It is used for the comment the code.
<% — — — JSP comment %>
The JSP API consists of two packages:
- javax.servlet.jsp: It consists of 2 interfaces and many different classes.
Interfaces are:
- JspPage
- HttpJspPage
Classes are:
- JspWriter
- PageContext
- JspFactory
- JspEngineInfo
- JspException
- JspError
2. javax.servlet.jsp.tagext