• 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...
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!