The moose likes HTML, CSS and JavaScript and the fly likes accessing request scope variable from javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "accessing request scope variable from javascript" Watch "accessing request scope variable from javascript" New topic

accessing request scope variable from javascript

veena sasikumar

Joined: Nov 27, 2006
Posts: 29
Dear all,
In my jsp page, I am using javascript function.The function is written in between <HEAD> and /<HEAD> in the document. I use only one form in the jsp page. I use a condition for calling a struts action class whenever a variable bCheck becomes true. I want to validate date fields in the execute method of the action class. In the execute method, I am setting a variable(newView) in the request scope.My aim is to open a new window with a specified url, if the validation is true. The strange behaviour, i noticed is mentioned below.

I printed the variable correctly which i set in the execute method before returning back to the same jsp form from the execute method.
Assume I entered values in the form 3 times(for testing) and clicked for validation. During the second time, i found that variable showNewView is getting the value that was set in the previuos time in the execute method. For example, assume the date format entered is correct for the first two times and wrong for the third time. I should get the variable showNewView as true for the first two times
and false for the third time. Instead of that, first time, alert window showed " showNewView value is null" . second time it showed " showNewView value is true" and in the the third time, it showed the previous value. That is " showNewView value is true"
what is the reason for this?

[ January 10, 2007: Message edited by: Bear Bibeault ]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
You need to rethink your logic here

document.forms[0].action = document.forms[0].action+"?Action=ValidateViewBill";

causes the post back to the server

The following lines may or not be called depending on how fast the post back works.

${requestScope.newView} is filled in from the orginal render of the page. View the source. It is not going to change with a postback.

Bear has an article on the JavaRanch journal that explains about CLient and Server differences. Hopefully he will read this and point you towards it.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

The Secret Life of JavaServer Pages

[Asking smart questions] [About Bear] [Books by Bear]
veena sasikumar

Joined: Nov 27, 2006
Posts: 29
Thanks for the answer. it was really informative and I read the article suggested by you . Still i am not getting a solution for implementing the same.

My existing jsp page is written as one form inside <html:form> tag. and from inside the form, i call a number of javascript functions which are written between <head> and </head> . As per your answer, there is no guarantee that the second function will be called only after the post back from the server. In such a situation, could you suggest a method to implement the same.

I have two struts action classes. If I use second action class, the details from the first jsp page ( or action form) is not available since I use a function showInNewWindow( strUrl ); which opens a new window. How can I solve the problem?

Expecting your valuable answer in this regard..

I agree. Here's the link:
subject: accessing request scope variable from javascript
jQuery in Action, 3rd edition