This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Why servlet has to reload the content after invoking doPost or DoGet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why servlet has to reload the content after invoking doPost or DoGet" Watch "Why servlet has to reload the content after invoking doPost or DoGet" New topic
Author

Why servlet has to reload the content after invoking doPost or DoGet

Ken Flor
Ranch Hand

Joined: Aug 01, 2006
Posts: 34
Hi All,

I am very puzzled. Why Servlet must reload the content of the response when I call doGet/doPost after submitting my form.

I just want to do some logic in the doPost after submitting my form and to return the original content response without doing anything else. But i must do PrintWriter to output the same content again.


Why is that so?
Any way to return the original content.

Please help me.

Servlet is driving me crazy.
[ September 12, 2006: Message edited by: Bear Bibeault ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It isn't really the Servlet's fault, it's the transport mechanism. Servlets (mostly) talk HTTP, and HTTP says you must have a request and a response. You can't have a request without a response and you can't have a response without a request. The default for browsers is to send the request and then display the response on the page, so if you want to have the same page displayed you need to send the same page as the response.

The alternative is to use something different like AJAX (ie communication via JavaScript) to talk to the servlet so that you don't get the browser to manage the request/response but manage it yourself.

Dave
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by David O'Meara:

The alternative is to use something different like AJAX (ie communication via JavaScript) to talk to the servlet so that you don't get the browser to manage the request/response but manage it yourself.

Dave


And.... even if you do this, the server side of the application will still return one response per request.
This is how HTTP works.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Scott Duncan
Ranch Hand

Joined: Nov 01, 2002
Posts: 363
But i must do PrintWriter to output the same content again.


Ideally, you would not want to use the servlet to output the content at all. You should use a JSP for this that submits to a servlet. The servlet performs some logic and forwards back to the JSP.


No more rhymes! I mean it!<br /> <br />Does anybody want a peanut?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why servlet has to reload the content after invoking doPost or DoGet
 
Similar Threads
please reply urgently!!!
Servlet
Error 400:
Will the browser Session created in the doPost methods be recognised by the doGet
Servlet chaining