• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading multiple request attributes from servlet in JSP

 
Sheet Pangasa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to JSP and have a requirement which I am not able to resolve. In my servlet i have about 20 session variables from folder1 to folder20. I want to read these in my JSP page using loop but i am unable to do so. Can you please guide me to the right directions.

Servlet


JSP
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheet Pangasa wrote:I want to read these in my JSP page using loop but i am unable to do so.

What's stopping you? You really should say more than just "it doesn't work".

But in this case, it's clear that your EL syntax is wrong.

But rather than fixing the EL syntax, you're whole approach is poor. If you have 20 elements to pass, why are you creating names for each of them rather than passing them in a List?
 
Stefan Evans
Bartender
Posts: 1784
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should also be noted that javascript is executed client side and Java/EL is executed server side.

You were looping in javascript in the function addFolders.
That would NOT evaluate your EL expression on each loop.
Rather the EL expression is evaluated at page execution time, and then returned to the page as static text.
At THAT point it starts running the javascript, and looping - but the java evaluation has already happened, and won't redo it.

As Bear said, it would make more sense to have the attribute as a list, and use a JSTL <c:forEach> tag to evaluate the list on the server.



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for mixing JavaScript and JSP, you need to read this article to understand how JSP works and why they cannot interoperate. Also see the JspFaq.
 
Sheet Pangasa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. And I agree that the approach is poor. This is the first time I am writing anything in JSP and Servlet and managed to do only this much for a "one off" requirement.

After going through your suggestions I did some research on JSTL and wrote the below code and I am able to get the folder list in my JSP page and I am pretty sure even this approach would be poor. But my requirement is to add these variable to a drop down and I don't seem to get away with the brackets in the beginning and end from the output of arrayList because of toString being called implicitly (this is what i read somewhere). Would taking care of the brackets in the servlet be a better approach or in JSP and if so any idea on how to do that?



After this code, all the items are added to the dropdown properly except the first and last element as they have brackets appended to them.

first item = [folder1
last item = folder20]

thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is folderList a string that you haver to split? Why is it not a List of the individual elements you want to display?

Rule #1: don't make the JSP do any work that the controller should do. The data passed to the JSP should always be a format suitable for use in the JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why are you using JavaScript to create the options rather than just constructing straight-forward HTML? You're making things much more complicated than they need to be.
 
Sheet Pangasa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:And why are you using JavaScript to create the options rather than just constructing straight-forward HTML? You're making things much more complicated than they need to be.


Well i think I should have clarified the requirement before asking for suggestions. Sorry about that.

Ok so I have two dropdowns on my page. On changing the value in first dropdown a servlet is called which runs a script at the server and stores the output (list of folders in this case) in a text file. I have to read these values from the text file and populate the second dropdown. As i mentioned in previous post as well that this is the first time I am using JSP and Servlet, i could not find a better way to do this (due to my lack of knowledge on the subject).

Anyway thanks for your suggestions.. i will figure out something.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would use Ajax for that.
 
Sheet Pangasa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You would use Ajax for that.


Would have already used that if I had knowledge in this field. Probably would not have come here to ask such silly questions :)

Btw, i have resolved it by using the below code in servlet itself (dont care much if it is right approach or not but it is working for me):



Thanks for your time.
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sheet Pangasa wrote:
Bear Bibeault wrote:You would use Ajax for that.


Would have already used that if I had knowledge in this field. Probably would not have come here to ask such silly questions :)

Btw, i have resolved it by using the below code in servlet itself (dont care much if it is right approach or not but it is working for me):



Thanks for your time.


That's the problem, that you don't care, but being honest that code sucks, you should not put 20 attributes in the request(*), specially not 20 variables that come from a List!! So you iterate the List in your Servlet to set the request attributes and then you iterate it in the JSP to put them in the option list... looks overkilling to me, why don't you just say in the Servlet:



and then in your jsp: something like:


So you create directly the option list and not lazily in JavaScript.

Best solution is as Bear said, use AJAX but in case you are going to refresh the full page I think this approach is much better.

(*) I mean in this specific case.
 
Sheet Pangasa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Albareto McKenzie wrote:
That's the problem, that you don't care, but being honest that code sucks, you should not put 20 attributes in the request(*), specially not 20 variables that come from a List!! So you iterate the List in your Servlet to set the request attributes and then you iterate it in the JSP to put them in the option list... looks overkilling to me, why don't you just say in the Servlet:


Thanks for taking out time and going through the post. I do take suggestions seriously and try to implement them as long as it does not make me put so much time that my current work suffers. After the very first suggestion I implemented what was suggested and instead of creating 20 variables out of the list, i set the list itself in the request parameter. Below is the code. Feel free to provide more suggestions if you find something wrong with the approach again:

in Servlet:



in JSP:


Thanks.
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's correct, just one question, why do you create the list in JavaScript and not directly in the page? Do you have any requirement to create it that way?

You could create it very easily directly in your body:
 
Sheet Pangasa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The listbox is already created. But i need to populate the items of this listbox based on the value selected in another listbox. When an item is selected in the first listbox a servlet is called and a lot of processing takes place. At the end of the processing a file with folders is created which i read in my servlet and store the output in the arraylist variable. The page gets refreshed and the request is dispatched back with the variable value. This variable is then read from the jsp and the values are created in the second listbox.
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still you are rebuilding the full page so I don't see the reason not to write the html directly and do it in a JavaScript function that will look in the generated html like this:

 
Sheet Pangasa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, i understand it now. Will try what you have suggested and get back. Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uggh! Never wait until the client to do things that can be done on the server. If you know what needs to be created while the JSP is executing, create it there as static HTML. Generating JavaScript to create it later is not a good plan. The only reason to do this sort of thing is JavaScript on the client is when you have to.

Is there a reason this would have to be done in the JavaScript?
 
Eric Blizzard
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for sharing

cc.jpg
[Thumbnail for cc.jpg]
good mood
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic