Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parameters with JSP

Status
Not open for further replies.
Nov 28, 2002
121
0
0
US
I have a JSP page that includes a drop down list box queried from a database. I want a check box adjacent to the list box to trigger a new query of the list box.
checking and unchecking the check box will result in alternating lists.
I want this to occur on the same JSP page. How do I pass a checkbox parameter to JAVASCRIPT and back to the JSP page again everytime the checkbox is checked.
 
Use an onclick event on the checkbox like this:
onclick="myfunction(this.checked);"

This should pass true or false depending on the checked state of the checkbox. You can check if the passed value is true or false to determine how you want to react.


It's hard to think outside the box when I'm trapped in a cubicle.
 
How do I read the parameter in JSP when a false or true is passed through the javascript?
 
I do not know JSP so could not say.
Can you call your JSP functions from Javascript? If so then you just do the function call passing the value and it should be available to JSP.

If you can call JSP functions from javascript then you could just change the onclick to do the function call directly rather than calling a separate JS function.



It's hard to think outside the box when I'm trapped in a cubicle.
 
Doing a quick search on passing parameter from javascript to jsp turned up a few ideas.

It sounds to me as if your JSP page pulls the data down and then dynamically builds the HTML elements (like the select box) so I would think the best method would be to use the onclick event to do one of the following.
1. Submit the form back to itself so the JSP runs again and have the JSP script check the submitted form values to see if the checkbox had been checked and then respond appropriately.

2. Call a javascript function to change the current URL of the page to the same page but with a value passed on the URL that your JSP page can read like:

As I said I do not know JSP but if it is like other server-side languages all of the JSP code is going to run first and then finish so your Javascript will not be able to call the JSP directly so you just pass the value as a form field value or as a value on the URL and check for it when your JSP runs.


It's hard to think outside the box when I'm trapped in a cubicle.
 
It sounds like you'll need to use AJAX to handle the communication with the database and updating the page without submitting and reloading.

Lee
 
Nuts, I missed that. You DID say on the same page.
trollacious' suggestion to use AJAX will work. Your javascript can make a call to a separate page to perform the new query and return the results to the javascript function which you can then use to replace the options in the select box.

However, if you do not have a lot of different possible options you could load the options in a Javascript array when you read them from the database and use Javascript to read that array and populate the appropriate values as needed rather than having to go re-query the database.

How do I pass a checkbox parameter to JAVASCRIPT and back to the JSP page again everytime the checkbox is checked.

Using arrays you would not really go Javascript to JSP. The JSP would run and create the Javascript array and then the onclick event would call a Javascript function to alter the content of the select box without ever running additional JSP code.

Using AJAX your Javascript can call a JSP page in the background (without reloading the current page) to give you data to insert into the select box then the Javascript would re-populate the options of the select. The currently displayed page would not run any additional JSP code though and the data returned from the AJAX call would have to be worked with in Javascript.


[blue]It's hard to think outside the box when I'm trapped in a cubicle.[/blue]

In a society becoming increasingly self-focused it is important to acknowledge the unselfish actions of others so they will feel encouraged to continue such actions. So please take the time to acknowledge others beneficial actions whether it be waving t
 
Since I'm not familiar with AJAX I like the idea of

"2. Call a javascript function to change the current URL of the page to the same page but with a value passed on the URL that your JSP page can read like:

</select><INPUT TYPE=CHECKBOX NAME='All' oncliCK='myfunction(this.checked);'>All L Numbers<BR>


Do I even need to use Javascript for this? Can I just reference the URL?
 
Well that solution would require re-loading of the page which you had indicated you did not want to do.
It would require a minimal amount of Javascript in order to handle the onclick event in which you can submit the form or to cause the URL to change to the same page with the parameter added on to tell your JSP what options to load.

To do it on the same page you have to either use AJAX to retrieve the data live or have your JSP file create a javascript array of the data that you can use without needing to reload the page.


[blue]It's hard to think outside the box when I'm trapped in a cubicle.[/blue]
In an increasingly self-focused society it is important to recognize the unselfish actions of others so they will feel encouraged to continue such actions. Please give acknowledgement to those who aid you whether it is waving to the person who let you out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top