JSP (Java Server Pages):
JSP refers to the java server pages. Before starting discussion on jsp let us discuss the some problems of servlet.
Problems in servlet:
- In servlet presentation logic and dynamic content generation logic are intermixed which result into maintains problem.
- Every time when presentation is changed servlet needs to compile and deploy again.
- A developer needs to write HTML code to the output stream of the servlet.
JSP is the extension of servlet that provides the solution of all above problems. JSP defines a structure which is java code inside HTML code. With this structure it simplifies the web development.
Javax.servlet.jsp and its sub packages provide the classes and interfaces facilitate the development of JSP.
Interfaces in Javax.servlet.jsp package.
Classes in Javax.servlet.jsp package.
JspPage extends the Servlet interface. JSP processor-generated servlet class must implement JspPage interface.
public interface JspPage extends Servlet
Methods of JspPage interface:
1. jspInit(): It is used to perform initialization process. This method is called by the web container only once when first request comes.
Syntax:public void jspInit()
2. jspDestroy(): It is used to perform cleanup process before destroying jsp page. This method is called by the web container only once.
Syntax: public void jspDestroy()
HttpJspPage interface extends the JspPage interface.
public interface HttpJspPage extends JspPage
Methods of HttpJspPage interface:
1. _jspService(): This method is called by web container every time when a request is come for the jsp.
Syntax: public void _jspService() throws ServletException,IOException
Note: In this method underscore (_) represents that implementation of this method is provided by auto generated servlet and it can’t be overridden by developer.
How JSP page works?
- A request comes for JSP page.
- Web container translates the JSP page into a servlet.
- Compile the auto generated servlet class.
- _JspService()method is called.
- Dynamic content will be generated.
- Send dynamically generated content as a response to the client.
For first request all 6 steps will be executed but for next requests of this JSP page 2nd and 3rd step will not execute.