Still working on my drive assignment. I have created and populated an ArrayList in a servlet, and set it as an attribute to forward to the jsp where I want to display the values stored in each item in the ArrayList. I can't figure out how to access the individual items in the ArrayList using declarations, scriptlets, or expressions. I have read that attribute returns an object, which must be cast into the type of object I need, so I tried to code something like: <%=request.getAttribute( "videoList" ) %> <%!ArrayList videoList = (ArrayList)request.getAttribute( "videoList" ) ; %> but either my compiler complains, or the page throws an error when the forward is sent to it.
I was trying to use a for loop to iterate through the ArrayList and print out the Strings stored in each item in a table format. Is it possible to code this in the jsp itself? I was also thinking that good MVC would be to put all of the java code possible in the servlet class, but then I can't seem to find a mechanism for getting the data from the servlet to the jsp in a form I can use to fill out the table. Any hints? Or somewhere to look for examples? I don't want to use beans, if I can avoid them, since I haven't studied them yet. I'm hoping there is a solution to this just using jsp and servlets.
If you are using scriplets, something like the following would be most appopriate:
Never use the <%! format to declare variables for a page lest you introduce thread-safety issues.
I can't seem to find a mechanism for getting the data from the servlet to the jsp in a form I can use to fill out the table. Any hints?
You can avoid Java on the page through use of the JSTL. That way, you wouldn't even need the scriplet fragment above. The JSTL and EL reference scoped variables (aka attributes) directly without the need to copy them into scripting variables.
Sometimes I just need another pair of eyes to look at my code. I thought I had done something like your example, but when I went back to take that exclamation point out, I noticed I was declaring videoList to be of type VideoList, and trying to cast the attribute to (VideoList). I must have looked at that code for hours without noticing that it should have been ArrayList! Ay me! So having corrected that dumb-ass mistake, I can work on the for loop. I don't think the assignment requires scriptless jsp's, so I'm thinking I'll just put the for loop in the jsp, because I can't come up with another way to put each String in the proper cell of the table. I haven't done much coding that requires thinking about thread safety issues. Exactly why is the <%! %> declaration any less safe than a scriptlet? I thought they both just got turned into java commands in the class file. What is the difference in how they work?
[ May 04, 2006: Message edited by: Carol Murphy ] [ May 04, 2006: Message edited by: Carol Murphy ]
Exactly why is the <%! %> declaration any less safe than a scriptlet? I thought they both just got turned into java commands in the class file. What is the difference in how they work?
Whne you use the !, its contents become class-level declarations. Since only one instance of the servlet created on behalf of your JSP will be shared across multiple threads, these declaration will be shared by all threads simultaneously.
Joined: Mar 15, 2001
Eureka! I now have code for Servlets3b that functions as desired. Thanks! Back to the thread-safe issue. So a declaration shared by all threads is a bad thing. Is that because any one of those threads has the potential to alter the code? I've seen an example in HeadsUp Servlets and JSPS that shows how a variable input by one client can be changed by another thread, but since this is just declaring a variable and there is no way or reason to alter that variable, would this be an case where I could consider the code thread-safe? Or would there be a problem if more than one client was trying to access the ArrayList at the same time? Hmmmmm. I guess it's time to start studying up on thread issues.
Let's take the example of a simple String variable:
Because the ! declaration was used, this becomes an instance variable of the JPS's servlet.
Then let's say at the top of the JSP, you set it from a request parameter:
Then later in the JSP, you reference it in a table cell:
Now, someone out there hits your JSP page with the query string ?whatever=Hi+there!
So the string "Hi there!" is set into xyz.
Just a millisecond later, someone else hits the same JSP with ?whatever=So+long!.
xyz gets changed to "So long!"
Now it's a race condition! Will the first hit on the JSP reach the table cell declaration before xyz gets changed? If not, even though the query string specified "Hi there!", the user will see "So long!".
You have entered the thread-safety crap shoot zone!
Now imagine this page being hit by tens or hundreds of users all at about the same time. Whoopie-doopie! We have'um fun! [ May 05, 2006: Message edited by: Bear Bibeault ]