• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HELP: doPost without throw

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go back to the basics of method overriding
 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Max

It can throw fewer or no exceptions



this solves my problem.

Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic