views:

29

answers:

2

hello,

Moment the main servlet is deployed, it needs to perform calculations and prepare a list. this list needs to be accessed by the other servlet which are called subsequently. the calculation needs to run only once. could some one please explain how to go about doing that.

thanks

+2  A: 

You can use a ServletContextListener and perform calculation from there.


The class file :

public final class YourListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent event) {
        ServletContext context = event.getServletContext();
        //Calculation goes here
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        //Nothing to do
    }
}

web.xml :

<web-app>
    <!-- ... -->
    <listener>
        <listener-class>ext.company.project.listener.YourListener</listener-class>
    </listener>
    <!-- ... -->
</wep-app>

Resources :

Colin Hebert
thanks Colin. But i perform the calculation and then how do i store/retrieve it...please explain
raqz
@Abdul, you can store the result in your [context](http://download-llnw.oracle.com/javaee/5/api/javax/servlet/ServletContext.html) as an attribute and load it later with `getServletContext().getAttribute()` from any servlet
Colin Hebert
@Colin... thank you so much for the help. to use this context object, do I need to call the request dispatcher using the context? please let me know
raqz
@Abdul, This context is the servlet context. Accessible from any servlet with `getServletContext()` and from `YourListener` in `event.getServletContext()`.
Colin Hebert
@Colin...and the moment i have done this, the server fails the load. It just says GLassFish server domain failed to load
raqz
@Abdul, It shouldn't can you show your code/configuration ?
Colin Hebert
oh..i am not allowed to type over 600 characters..how do i post the code
raqz
public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); try { System.out.println("Server int in listener Successfully"); Context initContext = new InitialContext(); System.out.println("Using the connection pool"); dataSource = (DataSource) initContext.lookup("jdbc/myDatasource"); } catch (NamingException ex) { Logger.getLogger(MainServlet.class.getName()).log(Level.SEVERE, null, ex); }
raqz
BuildRdLinkList getBuild = new BuildRdLinkList(dataSource); ArrayList<Nodelist> roadlink; try { roadlink = getBuild.getNetworkWhp(); context.setAttribute("roadLink", roadlink); }
raqz
the server starts performing the calculation and after a few seconds crashes without stating error except for Personal Domain GlassFish error
raqz
okay. Just try to remove the content of `contextInitialized` too see if it isn't the added code which is causing the error.
Colin Hebert
i tried doing that ...it still fails...the list calculation takes about 60-70 seconds totally.... but now the server crashes after 20-30 secs
raqz
@Abdul did you removed everything ? There shouldn't be any error.
Colin Hebert
yes i removed everything. just put the buildlink list class and its function. does the servletcontextlistener have any kind of time out or something?
raqz
Well.. i just removed the entire thing and put it on a tomcat server and it worked great..thanks Colin!
raqz
A: 

in your main servlet initialization method

public void init(ServletConfig config) throws ServletException {
    super.init(config);

    // do calculations
    ArrayList resultsList = calculate_something();

    // save for other servlets
    config.getServletContext().setAttribute("SAVED_DATA", resultsList);
}

in the other servlets

// retrieving value from ServletContext
ArrayList list = (ArrayList)getServletContext().getAttribute("SAVED_DATA");
Aaron Saunders
If the servlet is destroyed and reloaded this will happen twice.
Colin Hebert
@Colin Hebert you are correct, he mentioned that the main servlet needed to do the calculation, that is why I posted what I did. It is obvious from the exchange that is misunderstood the question.
Aaron Saunders
@Aaron... my mistake in typing the question wrong..sorry
raqz