File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HELP: doPost without throw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HELP: doPost without throw" Watch "HELP: doPost without throw" New topic
Author

HELP: doPost without throw

Siddharth Purandare
Ranch Hand

Joined: Nov 13, 2006
Posts: 101
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)


Siddharth Purandare<br />SCJP 1.4 - 76%<br />SCWCD 1.4 - 85%<br /> <br />It is the "attitude" and not the "aptitude" which determines "altitude".
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Try using these lines in your code...

PrintWriter out = response.getWriter();
out.println("blah");


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Siddharth Purandare
Ranch Hand

Joined: Nov 13, 2006
Posts: 101
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

Joined: Mar 27, 2006
Posts: 245
Go back to the basics of method overriding


Vikrant Pandit
Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

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."


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Siddharth Purandare
Ranch Hand

Joined: Nov 13, 2006
Posts: 101
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

Joined: Nov 13, 2006
Posts: 101
Hey Max

It can throw fewer or no exceptions


this solves my problem.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HELP: doPost without throw
 
Similar Threads
post request from jsp not received by Servlet
doPost()
Compilation Error
My very own MVC framework...
init(ServletConfig config) query