Hello,
I got this question in one of the mock question :
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 = req.getSession().getAttribute("userid");
if(userid != null) generateReport(req, res);
}
}
Assuming that loginUser() and generateReport() are valid methods and have no problems, which of the following statements about these servlets are true?
Options
Select 1 correct option.
1. LoginServlet.java won't compile.
2. ReportServlet.java won't compile.
3. LoginServlet will throw an exception at runtime.
4. ReportServlet will throw an exception at runtime.
5. Both will compile and run without any problems.
The answer is pretty simple that 2. ReportServlet.java won't compile as getAttribute should be typecast to (String) BUT in the first impression I just looked at the doPost Signature both of the doPost have different signature so I created a new servlet with the first servlets code inside it and its working fine. So from here my doubt started that If SUN has defined the signature with throws clause then we must use it and if NOT then it must throw some exception.
I hope I am able to clear my problem.