JSP/Servlet app design question

Posted on 2004-11-30
Last Modified: 2012-08-13
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.
Question by:snazzyrags
    LVL 8

    Expert Comment

    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

    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.

    Author Comment

    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.

    LVL 8

    Accepted Solution


    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....

    Author Comment


    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.

    LVL 8

    Expert Comment

    YW... Glad to be of help :)

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Run the project 5 79
    Set up Wildfly 10 8 281
    using if condition without JSTL 2 99
    maven project jars 7 39
    The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now