JSP/Servlet app design question

I have a project consisting of several JSP pages and JavaBeans. There is a login front-end and DB accesses. Also, an app properties file is read in and put in a static global class for access from several pages/beans.

Q: I'm looking for a way to start the entire project up when the web server is started, including reading in the properties file and making the DB connection, BEFORE the first client logs in. The JSP pages will be precompiled into CLASS files. The beans will be given application scope.

The product may be installed on any of the popular web servers (Websphere, Weblogic, Tomcat, etc.), so the solution must be generic.

Is this possible?

PS - If a solution exists, I will increase the point value of this question for more details.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for reading the properties file, making db connection etc you can have a servlet which loads on startup of your web application and then in the init method does all these and puts whatever java beans you want into Application context so that all the jsp/servlets can access...

for precompiling your jsp's before it is accessed here is something from www.jguru.com


Yes, there is a common solution that it's defined in the JSP specification and it has to be implemented by any container that claim to be compliant.
JSP 1.1 Specification paragraph 3.4.2 states:

    A request to a JSP page that has a request parameter with name "jsp_precompile" is a precompilation request. The "jsp_precompile" parameter may have no value, or may have values "true" or "false". In all cases, the request should not be delivered to the JSP page.
    The intention of the precompilation request is that of a hint to the JSP container to precompile the JSP page into its JSP page implementation class. The hint is conveyed by given the parameter the value "true" or no value, but note that the request can be just ignored in all cases.

Moved to paragraph 8.4.2 in JSP 1.2 PDF 2 Specification

So, to precompile 'myPage.jsp' it's enough to use 'myPage.jsp?jsp_precompile'.
This feature is extremely interesting even for verifying the code inside a page that requires parameters, or that contains code like sql insert or delete queries, etc.

In addition, many container have specific solution, some of them already covered by FAQs:
Tomcat with a comment regarding Oracle or JRun.
snazzyragsAuthor Commented:
Your first paragraph answered my question. There is a way to execute a servlet when the server starts up, independent of a client logging in. So, I write the servlet. How do I tell the server to run it and persist the access beans at the application level (scope) for use by the clients when they logon?

Also, I have clent statistics and other data to be written to a file when the client's session ends. How do I determine when this takes place? Do I need to create another session level servlet for each client, then put this "clean-up" code in the destroy() method?

I've increased the points in anticipation of your answer.


you can specify the servlet to startup in the web.xml file...
    <servlet-name>Startup Servlet</servlet-name>
your servlet class
    <load-on-startup> 1 </load-on-startup>

for you session level requirement you need to have a HttpSession Listener and then do the operations when the session is started or ended..
check out the following for the sample code for Session listener....


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snazzyragsAuthor Commented:

Thank you VERY much! You got me pointed in the right direction, I think I can carry it from here.

I figured there was a way to implement a listener on the session object, hadn't found it yet.
When I got your response, I was looking at the <servlet> element of the web.xml. So, I guess I was stumbling around in the right direction, but you put the light on my path.

YW... Glad to be of help :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.