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 Javaranch mock exam question 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 "Javaranch mock exam question" Watch "Javaranch mock exam question" New topic
Author

Javaranch mock exam question

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
Consider the following:

package com.javaranch;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>\n <body>\n");
out.println(" <h2>\n Hello World\n </h2>\n");
out.println(" </body>\n</html>\n");
out.flush();
out.close();
}
}

What is the result of compiling the above Servlet and accessing it by typing:
"http://www.javaranch.com/servlet/com.javaranch.PostServlet"
into the address field of a browser

Answers
The server will not find the Servlet due to an incorrect URL.
An error page is returned from the Server.
The browser displays "Hello World"
The code fails to compile.

Given answer:
The Servlet does not implement a doGet method so, an error is returned.

My question:
From what I understand, a servlet needs to implement either doGet or doPost.
In the above code, the doPost method is overridden.
So, why would the above code return an error?
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
A simple hyper link is always a GET request.

So by typing the address into the browser, you are sending a GET request. Since the servlet does not have a doGet method, an error is returned.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Bhumika Thakkar
Ranch Hand

Joined: Apr 18, 2005
Posts: 79
If we had overriden the doGet() then would the server throw an exception because of the URL?

Bhumika


SCBCD (Preparing) <br />SCWCD, 82%<br />SCJP, 90%
Kejal Shah
Ranch Hand

Joined: Jun 27, 2003
Posts: 87
Well,
If the mapping is done properly in the DD, it'll work fine, else you'll receive a Page not found or a container specific message


Kejal<br />SaneDevil@gmail.com
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

For the older versions of Tomcat, the URL is fine. But latest versions does not support such type of URLs.

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javaranch mock exam question