• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

JWebPlus question

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but its abt compiler .. where the hell container comes in to picture ...
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to expand the window, you will see both exception...
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit,
You're definitely right.
Jerson
 
R K Singh
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Yes, my master! Here is the tiny ad you asked for:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic