aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServletException and IOException in doGet(...) 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 "ServletException and IOException in doGet(...)" Watch "ServletException and IOException in doGet(...)" New topic
Author

ServletException and IOException in doGet(...)

Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
@WebServlet(urlPatterns={"/foo/*"}, name="NullHaus1", initParams=@WebInitParam(name="var1", value="Howdy!"))
public class NullHausServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String param1 = getInitParameter("var1");
String param2 = getServletContext().getInitParameter("var1");
resp.getWriter().print("Values: " + param1 + ", " + param2);
}
}
----------------------
Is it mandatory that doGet should throw ServletException and IOException? The above example only throws IOException.


"There are no dumb questions" quoted from HFSJ. "To err is human; to forgive is divine"
OCPJP 6, OCEJWCD 6
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 355

Not mandatory. But ServletException will be thrown when the Servlet can't handle the GET request.
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
Thanks for replying. In Nikos Mock exam, one of the exam items requires both Servlet and IO exception should be declared, otherwise it'd be wrong. I saw some code examples in Servlet 3.0 specs that they even do not declare either exceptions. So if doGet() declares no exceptions, would you think it is right?
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
In SCWCD 5, it seems like it is important to declare both ServletException and IOException in doGet(), doPost() etc, like some exam items in Nikos mock exam especially testing for it. In Servlet 3.0, it seems like it is not that important to do so. For example, many exam items in Piotr Nowicki's mock exam and some examples in 3.0 spec do not even declare any of the two exceptions, or just declare one in doGet() and doPost(). That confuses me.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

That's just because of the rules of the Java language for declaring exceptions thrown by overriding methods. Nothing to do with servlets or web applications specifically.

The rule is that a method of a subclass cannot declare that it throws an exception which the method it's overriding doesn't throw. So the overriding method can't throw more exceptions than the overridden method. However the opposite isn't true: the overriding method can throw fewer exceptions than the method it overrides. So what you're seeing is simply an instance of that rule. The overridden method might declare that it throws ServletException, but a method which overrides it doesn't have to declare that.
Joe Allen
Ranch Hand

Joined: Nov 14, 2012
Posts: 93
Hi Paul, thanks for the explanation. You are right. In fact, I thought about that too, but I am new to web component, not sure if there is something special in web component. I got confused after I took Nikos mock exam, and saw one of the exam items like this:

22.What happens when this servlet is compiled and deployed? (1 correct answer)

public class Test extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><body>");
out.println("<p>Hello!!</p>");
out.println("</body></html>"); out.close(); } }

1.Compilation fails because doGet() is protected.
2.Compilation fails because doGet() does not declare a ServletException.
3.Deployment succeeds and clients are served just fine.

Oh, as I was posting the code, I found out that the correct answer is c, i.e. 3, not what I thought it was, b, i.e. 2. I must have misread the correct answer. That was why I got confused.
My bad. It is non-issue now. Thanks again for taking time to answer my question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletException and IOException in doGet(...)