[Logo]
Forums Register Login
Servlet Model
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/test/HelloServlet"
into the address field of a browser




Answers
1) The code fails to compile.


2) An error page is returned from the Server.


3)The browser displays "Hello World"


4) The server will not find the Servlet due to an incorrect URL.

Acc. to me the correct answer is 4) but i dont know how the correct answer is 2)
HINT-
The Servlet does not implement a doGet method so, an error is returned.
The server does find the servlet from web.xml and via url pattern matching butas ther servlet doesn't implement the doGet method, an error page is returned.

Hope that makes things clear
I think answer is correct,but hint is not clear..! I think implementing doGet method is not required. You can still compile and use the servlet if only doPost method is there.
Thanks folks,,,
but i am still confused why the doPost method can't be implemented here.
Is there something to do with the position of out.flush(); and out.close(); ??
2nd option seems right as wen we direct approach servlet, its a Get request and as its not implemeted page cannot be displayed will show up
By Just looking at URL we can not say that, it will be a call to GET or POST, although for; GET parameters are appended at the end of URL, but what if we don't have any parameter. So we can not say which method is being called.

So overall we will go with the URL-pattern match and also we can not access any thing which is inside WEB-INF directly.

As nothing is given regarding web.xml here,so we can not say whether the error page is configured or not so option 2 is ruled out. I think answer will be 4th.
default implementation for doGet method in HttpServlet returns
405 code which is SC_NOT_IMPLEMENTED,
this is true for doGet and doPost
other methods like doHead works pretty fine and returns code 200 without overriding it.

hope this helps.
Tony
Also,
if you type a url in your address browser manually it is definitely a GET request which will trigger doGet in your case.
sunny i agree with you, but the mock exams shows correct ans 2, if i answered 4 acc. to them it will be wrong.
hi sunnny

I aggree that we can not decide by looking at url, if its get or post but as it says that we have punched it in address bar.. then its definetely a get request(default one)...

Also I dint get why you mention web-inf here?

so I suppose httpsservlets doGet method will run and return a error code, if we have a error page configured to that error code fine else browser will display the error code..

if anybody has implemented this scenario please share the results...
1: Clicking a hyperlink
2: Submitting a foem wothout any method attribute
3: Typing the URL directly in the address bar.
are three point in my knowledge where doGet is being called by default.
Hence in your case its calling the doGet(req,res) method of HttpServlet class which by default reyutns the error page by default,if not overrided in your servlet class.
Folks,

I do see the absence of the url-pattern entry in web.xml which plays a vital role. Perhaps it might have been ignored here since the url pattern is simple if you decode/parse it.

<server>/test/HelloServlet --> indicates the /HelloServlet should have been the url-pattern for the application/context root 'test'.

Apart from that, as other ranchers mentioned the default method gets invoked is GET (thus doGet()) if no method is explicitly mentioned while hitting the servlet. As the Servlet is not implementing the doGet() method, the container is unable to find a match. Hence the error!

Does that help?
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Dec 10, 2017 23:20:58.