GeeCON Prague 2014*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes regrd refresh in browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "regrd refresh in browser" Watch "regrd refresh in browser" New topic
Author

regrd refresh in browser

mysha ahamed
Greenhorn

Joined: Aug 03, 2005
Posts: 22
Hi,
I have a dbt how refresh works for servlets.
I have a html page which asks user for color.When submitted control goes to below servlet which dispalys users color..Thts ok.

http://localhost:9000/chapter01/DemoServlet2uri
Ur color:entered color

My dbt is, when i refereshed this url or clicked enter with same address on this browser's window addr bar, is a new request sent to the server ? or is the same old request ? bcas i think if a new request is sent then it shuld dispaly the color=null bcas this req doesnt have any user entered color val.
If its the same old req then wht is the mechanism behind it?
can anyone clear me this dbt or give me any link to study.

Thnx
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
It's a brand new request. The color is not null because the servlet is probably somehow storing the color. Post your code, so we can take a look at it.


Leandro Melo
SCJP 1.4, SCWCD 1.4
Alexandre Cervieri
Greenhorn

Joined: Aug 02, 2005
Posts: 27
Hello Mysha,
Just complementing Leandro's explanation. We should remember that HTTP is a stateless protocol. So, every request is seen as a brand new request by the web server. It's not possible to make the question "is it the same old request". Regarding only HTTP mechanisms each request is a new request even if it comes from the same client, to the same server and to the same resource (JSP, servlet, HTML, ...).
Regards,
Alexandre.
Aruneesh Salhotra
Ranch Hand

Joined: May 12, 2004
Posts: 167
Interestingly, if you have sessions maintained and set the color as a session attribute, you would be good.
mysha ahamed
Greenhorn

Joined: Aug 03, 2005
Posts: 22
Hey,
I thought it will be clear if i posted the code.I have an html which asks color frm user.next servlet which forwards the same req to 2nd servlet.

Sergey Tyulkin
Ranch Hand

Joined: May 10, 2005
Posts: 87
When you use requestdispatcher's forward method, you use the same requext object. That's why you get such results. Request dispatcher forward the request you send from client to another servlet or jsp, but client doesnt know this.
If you want, to create new request, you must use request.sendRedirect() method
mysha ahamed
Greenhorn

Joined: Aug 03, 2005
Posts: 22
Hey
I didnt get u.Second time since a new request object(whic has no color) is sent to the servlet n this req obj is forwarded..So the color value shuld be null?
Thnx
Alexandre Cervieri
Greenhorn

Joined: Aug 02, 2005
Posts: 27
When you submit the form the first time the following will be execute:
DemoServlet1.doPost --> DemoServlet2.doPost --> back to client

When you press enter in the brower's address field what address is displayed there? http://localhost:9000/chapter01/DemoServlet1uri or
http://localhost:9000/chapter01/DemoServlet2uri?

If it is http://localhost:9000/chapter01/DemoServlet2uri, than the DemoServlet2 is executed and is obtaining the correct color from the session, that I think is based on cookies in your case.

However, if the address field is displaying http://localhost:9000/chapter01/DemoServlet1uri, I don't know exactly what is happening. Method doGet should be called, but you didn't overriden it, so
Alexandre Cervieri
Greenhorn

Joined: Aug 02, 2005
Posts: 27
Just complementing my previous post.
Instead of pressing Enter in the browser's address field, if you press F5 (reload/refresh) in the page that is displaying your color (not the form), then what will happen is the same flow that ocurred in the first time:
form submit (browser "saved" the values you entered) --> DemoServlet1.doPost --> DemoServlet2.doPost --> back to client
right?
mysha ahamed
Greenhorn

Joined: Aug 03, 2005
Posts: 22
Hi
Thnx for ur reply.The url tht i refreshed or pressed enter is the below one.This url is for the first servlet which comes after submitting form..

http://localhost:9000/chapter01/DemoServlet1uri

When i refresh ,the same color is getting displayed.I have put some debug
system.out.println(),which r displayed in tomcat console..when i refresh,
the debug stmt from DemoServlet1 & DemoServlet2 r dispalyed.So,
when refreshed DemoServlet1.doPost()->DemoServlet2.doPost(). r invoked in this order.
I dont know why the doPost() is invoked n why the same color is dispalyed..

Thnx
 
GeeCON Prague 2014
 
subject: regrd refresh in browser