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: 5369
posted
0
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: 5369
posted
0
but its abt compiler .. where the hell container comes in to picture ...
Rozenkreutz
Greenhorn
Joined: Jun 17, 2002
Posts: 7
posted
0
Try to expand the window, you will see both exception...
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
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
posted
0
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 ?
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
posted
0
Amit, You're definitely right. Jerson
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
thanks a lot guys ... I think I have to concentrate also on basic java while lookign for answer for SCWCD ..