Small confusion about refreshing the page

Hi,

I have a small confusion about refreshing a page....

I have a JSF page in which when I click on thesubmit button I am saving all the data in the database.

The saving to the database is performed fine and after that I am returningnull from the action method of the submit button....

One more thing since I am returning to the same page again and the backing bean for this page is insession scope so when i return to the page I do something like this so that it gives a feel as if the user has opened the page freshly.

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("MyBean",null);

It works fine uptil here...But now when i refresh the page .... it submits the page again and it tries to send the same data to the database...

My confusion ishow its sending the same data to the database....I meanI made the backing bean as null and stored in the session map...then how its getting the same set of values....Shouldn't the fields of the backing bean be empty too...

Please throw some light on this....its a little confusing...it might be some misunderstanding of the concept....

So please help me know why its doing like that

[1320 byte] By [avnera] at [2007-11-27 10:23:32]
# 1

I'm a little rusty with my JSF, but try removing the bean from the session.

FacesContext.getCurrentInstance().getExternalContext().

getSessionMap().remove("MyBean");

Hope this helps,

CowKing

IamCowKinga at 2007-7-28 17:22:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

If a bean is null, then it will just be recreated on every request.

With refreshing you're resubmitting the POST request. To avoid this, do a redirect after POST.

BalusCa at 2007-7-28 17:22:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Hi,

Thanks Cowking and BalusC for your replies...

Cowking,

Removing the bean doesnt help...moreover i think i read somewhere that if in a map(HashMap) you do...map.put("MyBean",null) or map.remove("MyBean")....they are the same....

BalusC,

> If a bean is null, then it will just be recreated on

> every request

Thats exactly was my purpose.....to create a new bean but only after submitting the request....this way the user wont see previously entered values....

> With refreshing you're resubmitting the POST request.

> To avoid this, do a redirect after POST.

So with refreshing its resending the same post request...but how its getting the same data....the bean has been made null( also removed)...Shouldn't it show validation errors as nothing has been entred in the page...

Also please explain what is meant by redirecting...I mean when i submit the request i have to return to same page...so how can i do a redirect?

avnera at 2007-7-28 17:22:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Redirect to the same page. This will issue a new GET request without the input values in the headers.

Basically:response.sendRedirect(request.getRequestURI());

You can create a PhaseListener to accomplish this for every POST request. See http://balusc.xs4all.nl/srv/dev-jep-prg.html

BalusCa at 2007-7-28 17:22:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thanks BalusC....

so according to the soln. given at the link... we are going back to the same page through a "get" method...

In this case I think when we refresh the page the browser cannot find the old request submitted....bcoz its not directly coming back to the same page from the action method of the submit method....instead it has come through another request which is having request mehod as GET....

I think this is the only possible soln. ...thanks for this BalusC...

But i feel this is a kind of work around that we are doing and still I cant understand why its sending the old data why not the validations have been invoked....

I used a SOP inside the required attribute condition for InputFields to print which button has been invoked to do this submit when refresh has been done...bcoz I am doing validations only if particular button is clicked.....

But it does not print that.....instead it shows some IllegalStateException: Response already comitted ....however is not affecting anything

anyways it looks right now to convert into a get request....if you find some answers to why its sending the old data and why validations are not invoked...please let me know...

avnera at 2007-7-28 17:22:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...