Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doGet Serious Doubt

 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.print("Hey");
out.close();
}
}
1) The code fails to compile.
2) The server will not find the Servlet due to an incorrect URL.
3) An error page is returned from the Server.
4) The browser displays "Hello World"

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

Q-1 Why is ans 3) correct.
Is it because :The Servlet does not implement a doGet method so, an error is returned.

but HttpServlet provides the default doGet(). Why that is not called ?

Q-2 If i change the above doPost() to doGet(), why it's printing Hey
When i say flush() the response is committed.. Now i can't do anything but it's still printing Hey ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but HttpServlet provides the default doGet(). Why that is not called ?

It is called, but the default implementation will send an error. You must override it.

When i say flush() the response is committed..

You can still print after flushing. Otherwise, what would be the use of flushing ?
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default implementation of doGet() method throws an error. From Sun's site
doGet(HttpServletRequest, HttpServletResponse)
Performs the HTTP GET operation; the default implementation reports an HTTP BAD_REQUEST error.


Where is this statement in HFSJ ?

Flushing means that whatever data is there in the stream, pass it to the client. That means we have already sent response to client. Now anything you do after that will either have no effect or will throw IllegalStateException (sendRedirect() etc..)

What all operations (other than print() and close() )are permitted after response is committed ?
[ March 21, 2008: Message edited by: Sandeep Vaid ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic