can specify the super class for this jsp page using extends
attribute. This attribute should not be used without careful
consideration as it restricts the ability of the JSP container to
provide specialized superclasses that may improve on the quality of
rendered service.
If
you want to use a class as a super class for a JSP page, then that
class must implement one of the below interfaces.
javax.servlet.jsp.JspPage
javax.servlet.jsp.HttpJspPage
javax.servlet.jsp.JspPage
This
interface extends javax.servlet.Servlet interface. The JspPage
interface describes the generic interaction that a JSP Page
Implementation class must satisfy. This interface defines three
methods that all concrete sub classes must implement.
Method | Description |
void jspInit() | The jspInit() method is invoked when the JSP page is initialized. This method called from Servlet init method, when the jsp page loaded. |
void jspDestroy() | The jspDestroy() method is invoked when the JSP page is about to be destroyed. |
javax.servlet.jsp.HttpJspPage
HttpJspPage
interface extends JspPage interface. The HttpJspPage interface
describes the interaction that a JSP Page Implementation Class must
satisfy when using the HTTP protocol.
Method | Description |
void _jspService(HttpServletRequest request,HttpServletResponse response) | The _jspService()method corresponds to the body of the JSP page. This method is defined automatically by the JSP container and should never be defined by the JSP page author. |
package sample; import java.io.IOException; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.HttpJspPage; public abstract class Today extends HttpServlet implements HttpJspPage{ public String date; public static String getDateTime(){ int day, month, year; int second, minute, hour; StringBuilder buf = new StringBuilder(); GregorianCalendar today = new GregorianCalendar(); day = today.get(Calendar.DAY_OF_MONTH); month = today.get(Calendar.MONTH); year = today.get(Calendar.YEAR); second = today.get(Calendar.SECOND); minute = today.get(Calendar.MINUTE); hour = today.get(Calendar.HOUR); buf.append("Date :" + day +"-" +month+"-" + year +"<br />"); buf.append("Time :" + hour +"-"+minute+"-"+second); return buf.toString(); } @Override public void jspInit() { } @Override public void jspDestroy() { } @Override public void init(ServletConfig config)throws ServletException{ super.init(config); jspInit(); } @Override public void destroy(){ jspDestroy(); super.destroy(); } @Override public final void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ _jspService(request, response); } }
hello.jsp
<%@page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%@page extends="sample.Today"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World</title> </head> <body> <h1><%=getDateTime()%> </h1> </body> </html>
Output
<%@page
extends="sample.Today"%>
Since
the class Today defined in package sample, it is added with package
name like 'sample.Today'.
No comments:
Post a Comment