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.
- javax.servlet.jsp package
- The JspPage interface
- Methods of JspPage interface
- The HttpJspPage interface
- Method of HttpJspPage interface
- Classes of JSP API
- The Erro Data Class
- Methods of Erro Data Class
- The JspWriter Class
- Methods of JspWriter Class
- The PageContext Class
- Methods of PageContext Class
The JSP API consists of two packages:
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:
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.
Following are the methods of JspPage:
- jspDestroy(): It is invoked when the JSP page is about to be destroyed. It is basically used to perform some clean-up operations.
- 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.
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:
- _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:
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:
- clear(): It clears the contents of the buffer.
- clearBuffer(): it clears the current contents of the buffer.
- close(): It closes the stream, flushing it first.
- flush(): It flushes the stream.
- getBufferSize(): It returns the size of the buffer used by the JspWriter.
- getRemaining(): It returns the number of unused bytes in the buffer.
- isAutoFlush(): It indicated whether the JspWriter is autoFlushing.
- newLine(): It helps us rite a line separator.
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:
- forward(): It is used to re-direct or forward the current ServletRequest and ServletResponse to another active component in the application.
- getErrorData(): It provides convenient access to error information.
- getException(): It returns the current value of the exception object.
- getPage(): It returns the current value of the page object.
- getRequest(): It returns the current value of the request object.
- getResponse(): It returns the current value of the response object.
- getServletConfig(): It returns the ServletConfig instance.
- getServletContext(): It returns the ServletContext instance.
- getSession(): It returns the current value of the session object.
- handlePageException(): It is used to process an unhandled ‘page’ level exception by forwarding the exception to the specified error page for the JSP.
- include(): It processes the specified resource as part of the current ServletRequest and ServletResponse being processed by the calling Thread.
- 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.
- 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.
- release(): It is used to “reset” the internal state of a PageContext, releasing all internal references and preparing the PageContext for potential reuse.
It contains information about an error, for error pages. Following are the methods of ErrorData class:
- getRequestURL(): It returns the request URL.
- getServletName(): It returns the name of the servlet invoked.
- getStatusCode(): It returns the status code of the error.
- getThrowable(): It returns the Throwable that caused the error.
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
- BodyTag: It extends the iteration tag by defining additional methods that let a tag handler manipulate the content of evaluating its body.
- DynamicAttributes: Implement this interface for a tag to declare that it accepts dynamic attributes.
- IterationTag: It extends the tag by defining one additional method that controls the reevaluation of its body.
- JspTag: It basically serves as a base class for Tag and Simple Tag.
- SimpleTag: It is used for defining Simple Tag Handlers.
- Tag: It is a classic tag handler that does not want to manipulate its body.
- 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
- BodyContent: It is available to a tag handler because it is an encapsulation of the evaluation of the body of action.
- BodyTagSupport: It is a base class for defining tag handlers implementing BodyTag.
- FunctionInfo: It provides information for a function in a Tag Library.
- JspFragment: It is used to encapsulate a portion of JSP code in an object that can be invoked as many times as needed.
- PageData: It provides translation-time information on a JSP page.
- SimpleTagSupport: It is a base class for defining tag handlers implementing SimpleTag.
- TagAdapter: It is using a Tag interface to wraps any SimpleTag and exposes it.
- TagAttributeInfo: It provides information on the attributes of Tag available at translation time.
- TagData: It represents the attribute/value information for a tag instance.
- TagExtraInfo: It is an optional class used to describe translation-time information not described in the TLD.
- TagFileInfo: It is instantiated from the TLD which provides information for a tag file and is available only at translation time.
- TagInfo: It is instantiated from the TLD which provides information for a tag and is available only at translation time.
- TagLibraryInfo: It provides translation-time information associated with a taglib directive and its underlying TLD file.
- TagLibraryValidator: It is a translation-time validator class for a JSP page.
- TagSupport: It provides a base class for defining new tag handlers implementing Tag.
- TagVariableInfo: It is instantiated from the TLD which provides variable information for a tag and is available only at translation time.
- ValidationMessage: It provides a validation message from either TagLibraryValidator or tagExtraInfo.
- 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.