1. Can One Jsp/Servlet extend another Servlet/Jsp

Extending a JSP from another servlet or JSP can be a very tricky task. However it is not recommended by sun, because in doing so you restrict the capability of the JSP Engine.

The JSP super class and sub-class need to satisfy certain requirements.

Requirements for the Super Class

  • It must implement the HttpJspPage interface.
  • It will obviously extend the HttpServlet class. It needs to implement the three main lifecycle methods of the servlet:
    • init
    • destroy
    • service

These implementations are ‘final’. In addition, the method getServletConfig must be implemented to return the ServletConfig object, passed onto the servlet during initialization.

This super class now necessitates the base class to actually implement the JSP version of these three function calls: jspInit, jspDestroy, and _jspService, by providing abstract declarations for the same

    • public abstract void jspInit()
    • public abstract void jspDestroy()
    • The signature for the _jspService method depends on the kind of JSP Engine that has been deployed. For eg: if Weblogic is the JSP Engine, the signature is:
public abstract void _jspService(HttpServletRequest request, HttpServletResponse response);
The init method is called jspInit, the destroy method – jspDestroy, and the service method as _jspService.

Requirements for the sub class* It must provide a jspInit() method

* It must provide a jspDestory() method.

<@ page errorPage = “errorpage.jsp” extends=”PureJSPBaseClass” %>

<%!   public void jspInit() {


        public void jspDestory() {



.. other jps stuff.

Cons of JSP Inheritance

This practice is officially discouraged by the JSP specification because you may lose any optimizations that your JSP container provides. It basically restricts the JSP Engine, which would normally provide specialized super classes that improve the quality of a rendered servlet.

The argument against using JSP inheritance goes as follows:

  • If you want to provide a common look and feel, adopt the ‘includes’ usage.
  • If you want to share common utility methods, use utility classes.

Advantage: JSP Inheritance

  • Convenient control of the look and feel
  • Business-based customized statistical and performance measurement routines that are triggered at initiation and during requests to each business page. This could involve measurement of server load, product popularity, peak times, etc.
  • Secure user-role based access
  • Transaction and event logging
  • Consistent Error handling enforcement
  • Common Header Configuration like utility methods that could be extended/used by all JSP pages.
  • Centralized disabling response caching.

One Response

  1. This site is good for how to learn web works.thanks for this infor mation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: