aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes servlet's init method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "servlet Watch "servlet New topic
Author

servlet's init method

tehika danait
Ranch Hand

Joined: Oct 13, 2009
Posts: 52
hi all
this is a question i got from Nikos' blog and it says like this:
Does this servlet compile successfully? (1 correct answer)


1. Compilation succeeds.
2. Compilation fails because there is no init(ServletConfig) in GenericServlet.
3. Compilation fails because init(ServletConfig) of GenericServlet throws ServletException.
4. Compilation fails because init(ServletConfig) of GenericServlet throws IOException and ServletException.
It is said that the answer is 3, but i would say the answer is 1 bacause GenericServlet has a method.
so, please you guys help me by explaining the reason

SCJP 6(90%), SCWCD 5(92%)
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This is the kind of question which you could see at the SCJP exam... HttpServlet#init(ServletConfig config) throws a checked exception, ServletException. What's happening to it, if you're neither catching it, nor rethrowing it ?


[My Blog]
All roads lead to JavaRanch
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I've a doubt about this, here, we are overriding the init() method in our sub servlet class, it is a specific class, so we avoid throwing that exception. But, If the compiler invokes the super class init() method, then compiler gives us a compilation error. Is this the real case? And if the container directly calls the init() method of our sub class servlet, then no need of catch/declare the exception.

What is actually happening?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1522
    
  22

Hi Abimaran,

we are overriding the init() method in our sub servlet class, it is a specific class, so we avoid throwing that exception.

When you are overriding a method it is allowed to have less exceptions then your parent, that is correct.
If the compiler invokes the super class init() method, then compiler gives us a compilation error.

Our overriding method is explicitely executing the code of the super constructor (and normally when you want to override you want to add some code in addition to that, but this is just a trick question), so you have to deal with the Exception that might be thrown there.
And if the container directly calls the init() method of our sub class servlet, then no need of catch/declare the exception.

I am not sure what you are saying here

Regards,
Frits
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Forget about servlets for a moment. Here is a basic Java example to illustrate the problem.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks Christophe! Now I got it!
tehika danait
Ranch Hand

Joined: Oct 13, 2009
Posts: 52
It is now clear, thanks. i agree that this is more like an scjp than scwcd
 
jQuery in Action, 2nd edition
 
subject: servlet's init method
 
Similar Threads
init(ServletConfig config) query
init() method doubt?
Overriding both versions of init() method
Constructor in servlets
why init() method is not final?