What is the different between _jspInit() and jspInit()?

Showing Answers 1 - 33 of 33 Answers

gaurav0252

  • Nov 23rd, 2007
 

when a JSP page transalated into a Servlet then jspInit() method is converted to _jspInit(). We can override _jspInit() and _jspDestry() but we can't override _jspService() method.

  Was this answer useful?  Yes

gaurav0252

  • Nov 23rd, 2007
 

when a JSP page transalated into a Servlet then jspInit() method is converted to _jspInit(). We can override _jspInit() and _jspDestry() but we can't override _jspService() method.

  Was this answer useful?  Yes

vishnu.baji

  • Feb 28th, 2008
 

I dont know the difference between _jspinit() and jspInit() but jspInit() is not converted as _jspInit()... I have checked in the genereated java file it is the same jspInit() name I observed....

there is no _jspInit() ..

Underscore is the symbol which says we cannot Overrride

The life cycle methods are
                  
jspInit()   we can override

_jspService()  we cannot override

jspDestroy()    we can override
           
                       

ofdrm

  • Aug 20th, 2008
 

All those who say 'There is no_jspinit() in generated servlet'.... are wrong....

As of servlets spec 2.4, this method is generated by the container if any custom tags are used in the JSP. Otherwise it doesn't.

We can override jspInit()... in that case both th methods will be there, but that will not exactly be an override as _jspInit() will take precedence and be used for initialization.

if we try to override _jspInit() and using custom tags, container will throw compilation error saying -duplicate methods. (there will be two _jspInit() methods with same signature)

Similarly we can mention our own jspSerivce() method, but that will be of no use (even though in translated java file it will be there).

The difference between overriding jspInit() and jspService() is that  - _jspService() will always be there in any JSP->java file and will take precedence and our jspService() method will not be used ever.
But _jspInit() method appears in jsp->java file conditionally. so, our jspInit() method will be invoked when there is no _jspInit() method and this is quite possible.

Example:-
Here is the jsp file code I tested... (I'm not putting the java file code as it is lengthy... but the jsp got translated and compiled without any error and our jspService() method was not used).

<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/TR/REC-html40/strict.dtd">
<%@ page session="false" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>
<c:set var="drm" value="ddd" />
<%!
    public void jspInit() {
    }

    /* --duplicate method error
    public void _jspInit() {
    }*/

    public void jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {
            JspWriter out = JspFactory.getDefaultFactory().getPageContext(this, request, response,
                  null, false, 8192, true).getOut();

            out.write("<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/TR/REC-html40/strict.dtd">rn");
            out.write("rn");
            out.write("rn");
             out.write('r');
          out.write('n');
          out.write("rn");
          out.write("sss");
    }
%>
ddd

  Was this answer useful?  Yes

In JSP we have a life cycle methods like jspInit(), and jspDestroy() and _ JspService() methods but there is no sense if u write in ur jsp as _jspInint(), but u can write

  Was this answer useful?  Yes

Here is the skeleton of HttpJspBase class which is developed by tomcat developers.

You might have a clue from this for all related answers. 

public abstract class HttpJspBase extends HttpServlet
{
/*following methods are final*/
static 
{
// some clases are loaded in this section
}
@Override
final public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException
{
_jspService(arg0, arg1);
}
@Override
final public void init() throws ServletException
{
super.init();
jspInit();
_jspInit();
}
@Override
final public void destroy()
{
// TODO Auto-generated method stub
jspDestroy();
_jspDestroy();
}
/*following is implemented by the container during transformation stage*/
abstract void _jspService(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException;
/*following are exposed to developer and are empty by default*/
public void jspInit(){}
public void jspDestroy(){}
public void _jspInit(){}
public void _jspDestroy(){}
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions