• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Checkbox

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
I have a jsp file; in spme part of file, I created a check box like this:



I check whether the checkbox is checked or not in a Java Script function like this, and send the value of filter to my Java code:



Communicates with Java code with this method:




The submitSearch() is called only once.

Up to here everything works fine.

In some other part of jsp file, I try to go to some other page:



The ${minBudget}, ${maxBudget}, ${startResult-pageSize} values are available, but the filter value is not? This is the url I get which has no indication of filter variable?

http://localhost:8080/search/0/10000/21/

It should be something like:

http://localhost:8080/search/0/10000/21/false

The Java code method that controls this part has been modified accordingly.

Does anybody has any idea? Any help is greatly appreciated.


 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course that's not going to work. The filter variable is a JavaScript variable. How could you possible expect to access it in the JSP before the HTML page is even sent to the browser?

Sounds like you need to read this article to understand how JSP operates and why that is impossible.

You'll need to add the filter value to the URL in JavaScript.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Then I'm wondering how the values of minBudget and maxBudget are available thru out the jsp file? They are also defined in the same funcion:

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

R Ta wrote:Then I'm wondering how the values of minBudget and maxBudget are available thru out the jsp file?



They must be available as scoped variables set up for the JSP. The JSP is certainly not accessing the value of JavaScript variables that will not exist until some time in the future and on a completely different system.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I search for those variables thru out the jsp file, I cannot find anywhere which they are set up...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Conventionally, scoped variables are set up in the controller.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much. Your point, solved the problem.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more thing... anytime the page loads, the checkbox is unchecked. How can I modify the code, that with every page load, the checkbox appears checked - if it was previously checked - and appears non-checked - if it was previously non-checked?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add the checked attribute to the checkbox when you want it to be checked. Easy to do with the ternary operator or <c:if>.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I modified the code like below, but the checkbox is still un-checked regardless of whether it was checked before or not. Any idea?

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, document.search.filter.checked is JavaScript. You cannot use it to make any decision in the JSP code. Did you read the article?

Is the information you want to use to decide whether the control should be checked or not part of the previous submission?
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Is the information you want to use to decide whether the control should be checked or not part of the previous submission?

Yes, user enters two numbers and also he can check the checkbox or leaves it un-checked. But if he checked it, after I load the page with the result, I want the checkbox to be appeared as checked.

Actually my code looks like this now but still no result:

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there aren't any redirects between the submission and the redisplay of the JSP, the value of the parameter will be available on the param built-in scoped variable.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, using <c:choose> is excessively wordy for this usage. It'd be terser to use <c:if> or better yet, the ternary operator.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, checked="yes" is a bad bad idea. It makes you think that checked="no" will make the checkbox unchecked but it won't. If there is a checked attribute with any value on the control, the control will be checked. Use one of checked or checked="checked".
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it, the problem was solved. Thanks so much.
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I was working on your comment of the "param" built in... and that solved the issue, THEN I saw other comments of yours...

> Also, using <c:choose> is excessively wordy for this usage. It'd be terser to use <c:if> or better yet, the ternary operator.

I looked into web and noticed that if I need to have an <if><else> condition, I should use <c:when and <c:otherwise>, that's why I used that way. Since its in fact an <if, else> condition, I don't know how to use only <c:if Or the ternary operator.

I modified my code to set checked="checked".
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But you don't have an if/else condition -- you have an if condition. You want to include the attribute if the condition is true, and do nothing if not. So there's no else part.

Personally I'd use the ternary operator along the lines of: ${param.whatever == 'on' ? 'checked" : ''} (substituting whatever test you are using to turn the attribute on.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From a maintenance point of view, if you have code like this:

then you are generating what's called "Technical Debt". In other words, it was easy to write that code but you will pay later when you have to change something in the big lump of code which you have two slightly different copies of.

I can attest to this personally because we used that anti-pattern in a big way over the last few years in our web applications, and it's one of the things we are fixing in a wide-ranging cleanup. Ternary operator? Absolutely!
 
R Naijat
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Bear an Paul,
Thanks so much for the comments. I tried to apply what Bear mentioned but looks like I don't know what I'm doing! as it completely crashed my page!!!

So I modified the code to its original - not-efficient! - way to have my page back. This is my code at this point; if you have enough patient, please let me know the modified version that works with only <if> or the ternary operator, if not, I leave it as is, until I learn more! :-)

 
Acetylsalicylic acid is aspirin. This could be handy too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic