JSP Tutorial

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:

  1. In servlet presentation logic and dynamic content generation logic are intermixed which result into maintains problem.
  2. Every time when presentation is changed servlet needs to compile and deploy again.
  3. A developer needs to write HTML code to the output stream of the servlet.

JSP:

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 package.

Javax.servlet.jsp and its sub packages provide the classes and interfaces facilitate the development of JSP.

Interfaces in Javax.servlet.jsp package.

  1. JspPage.
  2. HttpJspPage.

Classes in Javax.servlet.jsp package.

  1. JspFactory.
  2. PageContext.
  3. JspWriter.
  4. JspEngineInfo.
  5. JspException.
  6. JspError.

JspPage interface:

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:

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?

  1. A request comes for JSP page.
  2. Web container translates the JSP page into a servlet.
  3. Compile the auto generated servlet class.
  4. _JspService()method is called.
  5. Dynamic content will be generated.
  6. 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.   

JSP Basic tutorial:

JSTL (JSP Standard Tag Library) tutorial:

Custom tags tutorial:

Maven eclipse JSP:

JSP Interview Questions: