JSP API
Python is a powerful multi-purpose programming language created by Guido Van Rossum and later developed by Python Software Foundation..
The JSP technology is based on the JSP API that consists of two packages: javax.servlet.jsp and javax.servlet.jsp.tagext. In addition to these two packages, JSP also needs the two Servlet packages—javax.servlet and javax.servlet.http. The javax.servlet.jsp package is the most important package of the JSP API, because when we study the package, we will know why we say that JSP is an extension of Servlet technology and understand why it is important that a JSP application programmer understands the Servlet technology well.
The “javax.servlet.jsp” package has two interfaces and four classes. The interfaces are as follows:
- JspPage
- HttpJspPage
The four classes are as follows:
- JspEngineInfo
- JspFactory
- JspWriter
- PageContext
In addition, there are also two exception classes: JspException and JspError.
JspPage Interface
The JspPage is the interface that must be implemented by all JSP Servlet classes. This is similar to the javax.servlet.Servlet interface and, not surprisingly, the JspPage interface does extend the javax.servlet.Servlet interface. The JspPage interface has two methods, JspInit and JspDestroy, whose signatures are as follows:
public void jspInit()
public void jspDestroy()
jspInit, which is similar to the init method in the javax.servlet.Servlet interface, is called when the JspPage object is created and can be used to run some initialization. This method is called only once during the life cycle of the JSP page: the first time the JSP page is invoked.
The jspDestroy method is analogous with the destroy method of the javax.servlet.Servlet interface. This method is called before the JSP Servlet object is destroyed. We can use this method to do some clean-up, if we want.
Most of the time, however, JSP authors rarely make full use of these two methods. The following example illustrates how we can implement these two methods in our JSP page:
<%!
public void jspInit() {
System.out.println("Init");
}
public void jspDestroy() {
System.out.println("Destroy");
}
%>
HttpJspPage Interface
This interface directly extends the JspPage interface. There is only one method: _jspService(). This method is called by the JSP container to generate the content of the JSP page. The _ jspService() has the following signature:
public void _jspService (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
We can’t include this method in a JSP page, such as in the following code:
<%!
public void jspInit() {
System.out.println("Init");
}
public void jspDestroy() {
System.out.println("Destroy");
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Service");
}
%>
This is because the page content itself represents this method. See the previous section.
JspFactory Class
JspFactory class is an abstract class that provides methods for obtaining other objects needed for the JSP page processing. The class has the static method getDefaultFactory() that returns a JspFactory object. From the JspFactory object, a PageContext and a JspEngineInfo object can be obtained that are useful for the JSP page processing. These objects are obtained using the JspFactory class’s getEngineInfo() method and the getPageContext() method, whose signatures are given here:
public abstract JspEngineInfo getEngineInfo()
public abstract PageContext getPageContext (Servlet requestingServlet,
ServletRequest request,
ServletResponse response, String errorPageURL,
boolean needsSession, int buffer, boolean autoFlush)
The following code is part of the _ jspService method that is generated by the JSP container:
JspFactory _jspxFactory = null;
PageContext pageContext = null;
jspxFactory = JspFactory.getDefaultFactory();
.
.
.
pageContext = _jspxFactory.getPageContext(this, request,
response, "", true, 8192, true);
JspEngineInfo Class
JspEngineInfo class is an abstract class that provides information on the JSP container. Only one method, getSpecificationVersion(), returns the JSP container’s version number. Because this is the only method currently available, this class does not have much use. We can obtain a JspEngineInfo() object using the getEngineInfo() method of the JspFactory class.
PageContext Class
PageContext represents a class that provides methods that are implementation-dependent. The PageContext class itself is abstract, so in the _ jspService() method of a JSP Servlet class, a PageContext object is obtained by calling the getPageContext() method of the JspFactory class. The PageContext class provides methods that are used to create other objects. For example, its getOut() method returns a JspWriter object that is used to send strings to the web browser. Other methods that return Servlet-related objects include the following:
- getRequest(), returns a ServletRequest object
- getResponse(), returns a ServletResponse object
- getServletConfig(), returns a ServletConfig object
- getServletContext(), returns a ServletContext object
- getSession(), returns an HttpSession object
JspWriter Class
JspWriter class is derived from the java.io.Writer class and represents a Writer that we can use to write to the client browser. Of its many methods, the most important are the print and println methods. Both provide enough overloads that ensure us can write any type of data. The difference between print and println is that println always adds the new line character to the printed data. Additional methods allow us to manipulate the buffer. For instance, the clear method clears the buffer. It throws an exception if some of the buffer’s content has already been flushed. Similar to clear is the clearBuffer() method, which clears the buffer but never throws any exception if any of the buffer’s contents have been flushed.