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 AnswerPut it in the servletContext:
getServletContext().setAttribute(name,value)
you sould sent the result as application variable. I guess this variable is used for many sessions.
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.
ServletContext is the most top scope level
User servlet context to get the parameters.
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.
call your function from jsp directly
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.
servlet context
ServletContext
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;
Share your knowledge