Question Details

Browse

init() and session management

By jxva - Aug. 23, 2008

Hi I have servlet callling java function which is need to be executed when application loads first time.
In that servlet there is one method only init() as doget and dopost wont work for loadup servlet.
The java function returns one value which I need to pass to jsp .how to do it as request.getsession wont work inside init() method.


Answers

Add Answer
  1. By jxva on Aug. 23, 2008

    Put it in the servletContext:


    getServletContext().setAttribute(name,value)


    0 Votes
  2. By adfxs on Aug. 23, 2008

    you sould sent the result as application variable. I guess this variable is used for many sessions.


    0 Votes
  3. By an anonymous user on Aug. 24, 2008

    Why don't you create a singleton object, which will get created the first time the object is called, and in its constructor, you can add your business logic to create and do what needs to be done only once. Thereafter, the object is never created again, and hence your function is never called. I don't know if this is what you want to do, but it is an option.


    0 Votes
  4. By gzgonikov on Aug. 29, 2008

    ServletContext is the most top scope level


    0 Votes
  5. By rodeliosr on Aug. 29, 2008

    User servlet context to get the parameters.


    0 Votes
  6. By snickernet on Aug. 29, 2008

    Implement ServletContextListener to call your method. This context listener will be called once when the app is started. You may choose to put the return value of you're method to a singleton object or you may elect this value to be application scope variable to be accessed by different servlets.


    0 Votes
  7. By droft on Aug. 29, 2008

    call your function from jsp directly


    0 Votes
  8. By rmarin on Aug. 29, 2008

    if you need code to be executed when application loads for the first time, you need to set up a context listener and in the contextInitialized method do whatever you need and then set the value in the ServletContext scope, you should then be able to access this from your JSP or servlet.


    0 Votes
  9. By chinaLong on Aug. 30, 2008

    servlet context


    0 Votes
  10. By qiandonghui on Sep. 01, 2008

    ServletContext


    0 Votes
  11. By jventure9999ostatic on Sep. 02, 2008

    You can use a static class variable.


    Class A{


    static protected Object VarToJSP;


    private Object nameMethod(params){

    Object result=null;

    .....


    VarToJSP = result;


    }


    }


    Now in the JPS to call write :


    ....


    someVariable=A.VarToJSP;


    0 Votes
Share your knowledge

Related Questions

Browse Get answers and share your expertise.