Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

beans  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If several clients store information into a bean in a JSP, where the bean name is "details", then there will be a bean called "details" for each client, and I suppose each bean will exist in a different session, if we set scope="session" when using .

So when we want to display the results for ALL users in a JSP/servlet, how can this be done, since all the beans have the same name???
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without more details about what you're trying to do, it will be difficult for anyone in a forum to help you out.

From what you've posted, it sounds like you're trying to do something similar to what one of my demo apps does. The demo app, "SessionMonitor" creates a reference to each an object in each user's session when they log in and maps it to a context scoped objet. This allows an administrator to loop through all the sessions to see each client's information.
You can find it with this URL: http://simple.souther.us/not-so-simple.html
Look for SessionMonitor.

If you're trying to do something different, please provide some more details.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I'll try and explain my problem in a little more detail.
I have a JSP (requestform.jsp) that collects user information using FORM,
eg, first name and last name. When submit is pressed, a JSP(validate.jsp) is invoked, this makes sure the fields(first name,last name) are filled.

If the fields are correctly filled, then a bean call "details" is created.
Now, in the website, I need a servlet/JSP that displays an excel spreadsheet, displaying the information of ALL clients, when entered.

This is no problem to display the bean called "details" (bean is created under session="scope") in the excel document.

But when multiple clients access the site, then I think there would be multiple sessions being created, and also multiple beans are called "details".

My problem is displaying the all properties of ALL beans called "details" in the excel file.

I've included a fraction of my code below.
This code here is validate.jsp



And here is spreadsheet.jsp (I may use a servlet here instead later) needed to display the properties of the beans.




And also Info is just a just a normal class with private fields, first and last, with the appropriate set and get methods.


Any help is much appreciated. Thanks
[ August 20, 2005: Message edited by: colin shuker ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put an instance of HashMap (or a similar collection object) into context scope.

Whenever you create a "details" bean, add a reference to that map (the user's sessionID would be a good key).

You will now have access to every instance of "details" within the application. You can iterate through them to build your spreadsheet.

You will also want to create a session listener that will remove them from the map when someone's session expires.

This, by the way is exactly what the demo app I described earlier does.
Feel free to browse the code.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much, I have my SCP 1.4 certification, so I'm fine with collections, but I'm new to JSP and servlets. So I'm not quite sure what you meant when you were saying:

Put an instance of HashMap (or a similar collection object) into context scope.
Whenever you create a "details" bean, add a reference to that map (the user's sessionID would be a good key).

But If your demo app does a similar thing, I can try and figure it out.
Ok, I've just looked at it, and It's too complicated, I can't even get the EL to work using Tomcat(in netbeans)
I think its pretty hopeless


Thanks for your help anyway
[ August 20, 2005: Message edited by: colin shuker ]
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't there a simply way to put the beans into a Map or other collection, and then use them later in servlet??
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks, that makes a bit more sense, But I'm still a bit confused.
What is application? Do I have to I have to make the instance application of some class??
Also I suppose I need to instantiate session with:
HttpSession session=request.getSession();???
I'm not sure. I'm just trying to modify my code so I can fit in a Map to store the session IDs.

But I've just thought, if I create the map in the JSP validate.jsp(from my original question) then will I still be able to iterate through the elements(the beans) in the servlet/JSP which displays them?


Thanks again for your time, it is much appreciated.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by colin shuker:
OK, thanks, that makes a bit more sense, But I'm still a bit confused.
What is application? Do I have to I have to make the instance application of some class??
Also I suppose I need to instantiate session with:
HttpSession session=request.getSession();???
I'm not sure. I'm just trying to modify my code so I can fit in a Map to store the session IDs.

But I've just thought, if I create the map in the JSP validate.jsp(from my original question) then will I still be able to iterate through the elements(the beans) in the servlet/JSP which displays them?


Thanks again for your time, it is much appreciated.



"application" is one of the implicit variables provided by the container for you. It points to the servlet context. Objects bound to the application variable with application.setAttribute are said to be in context scope and are available from all pages.

You don't need to instanciate the session object. The container does that for you as well. You can access it by using the "session" varible - also an implicit object provided by the container.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, I think I've almost done it.
I've entered the above code (more or less) into validate.jsp(the JSP which validates the user information, and creates the beans.

But as I said before, suppose I want to display the results of all beans in a servlet, say SpreadSheet.java, then I would write the code as something like this:



But this servlet doesn't know what the HashMap detailBeans is, since it was created in the validate.jsp JSP, so how do I get around this???

Hope you can help, thanks.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But this servlet doesn't know what the HashMap detailBeans is, since it was created in the validate.jsp JSP, so how do I get around this???


Chapter 13 of this book does a good job of explaining the various scopes that you can bind your objects to in a Servlet/JSP container for sharing objects across requests. Look, in particular, at section 13.4.
http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter13.pdf
[ August 20, 2005: Message edited by: Ben Souther ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!