Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JWebPlus question

 
Rowan Chattaway
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but its abt compiler .. where the hell container comes in to picture ...
 
Rozenkreutz
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to expand the window, you will see both exception...
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Amit Ghag
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit,
You're definitely right.
Jerson
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot guys ... I think I have to concentrate also on basic java while lookign for answer for SCWCD ..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic