Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP: doPost without throw

 
Siddharth Purandare
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I need urgent help regarding the doPost() method. As per the signature of doPost method the SUN API says

protected void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException

BUT if I try
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
{
// my code here
}

in my servlet Its compiling fine.


My Question is
::

If it can run without mentioning the throw clause then why SUN has given the signature of the method (like throws ServletException, java.io.IOException)
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using these lines in your code...

PrintWriter out = response.getWriter();
out.println("blah");
 
Siddharth Purandare
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey John,

Thanks for the reply But my question is WHY the code is compiling if it does not adhere to the API i.e without any throws clause???
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go back to the basics of method overriding
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a SCJP objective.
I kind of forgot about it too.
"When overriding a method, the types listed in the overriding method throws clause can be a subset of the types listed in the overridden method throws clause. It can throw fewer or no exceptions."
 
Siddharth Purandare
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Siddharth Purandare
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Max

It can throw fewer or no exceptions


this solves my problem.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic