Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

iterating hashmap in jsp using value of dropdown as key  RSS feed

 
shriya hegde
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a a hashmap in request attribute which is available to jsp file where i have a dropdown. Hashmap kay= value of this dropdown.
As and when I select CLT i want to perform some opertation depending on value associated with that key in hashmap.

How can I achieve this
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question isn't real clear to me (what is CLT?) but that sort of user interface interaction sounds like a job for JavaScript.
 
shriya hegde
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for not being clear. I mean to say when I select any value from dropdown , that value should be passed as key to hashmap and get the value.

What I am trying for this is
on selection of dropdown value, i am calling javascript function and pass selected value of dropdown as parameter. Inside javascript function I have written scriptlet to do hashmap.get();
But I am not able to send that dropdown value as parameter to hashmap.get method. There is no form submit between dropdown selection and calling this function

Any suggestions?
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I'm still trying to sort this out. Why not just build your dropdown based on the keys and values in the map? Then use the change event in JavaScript to get the selected value.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So let's say your servlet looks like this:

Your jsp will look like this:


That will give you a dropdown (it's a select/option element to be precise) that has the keys and values of your map. From here you should be able to use JavaScript to detect a change in the select/option control and get the selected value. I recommend JQuery, look at the .change event.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shriya hegde wrote:Inside javascript function I have written scriptlet to do hashmap.get();


1) Scriptlets? In 2014?

2) Scriptlets run on the server, so putting them inside a JavaScript function to execute on the client makes no sense. Please read this article to understand how JSP works.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What I am trying for this is
on selection of dropdown value, i am calling javascript function and pass selected value of dropdown as parameter. Inside javascript function I have written scriptlet to do hashmap.get();

Remember a jsp page is translated to a servlet before producing an html page that is displayed at the client side. So your java code(which is executed first) that you have put in the javascript function will no longer be there after the html page is rendered. If in your dropdown list the actual value of any option element is equal to its corresponding displayed value, i see no need of doing further work. But if these two values are not the same, eg:<option value="key">value</option> where key is different from value, then you can append the value to the key using a separator so that in your javascript function you can easily break the actual value of your select element value and retrieve the displayed value. Eg: using "_" as seperator, you will have ..<option value="key_value">value</option>
Hope that will help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!