wood burning stoves 2.0*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JWebPlus question" Watch "JWebPlus question" New topic
Author

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!
Rowan


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: 5371
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: 5371
but its abt compiler .. where the hell container comes in to picture ...
Rozenkreutz
Greenhorn

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: 5371
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 ]
flagholder
Greenhorn

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
Greenhorn

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.


Regards,
Amit
Jerson Chua
Ranch Hand

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

Joined: Oct 15, 2001
Posts: 5371
thanks a lot guys ... I think I have to concentrate also on basic java while lookign for answer for SCWCD ..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JWebPlus question
 
Similar Threads
jweb+ question
Question ID :996245849311
doPost() method signature
HELP: doPost without throw
Wil a servlet Compile when no ServletException is thrown?