Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet's init method

 
tehika danait
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2415
95
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about servlets for a moment. Here is a basic Java example to illustrate the problem.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Christophe! Now I got it!
 
tehika danait
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is now clear, thanks. i agree that this is more like an scjp than scwcd
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic