Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question ID :996245849311

 
Yu Chen
Greenhorn
Posts: 28
  • 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 is true?
--------------------------
I don't think the doPost() methods have the correct signature, will these really compile?
The answer says,
- The generateReport() method will be executed only if a post request is sent to the LoginServlet before ReportServlet.
Thanks,
-yu chen
 
Kyle Tang
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you overwrite a method, you can throw less exceptions, right? (SCJP context)
So you don't have to throw ServletException from your do*** methods. If you don't put it in the method signature, it compiles fine.
Kyle
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Yu Chen:
I don't think the doPost() methods have the correct signature, will these really compile?

The doXXX() methods throw ServletException and IOException, so a servlet that overrides HttpServlet can have a doPost() that throws both exceptions, one of them, or none. (Or subclasses...) Of course, if you invoke methods that throw the checked IOException, you have to catch or rethrow it...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic