jQuery in Action, 3rd edition
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JWebPlus question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JWebPlus question" Watch "JWebPlus question" New topic

JWebPlus question

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
Question ID :996245849311
Consider the code for two servlets of the same web application.
//In file LoginServlet.java
public class LoginServlet extends HttpServlet
public void doPost(HttpServletRequest req, HttpServletResponse res)
String userid = loginUser(req);
req.getSession().setAttribute("userid", userid);

//In file ReportServlet.java
public class ReportServlet extends HttpServlet
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException
String userid = (String) req.getSession().getAttribute("userid");
if(userid != null) generateReport(req, res);
Assuming that loginUser() and generateReport() are valid methods, which of the following statements about these servlets are true?
1)ReportServlet.java won't compile
2)Method generateReport() will never be executed
3)Method generateReport() will be executed only if a post request is sent to LoginServlet before ReportServlet
4)Share-session property should be difined to true in web.xml for ReportServlet to get 'userid'
5)None of the above
it says the correct answer is 3)
However, I was under the impression that doPost() throws IOException, ServletException - so they should both fail to compile!?
but I could be getting horrifically confused...
Any help would be much appreciated!

The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
actually even I dont know why my compiler is compiling.
but as per servlet2.3 API doGet() signature says that it should throw both exceptions.
I have not gone throw Specification so I dont know, why container is allowing that ?

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
but its abt compiler .. where the hell container comes in to picture ...

Joined: Jun 17, 2002
Posts: 7
Try to expand the window, you will see both exception...
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
here is my code which i tried .. I use TextPad .. so no question of exoanding window
Please note the comment ...

after compiling this I get result as
Tool completed successfully
which means it has been compiled in to class file.
[ June 19, 2002: Message edited by: Ravish Kumar ]

Joined: Jun 10, 2002
Posts: 1
I think the anwser may be is :
In the HttpServlet class, Thare are many method like doPost, doGet ... which will throw ServletException and IOException. It is right.
But in your case, you define tow new class extending HttpServlet, and you override the doPost method. So to your doPost method, it may or may not be throw ServletException and IOException, for you had overrided that method.It is the feature of Java itself.
Am i right ?

+-------------+<br />+ Freedom +<br />+-------------+
Amit Ghag

Joined: May 11, 2002
Posts: 27
I agree with flagholder. We declare a throws clause to indicate that the calling method should catch and treat the thrown exception. However, it is not necessary that we always want to delegate the exception handling to the calling method.
Note - An overriding method may or may not throw the exceptions declared in the throws clause of the overridden method.
Correct me if i m wrong.

Jerson Chua
Ranch Hand

Joined: Feb 08, 2000
Posts: 68
You're definitely right.
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
thanks a lot guys ... I think I have to concentrate also on basic java while lookign for answer for SCWCD ..
I agree. Here's the link: http://aspose.com/file-tools
subject: JWebPlus question
It's not a secret anymore!