|
jsp-develop.de presents: JSP-Vererbung |
| von shark, 16.10.2004 10:44:35 |
|
Seit der Publikation der Servlet Spezifikation 2.2 besteht die Möglichkeit das HttpJspPage Interface zu implementieren und dabei die implementierende Klasse des Interfaces als Basisklasse für JSP-Seiten zu verwenden. Diese Technik entfaltet sich besonders gut in Modell-1 basierten WEB-Applikationen. MVC Anwendungen bieten im Gegensatz zu Modell-1 Applikationen von Hause aus die Möglichkeit Aktionsklassen durch Vererbung zu erweitern. Modell-1 Applikationen können aufgrund der Vielzahl von JSP-Seiten schnell, unübersichtlich und schlecht wartbar werden, wobei auch solche Anwendungen mit JSP-Vererbung und einem JSP-basierten Frontcontroller strukturiert werden können. Gerade der Frontcontroller ist ein gutes Beispiel für eine "Best Practice". Dabei basiert ein Controller bei klassischen Modell-2 konformen Anwendungen in der Regel auf einem Servlet und nur partiell auf einer JSP-Seite. Die nachfolgende Klasse implementiert das HttpJspPage Interface und ist ein Beispiel dafür, wie eine Versionsinformation in einer JSP-Seite ausgegeben werden kann. Es besteht bei der Wahl dieser Technik auch die Möglichkeit JSP-Seiten über einen eleganten Weg mit Konfigurationsdaten zu versorgen ohne dass diese Funktionalität über viele JSP-Seiten verteilt werden müsste. JSP-Basisklasse: -------------------------- package de.jsp.base; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.jsp.HttpJspPage; /** * base class for jsp pages which holds version * information for jsp application; this technique * may also be used to initialize jsp page with application * specific initialization data which should be shared by * jsp pages, eg. database relating information in cases * of database access; * * the "HttpJspPage" interface is part of the servlet 2.2 * specification and therefore a servlet 2.2 complaint * servlet engine and "javax.servlet" package is necessary * to build and run this application; * * "HttpJspPages" interface includes the following jsp methods: * *_jspService(): this method corresponds to the body of the jsp page * * "HttpJspPages" interface inherits the following methods * from the Servlet interface: * * init() * destroy() * service() * getServletConfig() * getServletInfo() * * @see servlet specification 2.2 */ public abstract class JspHeader implements HttpJspPage { /** * version information */ final private String VERSION_KEY = "version-info"; final private String VERSION_VALUE= "JSP-Application / Release 1.1 / 15.10.2004"; /** * class information */ final private String CLASS_INFO = "JSP-Page-Superclass"; /** * servlet config object reference */ private ServletConfig servletConfig = null; /***************************************************************** * jsp interface methods *****************************************************************/ /** * jsp init method */ public void jspInit() {} /** * jsp destroy method */ public void jspDestroy() {} /** * jsp service method * @param servlet request * @param servlet response * @exception ServletException * @exception IOException */ public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {} /*********************************************************** * servlet interface methods ***********************************************************/ /** * servlet init method * (called by servlet container) * @param servlet config */ public void init(ServletConfig conf) { this.servletConfig = conf; } /** * servlet destroy method * (called by servlet container) */ public void destroy() {} /** * servlet service method * (called by servlet container) * @param servlet request * @param servlet response * @exception ServletException * @exception IOException */ public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { // version information should be set only once if(null == ((HttpServletRequest) request).getSession().getAttribute(VERSION_KEY)) { // set the version information of this web application ((HttpServletRequest) request).getSession().setAttribute(VERSION_KEY,VERSION_VALUE); } // do additional service initialization _jspService((HttpServletRequest) request, (HttpServletResponse) response); } /** * get the servlet config object * @return servlet config */ public ServletConfig getServletConfig() { return(servletConfig); } /** * get servlet info identifier * @return servlet info identifier */ public String getServletInfo() { return(CLASS_INFO); } } JSP-Seite: ------------------ <%@page extends="de.jsp.base.JspHeader"%> <b><%=request.getSession().getAttribute("version-info")%></b> |
|
URL dieses Beitrags: http://www.jsp-develop.de/forumbeitrag/view/30115/ |