wood burning stoves 2.0*
The moose likes JSP and the fly likes Reading multiple request attributes from servlet in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Reading multiple request attributes from servlet in JSP" Watch "Reading multiple request attributes from servlet in JSP" New topic
Author

Reading multiple request attributes from servlet in JSP

Sheet Pangasa
Greenhorn

Joined: Jan 02, 2013
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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

Joined: Jan 02, 2013
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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

Joined: Jan 02, 2013
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

You would use Ajax for that.
Sheet Pangasa
Greenhorn

Joined: Jan 02, 2013
Posts: 7
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

Joined: Apr 08, 2009
Posts: 298
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

Joined: Jan 02, 2013
Posts: 7
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

Joined: Apr 08, 2009
Posts: 298
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

Joined: Jan 02, 2013
Posts: 7
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

Joined: Apr 08, 2009
Posts: 298
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

Joined: Jan 02, 2013
Posts: 7
Alright, i understand it now. Will try what you have suggested and get back. Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

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

Joined: Jan 22, 2013
Posts: 1
thanks for sharing




[cc.jpg]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading multiple request attributes from servlet in JSP
 
Similar Threads
Have the rules for EL in a JSP changed?
Regarding war creation in Maven
Displaying data in servlets
servlet programming problem in Eclipse
Moving resources under WEB-INF