Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

annoying expression  RSS feed

 
Carol Murphy
village idiot
Bartender
Posts: 1211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I'm still working on the cattledrive assignment. I've got an ArrayList populated with video objects that I have forwarded to my jsp where the ArrayList is iterated through, data extracted and printed out in a table using scriptlets and expressions. The code works, but the nitpicker would like me to put as much of the logic in the servlet as possible. I am stuck on how to do this. I'm thinking if I create the table in the servlet class (perhaps by making it an inner class) and forward the completed table to the jsp as an attribute, then I can use an expression to print the table in the jsp. Is this possible? Or is there an easier solution? I guess I want to avoid any iteration logic in the jsp, and I can't think of any other way to put data in a table unless the table is sent already intact from the servlet. I'm just not sure how to go about it.
[ May 25, 2006: Message edited by: Bear Bibeault ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First time I hear that kind of requirements. Why would you create the table in the servlet. Avoid scriptlets and use JSTL instead. A forEach loop in the JSP sounds fine to me.
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The table layout is part of the view, and as such should be on the JSP.
You should not be having html code mixed up in a servlet controller class.

Sounds to me like you have it right
Servlet:
- retrieve data into list of objects (via Data Access object?)
- forward list to JSP
JSP : iterate through list, and display the results.

Is your JSP doing any more than that?
For instance calculations/decision making on the Video class?

Can't really say much more without knowing the assignment/seeing some code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I concur with the previous posters. Presentation should be isolated to the JSP. That includes iteration logic to create a presentation construct from the list of data.

I think I understand where you're coming from, but don't look at "display support logic" in the same way as, say, the logic to obtain the list of data from the DB. That most certainly does not belong in the JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One simplistic, but fairly accurate, way to look at it is: if you find yourself building up HTML, JavaScript, CSS or other client-side constructs in Java code, it's probably something that should be in the JSP. Similarly, if you find yourself putting code in the JSP that's not there to help build such client-side constructs, it probably doesn't belong in the JSP. (There are exceptions, of course; classic tag handlers for example).

I also concur that the JSTL is far preferred over scriptlets. Generally, if you find yourself trying to do something that the JSTL or EL doesn't support, it's probably misplaced.
 
Carol Murphy
village idiot
Bartender
Posts: 1211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a straight forward cattle drive assignment. The task is to use the given code and supply two missing pieces, a jsp to display a list of videos, and a servlet class that receives form data from a given jsp, adds that data to an ArrayList, and then forwards the ArrayList to the jsp which will display it.
While my code works, and I like it just fine, the nit-picker would like me to take the iteration logic out of the jsp and put it in the servlet class. I'm looking at this as an alternate solution that I would like to find. I am having trouble coming up with a way to implement her suggestion. Once I figure that out, then I can make an educated argument as to which solution I like better.
There's more than one way to skin a gopher. (My cat is watching me type.)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I didn't catch from your first post that "the nitpicker" wasn't you!

Some ways I can think of to move the iteration logic off the JSP are:

1) build the markup for the list in the controller servlet
2) write a custom action (tag) to emit the list
3) use Ajax to retrieve the list and add the list to the HTML DOM using DHTML

As discussed, (1) is a bad architectural move and I cannot imagine why anyone would try to make you do that.

(2) is a common method for abstracting common elements that will be used multiple times.

(3) is a bit advanced and I'm not sure that's the right way for you to go at this point (and actually has little to do with JSP).
 
Stefan Evans
Bartender
Posts: 1834
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just saw the related conversation you are having in the cattle drive Forum.
http://www.coderanch.com/t/5101/Cattle-Drive/Okay-but
Particularly this comment:
Originally posted by Marilyn de Queiroz:
It seems to me that a few <tr>'s and <td>'s in the servlet are better than scriplets in the jsp.

I may not necessarily agree with it 100%, but if thats what the requirement is, I'd say go along with that. Welcome to the world of requirements.


So you currently have a JSP that works, but it has scriptlet code in it.
Aim: Remove all scriptlet code from the page.

My suggestion:
- Follow the instructions. Write a method in the servlet which will generate the table code, and then pass that String as an attribute to your JSP.

- Then also rewrite the JSP page using JSTL/EL - replacing the scriptlet loop with a <c:forEach> tag on your page.

Compare the two. The whole point of the cattle drive is a learning experience.
 
Carol Murphy
village idiot
Bartender
Posts: 1211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did it! After a broad hint from the nit-picker I was able to put all of the logic for creating the table rows into the servlet code. Without giving too much away, it involved figuring out how to put the html into one long String and sending that as an attribute to the jsp. Making it work took some effort though, because creating the String was a little tricky. I kept getting duplicate rows in my table and I couldn't figure out why at first!
This solution is definately more elegant than my first solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!