• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doGet Serious Doubt

 
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic