This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet/JSP problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet/JSP problems" Watch "Servlet/JSP problems" New topic
Author

Servlet/JSP problems

Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Hi,

Hopefully this isn't a complicated problem. I'm making a cd store web app, and I have all the music genre categories stored in my database. I'm using the MVC pattern where all my views are in JSP, including my index. I have a jsp file dedicated to just show these genre categories, which is included in my index and several other views. My problem is... when I start my application and it begins at index.jsp... I have no way of querying the database to get those category titles.
I don't want to hard code the titles in because I'm setting up the site so an administrator can login and add or delete categories and the site would update dynamically. I don't want to have the jsp file to connect and query itself because I want only my models to have access to the database, and my controllers to forward that info to the views.

Anyway this is possible?
H Wilson
Ranch Hand

Joined: Feb 23, 2005
Posts: 33
Originally posted by Jenn Person:

My problem is... when I start my application and it begins at index.jsp... I have no way of querying the database to get those category titles.


Oh, yes you do! You can use a bean ... with application scope.

<jsp:useBean id="myDatabaseQueryingBean" class="myPackage.myBeanClass" scope="application" />

Put your JDBC code in the bean and access the result set for display in the JSP with a tag from the JSTL!

Good luck!
Randy James
Greenhorn

Joined: Dec 28, 2004
Posts: 5
I'm not sure this is the best approach or whether it fits your code base, but here is what I've done in a similiar situation. In your jsp page, refer to a static fetch method in your server side code. For example, in your jsp:



This is probably too much logic code to put in a jsp but it works for us. I too would be open to a better solution if it exists.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

Your index.jsp doesn't have to contain the main page of your application. In almost all my web apps, index.jsp performs a redirect to the servlet controller responsible for displaying the main page. The controller performs any duties that the main page needs (such as fetching categories as in your case) in preparation for forwarding to the view component (JSP) of the main page.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
These are some good ideas... As for making an initial page that would call a 'loading' servlet to prepare the actual shopping page, I considered that. However I wanted the user to immediately be able to start shopping, even though one extra intro page wouldn't be a huge downfall.
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
I'm going to go with the beans, using the application scope, I think that should do the trick... I'll have to review that a little though!! Thanks for all your help guys!!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is no reason why you can't use a servlet as your welcome page.
http://simple.souther.us
shows 2 ways of doing this. In Servlet Spec 2.4, you can just do it directly from your welcome file list.

There is also a hack up there for pre 2.4 servers.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Originally posted by Ben Souther:
There is no reason why you can't use a servlet as your welcome page.
http://simple.souther.us
shows 2 ways of doing this. In Servlet Spec 2.4, you can just do it directly from your welcome file list.

There is also a hack up there for pre 2.4 servers.


So I'll set my index to a servlet in my welcome file list in my web.xml, and with this servlet... I would have it do all the preloading via info from models and forward it to my view which is my index.jsp file... ok I can do that.

At the same time as I mentioned before I want to be able to have admins log in and be able to delete or create new categories and have the option to add it to the live list of categories. So as soon as they save the changes, the list would update. How can I do that? Would it be that after the changes are saved, I could simply forward to that same index servlet which will reload the list?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you're caching the list in a singleton object or an application scoped object, you could give the object a "refresh" method that causes it to re-read the list from the database.

Have the update routine that the admins use call the refresh method on that object after an update.
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Originally posted by Ben Souther:
If you're caching the list in a singleton object or an application scoped object, you could give the object a "refresh" method that causes it to re-read the list from the database.

Have the update routine that the admins use call the refresh method on that object after an update.


Ok that sounds good. I have one last question then, how would I enable that refresh method? Secondly, as if I wasn't hopeless enough, I know how to save to a session. How do I make an application scoped object? WOuld that be through servlet context?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Jenn Person:
Ok that sounds good. I have one last question then, how would I enable that refresh method?

If the object has it's own DB functionality, then the refresh method would just re-read the list from the database. If the object only stores the list, then your refresh method could have arguments that allow you to pass the new lists in.


I know how to save to a session. How do I make an application scoped object? WOuld that be through servlet context?

Yes, ServletContext has setAttribute and getAttribute methods just like the request and session objects.

A context listener would be a good place to create the object and initially populate it. http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContextListener.html


GOTCHAS:
If you are planning to run this app in a distributed or clustered environment, you would need to find a way to sync up the context scoped objects.

If the database can be altered from an interface other than your webapp, you would also need to find a way to detect any changes made by the adminstrators.
[ March 22, 2005: Message edited by: Ben Souther ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet/JSP problems
 
Similar Threads
Caching dynamic content
jsp:useBean
Is This Switch Correct and Efficient?
How to create a dynamic index page
CachedRowSet?