Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Looking for a good way to relay information to my JSP's

 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I'll preface this by saying I'm new to servlets/JSP's. Alright, here goes:

I have a JSP that successfully iterates through a given list of objects and displays the pertinent information. But, for each of those objects, lets call them Requests, there is one more piece of information I would like to display and this information is not directly stored in the Request. Instead, every Request holds an ID number identifying another object, a User, in my database. What I want is for the JSP to display not just the Request's information but also some of the associated User's information.

Here's what I think might be a good solution. After my servlet obtains a list of Requests from my database, it will iterate through them and find the associated User object. It will then bundle the User and Request objects together in some kind of Tuple object and send the list of Tuples down to the JSP instead of a list of Requests. I think this will work fine but I wanted to see what other people thought of it before I started implementing.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you have a good point there. Implement it and let us know about the outcome.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65534
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If by "tuple" you mean a bean that contains properties for the Request (not to be confused with an HTTP request, I assume) and the associated User (sounds like a one-to-one relationship, right?) might be the easiest approach. The list would contain these beans so you can iterate over the data easily in the JSP.

Personally, I would model the data better in the ORM style where the Request has the User rather than a user id, but that's a matter for the model and frameworks like a JPA implementation.

Remember, always make the JSP as stupid as possible. Keep any complexity in the model and business layers.
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, I'm thinking of implementing a generic Tuple<X,Y> bean class rather than a bean class holding all of the properties I want as member variables as I think you mentioned. That way I can reuse the class for other purposes.

The Request to User relationship is actually many to one so I'm not sure if changing the database structure is in the cards. Let me know what you think.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65534
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is a poor approach. There is no reason not to be explicit about the properties.

If you're going to go that route, you might as well just use a Map rather than create a class that mimics its capabilities.

Again, make the JSP easy and stupid. Being clever rarely is the right approach.
 
Amar Saikia
Ranch Hand
Posts: 43
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
list of Requests from my database
If your list of objects is not huge in terms of size, I think you should follow as Bear suggested i.e pass all the Objects to JSP instead of passing some ID. However, if your list of Objects is huge, then you may think of passing only IDs as part of request and using the IDs in JSP, load the corresponding Objects using Ajax calls.
 
Kesava Krishna
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edward, Your approach is correct, but I will add a little more. Upon getting the necessary information from database create a VO(value object) with only getters and setters. Try to populate the VO with the information from requests and user objects. I am assuming here request and user has one to one mapping. Request here should not be confused with http request object, if my assumption is correct. Now these list of VO objects should be passed to jsp which is a good design.
ORM frame works should be used only if there is any complex data model involved. But in your case I don't think there is a complex data model involved.
I would rather suggest to go with your approach with my suggestions.
Try this and let me know your findings.
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice guys. Yes, it does seem like the list of Tuples would be mimicking a HashMap. Maybe I should use a HashMap then. It seems strange to me to create an a class for the sole purpose of to sending properties explicitly to this specific JSP when my User and Request classes already hold this data and can be sent down the JSP's themselves. Is it really the best approach?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65534
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward Nunez wrote:It seems strange to me to create an a class for the sole purpose of to sending properties explicitly to this specific JSP

It shouldn't; that's what classes are for. If that makes you uncomfortable, then use a Map. But I've created lots of classes whose sole purpose is to model data for easy consumption by a JSP (or other template).
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edward,
Why don't you implement what you have on hand and develop from there? You could explore other solutions afterwards.
Regards
 
Edward Nunez
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright cool, thanks again guys. I know there are a lot of ways of doing what I'm trying to do but it's always good to have experience on your side.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!