Hi, I'm trying to make a start on the MVC Model 2 approach with a simplish JSP/Servlet application. I'm looking for tips for best practice.
I have a main controller servlet, and various JSP views. When the controller servlet is first called (let's say we pass action="start" in the request to the servlet to let it know that this is the entry point), I can gather info from the request about the user. I only want to do this once, then store this information and make it available to the JSP views as required.
I could store this info in session variables, but I thought it might be tidier to store it in a bean object. However, my experience of using beans is very basic. If I initialise a bean in the servlet, what lifespan will it have? Just the session? (There is a new instance of the servlet for every session...?) To access the bean from the JSP page, would I use the JSP useBean tag, and if so, with what scope? Or should I save the bean to the session in the servlet, then retrieve it from the session in the JSP pages?
Also, is it reasonable to just intialise the bean when the web application is first accessed (action="start"), or should I be checking at every stage to see if the bean is initialised, and if not, start the initialisation procedure?
Sorry if these are basic issues, but i don't have a good overview of the process.
If I initialise a bean in the servlet, what lifespan will it have?
Only as long it is referenced, just like any other java object. If you create a bean in the servlet, and then don't do anything else with it, it will go out of scope and made available for garbage collecting when the servlet ends execution.
There is a new instance of the servlet for every session...?
No. There is one instance of the servlet per web application.
Or should I save the bean to the session in the servlet, then retrieve it from the session in the JSP pages?
If you want the bean to be available to all JSPs in the session, this is the way to do it.
You would the useBean tag on the JSP to retrieve the bean if you are writing old-fashioned JSPs with scriplets, or EL expressions and the JSTL if writing modern scriptless JSP pages.
Also, is it reasonable to just intialise the bean when the web application is first accessed (action="start")
Only if the data you are storing is to be identical for all sessions/users and all JSPs.
I'm also a little concerned about this "start" action. Are you actually depending upon an external hit in order to initialize the application? If so, I'd highly recommend re-thinking that. A context listener would be a much safer approach.
If however, by "start" you are referring to an action more along the lines of a user login, then it may be ok. [ October 06, 2005: Message edited by: Bear Bibeault ]
Thanks, that's very helpful. Just to clarify - I don't want one bean with user data for all sessions/users. I want one for every user (one per session). So I suppose when a user starts using the web app (they do something like a log-in as you guessed), or makes any other call to the controller servlet, I should check to see if a bean has been saved to the session. If not, initialise a new one and save it to the session. Mike
A little more advanced would be to use a servlet filter to check for the existance of the bean. If one doesn't exist, the request is hi-jacked to the login page. The act of logging in creates the bean and places it in the session.
That way, if they bookmark an inner page, or if the session times out, they are directed to the login page whenever the bean does not exist.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com