This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

doGet Serious Doubt

 
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 ?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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 ]
 
The problems of the world fade way as you eat a piece of pie. This tiny ad has never known problems:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic