Problem with </c:if>

This is an my Page advancedOptions.jsp

<form action="advancedOptions.jsp" method="GET">

<input type ='Submit' name ='action' value = 'Clear' >

</center>

</FORM>

and then

<c:if test="${param.action == 'Clear'}">

<%

leaveChangeList.clear();

leaveBean.setName("-");

leaveBean.startDate(startDate);

leaveBean.endDate(endDate);

leaveBean.setNumDays(leaveBean.getElapsedDays(startDate,endDate) + 1);

leaveChangeList.add(leaveBean);

session.setAttribute("leaveChangeList", leaveChangeList);

%>

</c:if>

So first time I visit the page populate a few Beans in the Array and they are displayed on the page. Then I click Clear and it clears all the elements and just creates a new starting point.

My problem is with

<c:if test="${param.action == 'Clear'}">

it seems as if as soon as I click Clear for the first time the page thinks I click it everything I refresh the page. I think this has to do with param.action == 'Clear' becoming true the first time I click and it thinks I click it then every time I refresh the page even though I don't.

Is there a way of setting this to false or only going into

<c:if test="${param.action == 'Clear'}">

When the button is actually clicked

Thanks

[1398 byte] By [Lunnya] at [2007-10-2 23:53:42]
# 1

Its behaving exactly as specified. Looks like you'll have to take a step back and understand the request lifecycle to understand your problem.

Ok so you have this page with a form that has a 'clear' button. The form's action attribute(action="advancedOptions.jsp") specifies that when the form is submitted (the form button is clicked), the browser sends a http request to the resource 'advancedOptions.jsp'. Also the name and value of the submit button gets transmitted in the request query string. You can easily enough check in your browser's url. On submit it would show something like http://blah.blah:port/blah/advancedOptions.jsp?action=clear.

> This is an my Page advancedOptions.jsp

>

> <form action="advancedOptions.jsp" method="GET">

> <input type ='Submit' name ='action' value = 'Clear'

> >

> </center>

> </FORM>

>

Now since this is the same jsp in question, advancedOptions.jsp receives the request with a param action=clear. This in turn causes the below if condition to evaluate to true and execute the contents within it.

> and then

>

> <c:if test="${param.action == 'Clear'}">

> <%

> leaveChangeList.clear();

> leaveBean.setName("-");

> leaveBean.startDate(startDate);

> leaveBean.endDate(endDate);

> leaveBean.setNumDays(leaveBean.getElapsedDays(startDat

> e,endDate) + 1);

> leaveChangeList.add(leaveBean);

> session.setAttribute("leaveChangeList",

> leaveChangeList);

> %>

> </c:if>

>

> So first time I visit the page populate a few Beans

> in the Array and they are displayed on the page. Then

> I click Clear and it clears all the elements and just

> creates a new starting point.

>

> My problem is with

> <c:if test="${param.action == 'Clear'}">

>

> it seems as if as soon as I click Clear for the first

> time the page thinks I click it everything I refresh

> the page. I think this has to do with param.action ==

> 'Clear' becoming true the first time I click and it

> thinks I click it then every time I refresh the page

> even though I don't.

>

> Is there a way of setting this to false or only going

> into

> <c:if test="${param.action == 'Clear'}">

>

> When the button is actually clicked

>

> Thanks

So there you see, you click the button every time and each click submits the request with a param action=clear, the same jsp executes, the if condition evaluates to true and the page is reloaded as part of the jsp's response which is how it should behave.

There's no way you can avoid the page refresh because to delete the elements from the list, you have to submit a request (just like how you do it to via the form) to the jsp. The browser sends the request, the jsp executes on the server and sends html output to the browser for display

Does that help?

ram.

Madathil_Prasada at 2007-7-14 16:39:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thanks Madathil,

Yes it did explain it a bit, but can you please explain then how I would get something working that when a button is pressed I would like to perform an action, and only perform that action when the button is actually pressed.

Ive been trying to get this working all day with no avail. Well with the

if<C it works for the first time I pressed the button but then it performs the action within the ><C if> every time on refresh even if the button isnt clicked

I dont mind the page refresh, Its that I only want to perform action when the button is pressed, but for some reason now after the button is clicked once, it thinks the button is pressed every time.

Lunnya at 2007-7-14 16:39:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Just reread your post

> that when the form is submitted (the form button is

> clicked), the browser sends a http request to the

> resource 'advancedOptions.jsp'. Also the name and

> value of the submit button gets transmitted in the

> request query string. You can easily enough check in

> your browser's url. On submit it would show something

> like

> http://blah.blah:port/blah/advancedOptions.jsp?action=

> clear.

On yes after the first time Clear is pressed and the page is refreshed

the URL remains

http:///advancedOptions.jsp?action=Reset

So is there a way then to unset the action when the page is refreshed after the Clear button is clicked ?

Lunnya at 2007-7-14 16:39:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> I pressed the button but then it performs the action within the ><C if> >every time on refresh even if the button isnt clicked

How do you refresh the page - using an F5 manually? If yes, is that a valid scenario for you to worry about? I mean the list has just been cleared and the effect of a refresh is to clear an already cleared list again. Whats the problem?

>On yes after the first time Clear is pressed and the page is refreshed

>the URL remains http:///advancedOptions.jsp?action=Reset

Exactly. Because that was the last page that was requested by the browser instance. A refresh would resend your last request which was the form submission with the action=Reset attribute.

However if your page is refreshed using some other action (like clicking on another link or submitting another form), then you wouldnt need to worry about the original request being resent as they would either point to some other jsp/servlet or have a different query string.

ram.

Madathil_Prasada at 2007-7-14 16:39:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

ok the way it is refreshed it as follows...

http:///advancedOptions.jsp

on that page are 2 links that open a pop-up with form inputs, once submitted they create a bean and bean gets added to the array. Pop-up closes and http:///advancedOptions.jsp gets refreshed.

This is all works no problem then when I click Clear on

http:///advancedOptions.jsp

The ArrayList is cleared and all the beans previously there are now gone, which again working as intended.

But then when I open a pop-up to create a new Bean and click submit I go back to the http:///advancedOptions.jsp as before but no beans are displayed. with the HTTP header being

http:///advancedOptions.jsp?action=Reset

Even though I havent clicked Clear it has still performed that action. But as you said you explained why this happened. But then how do I get around it that when page is refreshed after clear action=Reset but back to original blank ?

Lunnya at 2007-7-14 16:39:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

So heres the action from one of the pop-up pages name Option1.jsp

which has input forms and handles the input on the same page....but adds a bean to ArrayList...

<form action="Option1.jsp" target="_parent" method="POST">

<input type ='submit' name = 'action' value = 'Submit' >

</form>

<c:if test="${param.action == 'Submit'}">

die( "<script>window.opener.location.reload();window.close();</script>");

</c:if>

Lunnya at 2007-7-14 16:39:39 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Ok sorry for spamming but I still need help

I now know the problem lies with

''However if your page is refreshed using some other action (like clicking on another link or submitting another form), then you wouldnt need to worry about the original request being resent as they would either point to some other jsp/servlet or have a different query string.''

After user presses submit on pop-up the form actually sends data to pop-up since thats where i handle it and to get back to main page I just do

die( "<script>window.opener.location.reload();window.close();</script>");

So the original request header for ''Clear'' doesnt go away leaving me unable to add new data.

How could I replacing this request then ? Hidden Forms maybe ? or use Booleans values for onClick in the main page?

Message was edited by:

Lunny

Lunnya at 2007-7-14 16:39:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

This is not a new issue. Refreshing the page, issues exactly the same request as was used to generate the page in the first place, with all the same parameters.

So if you passed the "clear" parameter, then that would get re-submitted with the refresh.

The standard way to avoid this sort of thing is to handle the action in code, and then redirect to a JSP to display the results of that action.

Now if the page gets refreshed, all it does is re-request the JSP displaying the results - it doesn't redo the "clear"

<c:if test="${param.action == 'Clear'}">

<%

leaveChangeList.clear();

leaveBean.setName("-");

leaveBean.startDate(startDate);

leaveBean.endDate(endDate);

leaveBean.setNumDays(leaveBean.getElapsedDays(startDate,endDate) + 1);

leaveChangeList.add(leaveBean);

session.setAttribute("leaveChangeList", leaveChangeList);

%>

<c:redirect url="advancedOptions.jsp"/>

</c:if>

Cheers,

evnafets

evnafetsa at 2007-7-14 16:39:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

ok that makes sense im at home now though so can't check it out but will let you know tomorrow..

but then for AdvancedOptions and my Clear Button what do i put in my

Form action etc...

Do I still do

<form action = ''advancedOptions.jsp'' method=''post''>

<input type ='submit' name = 'action' value = 'Clear' >

</form>

and have

<c:if test="${param.action == 'Clear'}">

<%

leaveChangeList.clear();

....

session.setAttribute("leaveChangeList", leaveChangeList);

%>

<c:redirect url="advancedOptions.jsp"/>

</c:if>

Seems like I am directing it twice to advancedOptions.jsp once in the form and the other in C tags.

Anyway will try it tomorrow..

Lunnya at 2007-7-14 16:39:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

>Seems like I am directing it twice to advancedOptions.jsp once in the >form and the other in C tags.

No get this right even if it takes time (it'll save you a lot of headaches in the future).

The form is displayed on the browser(client side). Clicking the submit button (or clicking a link for that matter) sends a request (from the client browser) to the server. The jsp executes on the server. The output of the jsp execution on the server is html which is sent to the client browser. Any subsequent action on the brwoser is a new request to another (or the same) jsp. A refresh is also a new request to the same jsp.

That is the vanilla scenario. A jsp rather than writing out the response html may forward to another jsp (using the <jsp:forward> action) which in turn may fill up the response. A forward thus is internal to the server.

Alternatively it may choose to redirect to another resource (probably a jsp). A jsp receives a browser request, does some processing and then as response, rather than sending html across, instructs the browser to send another request to some other resource. That resource in turn outputs html. So actually a redirect involves 2 browser requests.

Now in your case, as in the way evnafets pointed out, the jsp (advancedOptions.jsp) receives the request, checks if there is a parameter called action and if this parameter's value is 'reset', it deletes the beans. So far so good. What's important is that it then sends a redirect to itself (without the param). The browser in turn sends another request to the same jsp and this time since the param is missing, the <c:if> evalutes to false. The jsp just outputs the html form. The point being, the last request made by the browser was to advancedOptions.jsp without the action param. This would ensure that subsequent refreshes too do not have the param in the query string and that would mean your bean doesnt get deleted. Does that make things clear?

ram.

Madathil_Prasada at 2007-7-14 16:39:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Yes Ram your post have helped me a great deal with understanding more how JSP works.And to the solution posted above with <c:redirect url="advancedOptions.jsp"/>That worked, so thanks a million guys
Lunnya at 2007-7-14 16:39:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...