• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we use jsp stuff in onklick events

 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Assume I have a table of N rows with two columns. I will have one checkbox on column-1 and zero or more on column-2. Column-1 may represent categories and column-2 represents associated things. Just like below:


Now user can select EJB and Hibernate of first row, HDTV of the third row, and no value from second row and submit

Now I'm looking at adding EJB and Hibernate to a list and add that list to a map with Books as Key. In the same way HDTV to a list and Electronics as the Key.

I tried to accomplish the above thing with the below code, but I actually ended up with a Map with three keys, Books, Foods and Electronics and for each corresponding key, all the elements in the list.



Can some one guide me about this problem.

[ August 07, 2008: Message edited by: Schandha Ravi ]
[ August 07, 2008: Message edited by: Schandha Ravi ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the HTML that is being sent to the browser? Is it what you expected?
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Bear,

I'm getting the page displayed as required. Do you smell anything wrong in the code?

The text that is being displayed may differ from what I mentioned.
[ August 07, 2008: Message edited by: Schandha Ravi ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a lot of Java in that JSP (bad bad idea) so it's hard to see what's going on.

Are you expecting JSP code to execute as a result of the onclick event? If so, that can't happen.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

There's a lot of Java in that JSP (bad bad idea) so it's hard to see what's going on.

Are you expecting JSP code to execute as a result of the onclick event? If so, that can't happen.


Bear, I too agree with you regarding the usage of that much java in jsp. I'll try to use Expression Language and tags instead.

But as you mentioned, if we can't get JSP code executed as a result of onClick event, can you suggest me an alternative to accomplish the same.
I should be able to send the servlet with information about categories and the corresponding items selected , when submit button is clicked.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the event handler you can communicate back to the server by submitting the page or by making an Ajax call.

You do understand why JSP in the same page can't be executed as part of a JavaScript event, right? If not, this article might help.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Bear.

I do have couple of constraints here.

1) I can't use Ajax. Its out of scope and not considered at this stage in project.

2) Calling server in the event handler, sounds good, but it results in multiple trips to the server as there could be N number of check boxes and multiple checks.

Initially I thought, what I was looking at is quite simple but apparently that is not the case here .

How would these typical advertisement pages work? I mean, all those web pages, where in customer is opted to select one or more options for multiple categories. Can't we cache all the selections and corresponding categories ,map each category to the collection of items selected, bundle all of them together and send to servlet all at a time at one single click.

Am I over ambituous
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't really answer, because I really don't know what you are actually trying to achieve. What is it that you'd like to have happen? Without going back to the server, you are limited to what's available on the client.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,

Let me try to make my question more clear here.

I get the following details from the servlet as session attributes
A) Categories which is an ArrayList
B) Items which is a Map. Each element of Category has a corresponding Arraylist of items.

Categories - Books, Foods, Electronics
Items -
Books - HFSJ , RJB, Hibernate
Foods - Pizza, Muffins
Electronics - HDTV, Laptops

In a table of two columns, categories goes on first column, Items on second column and each represented by a check box.

Now as a user we can select any of these checkboxes to show our interest. Say I'm intersted in Pizza and Laptops. So I select Pizza and Laptops and it implies that we selected Foods and Electronics too.

Now upon submit the form only once, I should be able to send only
Foods - Pizza
Electronics - Laptops.

We may be able to navigate through request parameters in servlet , but it does not tell us what maps to what. There should be a way servlet should map Pizza to Foods and Laptops to electronics.

This is what I'm trying to achieve. I know that I might be complicating things here with my logic. But if you can guide me anything better, that would be great. Thanks in advance.

Note : We would not be aware what categories and items are displayed in advance. Those are dynamic.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't clear to me why the client has to tell the server that Pizza is in the Food category, when the server already has that information.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

yes servlet would have the info that Pizza belogs to Food. But Food may contain the other information like Muffins or Bread. But user may not be selecting them. And more over for the servlet, also would not generate this information. It gets from some other service as a collection. So , I'm not able figure out that ok out of these many items, not every thing is selected, only few of the choices are selected and those are related to one particular category.

If my question is too confusing, would you mind letting me know, how do I get problem solved. I'm sure there is some link which I'm missing.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

After going through the entire conversation, I get a feeling to have my below code checked, for its design. Would some one mind to look into this and provide me the feedback. I used few jsp codes in onclick events. I should say this is working fine, but wanted to see if it is appropriate or not.

As I mentioned earlier , I have this table with two columns 1 and 2 with check boxes present. For each row, I should have column-2 displayed only when the check box of column-1 is clicked. When it is unchecked , column2 should be invisible. To achieve this, I coded as below


With the above code, I was able to get what I was looking at.

Would some one provide feed back on this. I was not sure, if I need to open a new thread for this. I thought since both of my issues are related to jsp in javascript, thought of putting in the same.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

As you mentioned

It isn't clear to me why the client has to tell the server that Pizza is in the Food category, when the server already has that information.


It makes me think that, I can try to compare the request parameter values with the values available to the servlet. I believe this may lead to solution. However, I'm just thinking if it would make my servlet fatter (Ofcourse I do not want my jsp fatter too ).

But I'm just curious to know if there is a way we can bunch all the selected values as one single entity and send them as a request parameter.

I'm extremely sorry If I'm asking the same question again and again... still a long to go for me in this technology
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're asking how to generate HTML and/or Javascript which has checkboxes or radio buttons which act the way you want them to act, then this question really shouldn't be in the JSP forum. Especially since you would rather not look at server-side solutions.
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your point Paul.

I'll try to look for solution from server side.

By the way, its not that I don't want to look at server side, but rather curious to know the possibility at client side.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!