Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Servlet Model

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks folks,,,
but i am still confused why the doPost method can't be implemented here.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there something to do with the position of out.flush(); and out.close(); ??
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Tony romer
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also,
if you type a url in your address browser manually it is definitely a GET request which will trigger doGet in your case.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sunny i agree with you, but the mock exams shows correct ans 2, if i answered 4 acc. to them it will be wrong.
 
Yatin Dhingra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic