views:

5637

answers:

3

I need to get the value that a user has entered in a textbox on a JSP. I used JavaScript to obtain the value, but then I need to access that same variable outside the JavaScript later on in the page.

Here's what I have:

<script type="text/javascript">

   var sp;

function setPolicy(){

sp = document.getElementById('policy').value;

if(sp != "")
    alert("You entered: " + sp)

else
    alert("Would you please enter some text?")
}

</script>

the textbox:

input type="text" id='policy' size="15" tabindex = "1" onblur="setPolicy()"

But I need to access the string entered in this "policy" textbox later on in a scriplet to call a function within my session bean. Is there any way I can pass that variable outside the JavaScript? Or is there another way to do this?

Thanks!

+1  A: 

JavaScript execution happens at client side and Scriptlet executes at server side. You are trying to combine the two.

You should submit the form to the same page by passing a param which will have the value entered in the textbox. Your scriptlet should check if the param is present or not. First time coming on this jsp the param will not be present, it will be available only when user enter something in textbox.

May not be the best solution as I don't know the whole context.

Bhushan
A: 

I'm not sure you have the lifecycle of a jsp down quite yet. By the time that the javascript is running, all the scriptlets have been evaluated.

By this I mean, one the JSP is rendered, html and javascript are emitted on the response to the browser, and there is no more server to interpret the JSP.

So, you should think of your problem as how do I communicate back to the server, the result of the user action?

Probably by posting a form to an action on the server.

Nathan Feger
A: 

If you are asking how to get hold of text input value in a java session bean, it has nothing to do with your javascript code.

The session bean is server side code. To pass the input value to your session bean, you need to change server side code, a servlet, strut action, webwork action depending on which web framework you use.

Journeyman Programmer