The JSP API

The JSP API

In this article, I am going to discuss the JSP API. Please read our previous article where discussed JSP Web Application Development using NetBeans IDE. As part of this article, we are going to discuss the following pointers.

  1. javax.servlet.jsp package
  2. javax.servlet.jsp.tagext
  3. The JspPage interface
  4. Methods of JspPage interface
  5. The HttpJspPage interface
  6. Method of HttpJspPage interface
  7. Classes of JSP API
  8. The Erro Data Class
  9. Methods of Erro Data Class
  10. The JspWriter Class
  11. Methods of JspWriter Class
  12. The PageContext Class
  13. Methods of PageContext Class

The JSP API consists of two packages:

  1. javax.servlet.jsp
  2. javax.servlet.jsp.tagext
Package javax.servlet.jsp

The javax.servlet.jsp package contains a number of classes and interfaces. The classes and interfaces in javax.servlet.jsp package describes and defines the contracts between the runtime environment provided for an instance of such a class by a conforming JSP container and a JSP page implementation class.

Interfaces of package javax.servlet.jsp

The interfaces provided by javax.servlet.jsp package is as follows:

  1. JspPage
  2. HttpJspPage
JspPage:

The class implementing the JspPage interface is responsible for invoking the below methods at the appropriate time based and also describes the generic interaction that a JSP page implementation class must satisfy; pages that use the HTTP protocol are described by the HttpJspPage interface. According to the JSP specification, all the generated servlet classes must implement the JspPage interface. It extends the Servlet interface. It provides two life cycle methods.

The JSP API

Following are the methods of JspPage:

  1. jspDestroy(): It is invoked when the JSP page is about to be destroyed. It is basically used to perform some clean-up operations.
  2. jspInit(): It is invoked when the JSP page is initialized. It is invoked only once during the life cycle of the JSP when the JSP page is requested first. It is the same as the init() method of the Servlet interface.
HttpJspPage:

The HttpJspPage is implemented by the generated JSP page implementation classes when HTTP is used and also provides the interaction that a JSP page implementation class must satisfy when using the HTTP protocol. Following are the methods of HttpJspPage:

  1. _jspService(): The _jspService() method corresponds to the body of the JSP and is defined automatically by the JSP container. The superclass may choose to perform some actions in its service() method before or after calling the _jspService() method if a superclass is specified using the extends attribute.
Classes of package javax.servlet.jsp

The classes provided by javax.servlet.jsp package is as follows:

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

The actions and template data in a JSP are written using the JspWriter object which is actually referenced by the implicit variable out which is initialized automatically using methods in the PageContext object. Following are the methods of JSPWriter class:

  1. clear(): It clears the contents of the buffer.
  2. clearBuffer(): it clears the current contents of the buffer.
  3. close(): It closes the stream, flushing it first.
  4. flush(): It flushes the stream.
  5. getBufferSize(): It returns the size of the buffer used by the JspWriter.
  6. getRemaining(): It returns the number of unused bytes in the buffer.
  7. isAutoFlush(): It indicated whether the JspWriter is autoFlushing.
  8. newLine(): It helps us rite a line separator.
PageContext:

The PageContext class extends JspContext to provide useful context information for when JSP technology is used in a Servlet environment. Following are the methods of PageContext class:

  1. forward(): It is used to re-direct or forward the current ServletRequest and ServletResponse to another active component in the application.
  2. getErrorData(): It provides convenient access to error information.
  3. getException(): It returns the current value of the exception object.
  4. getPage(): It returns the current value of the page object.
  5. getRequest(): It returns the current value of the request object.
  6. getResponse(): It returns the current value of the response object.
  7. getServletConfig(): It returns the ServletConfig instance.
  8. getServletContext(): It returns the ServletContext instance.
  9. getSession(): It returns the current value of the session object.
  10. handlePageException(): It is used to process an unhandled ‘page’ level exception by forwarding the exception to the specified error page for the JSP.
  11. include(): It processes the specified resource as part of the current ServletRequest and ServletResponse being processed by the calling Thread.
  12. initialize(): It is called to initialize an uninitialized PageContext so that it may be used by a JSP Implementation class to service an incoming request and response within it’s _jspServlet() method.
  13. pushBody(): It returns a new BodyContent object, saves the current “out” JspWriter, and updates the value of the “out” attribute in the page scope attribute.
  14. release(): It is used to “reset” the internal state of a PageContext, releasing all internal references and preparing the PageContext for potential reuse.
ErrorData:

It contains information about an error, for error pages. Following are the methods of ErrorData class:

  1. getRequestURL(): It returns the request URL.
  2. getServletName(): It returns the name of the servlet invoked.
  3. getStatusCode(): It returns the status code of the error.
  4. getThrowable(): It returns the Throwable that caused the error.
Package javax.servlet.jsp.tagext

It provides a portable mechanism for the description of tag libraries. It contains a tag library descriptor and a number of Tag Files or Tag Handler classes defining request-time behavior. It also contains additional classes and resources used at runtime and possibly some additional classes to provide extra translation information.

Interfaces of package javax.servlet.jsp.tagext
  1. BodyTag: It extends the iteration tag by defining additional methods that let a tag handler manipulate the content of evaluating its body.
  2. DynamicAttributes: Implement this interface for a tag to declare that it accepts dynamic attributes.
  3. IterationTag: It extends the tag by defining one additional method that controls the reevaluation of its body.
  4. JspTag: It basically serves as a base class for Tag and Simple Tag.
  5. SimpleTag: It is used for defining Simple Tag Handlers.
  6. Tag: It is a classic tag handler that does not want to manipulate its body.
  7. TryCatchFinally: It is an auxiliary interface of a Tag, Iteration tag, or Body tag handler that wants additional hooks for managing resources.
Classes of package javax.servlet.jsp.tagext
  1. BodyContent: It is available to a tag handler because it is an encapsulation of the evaluation of the body of action.
  2. BodyTagSupport: It is a base class for defining tag handlers implementing BodyTag.
  3. FunctionInfo: It provides information for a function in a Tag Library.
  4. JspFragment: It is used to encapsulate a portion of JSP code in an object that can be invoked as many times as needed.
  5. PageData: It provides translation-time information on a JSP page.
  6. SimpleTagSupport: It is a base class for defining tag handlers implementing SimpleTag.
  7. TagAdapter: It is using a Tag interface to wraps any SimpleTag and exposes it.
  8. TagAttributeInfo: It provides information on the attributes of Tag available at translation time.
  9. TagData: It represents the attribute/value information for a tag instance.
  10. TagExtraInfo: It is an optional class used to describe translation-time information not described in the TLD.
  11. TagFileInfo: It is instantiated from the TLD which provides information for a tag file and is available only at translation time.
  12. TagInfo: It is instantiated from the TLD which provides information for a tag and is available only at translation time.
  13. TagLibraryInfo: It provides translation-time information associated with a taglib directive and its underlying TLD file.
  14. TagLibraryValidator: It is a translation-time validator class for a JSP page.
  15. TagSupport: It provides a base class for defining new tag handlers implementing Tag.
  16. TagVariableInfo: It is instantiated from the TLD which provides variable information for a tag and is available only at translation time.
  17. ValidationMessage: It provides a validation message from either TagLibraryValidator or tagExtraInfo.
  18. VariableInfo: It provides information on the scripting variables that are created or modified by a tag at run-time.

In the next article, I am going to discuss the JSP – Architecture. Here, in this article, I try to explain the JSP API. I hope you enjoy this JSP API article.

Leave a Reply

Your email address will not be published. Required fields are marked *