Servlet initialization
Using J2EE IDE:
1. Run servlet.
There is:
private boolean var1=true;
private String var2 = "";
declared in servlet properties section.
2. Do some action.
Change var1=false;
var2="new value";
3. Close browser.
4. Run servlet again.
var1 == false;
var2 == "new value";
I need these values to be initial values, not changed after previous run.
don't have them as instance variables, then. instance variables in a servlet are a bad idea, anyway. if your serlvet has several methods that rely on these variables being instance variables, refactor them so that they get them from, say, the context, or are passed the variables in their signature
1. What is the context applying to servlet?2. Is it good idea to bind this variables to current session?
if the variables you want to set are application variables, so use:
request.getSession().getServletContext().setAttribute("name", obj);
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletContext.html#setAttribute(java.lang.String,%20java.lang.Object)
but if it's a user (session) variables, so use:
request.getSession().setAttribute("name", obj);
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpSession.html#setAttribute(java.lang.String,%20java.lang.Object)
NB: 1 - if you close the browser, the session'll be lost so are its attributes
2 - if you set variables in the application context (first issue), every user (client) could alter them !!
hth
Message was edited by:
java_2006
> 1. What is the context applying to servlet?
A servlet uses a context to communicate with its container, and can use the ServletContext abstraction (interface) to get information such as initialization params for your web app. See Servlet API.
>
> 2. Is it good idea to bind this variables to current
> session?
No. The scope is wrong. If I understood your requirements correctly, those vars have Web app scope, and should be stored in the context.
I can get ServletContext parameters with getInitParameter(String name) method.How can I set init parameter?
add this to your web.xml
<init-param>
<param-name>YOUR_PARAM1_NAME</param-name>
<param-value>YOUR_PARAM1_VALUE</param-value>
</init-param>
<init-param>
<param-name>YOUR_PARAM2_NAME</param-name>
<param-value>YOUR_PARAM2_VALUE</param-value>
</init-param>
hth
> add this to your web.xml
>
> <init-param>
> <param-name>YOUR_PARAM1_NAME</param-name>
>
>
> param-value>YOUR_PARAM1_VALUE</param-value>
> </init-param>
>
> <init-param>
> <param-name>YOUR_PARAM2_NAME</param-name>
>
>
> param-value>YOUR_PARAM2_VALUE</param-value>
> </init-param>
>
>
> hth
I'm not sure that's what the OP wants. sounds to me like he wants some variables to be re-initialized for every request. I strongly suspect he's misused the term "run servlet again", or doesn't understand that the same servlet instance services many requests, rather than a new instance of the servlet for every request. but only he can confirm this obviously! OP?
Not only that George, I suspect that the OP is going to run into thread-safety issues.
I suppose that executing web-application and creating instance of servlet class rather different things with calling servlet method processRequest(...).
The problem is that i don't know what happens when I close browser which interacts with web-application and servlet.
Running application again shows dissapointed results:
servlet properties still has old values.
It looks like new application instance uses old servlet instance.
I just have no idea how it works.
> I suppose that executing web-application and creating
> instance of servlet class rather different things
> with calling servlet method processRequest(...).
>
> The problem is that i don't know what happens when I
> close browser which interacts with web-application
> and servlet.
>
> Running application again shows dissapointed results:
>
> servlet properties still has old values.
> It looks like new application instance uses old
> servlet instance.
>
> I just have no idea how it works.
the browser is one thing, the servlet another. closing the browser down has no effect on the servlet at all. until you re-start your web container, the same servlet instance is used for all the requests. I think that's the bit of information you're missing
I suppose that executing web-application and creating instance of servlet class rather different things with calling servlet method processRequest(...).
The problem is that i don't know what happens when I close browser which interacts with web-application and servlet.
Running application again shows dissapointed results:
servlet properties still has old values.
It looks like new application instance uses old servlet instance.
I just have no idea how it works.
> I suppose that executing web-application and creating
> instance of servlet class rather different things
> with calling servlet method processRequest(...).
>
> The problem is that i don't know what happens when I
> close browser which interacts with web-application
> and servlet.
>
> Running application again shows dissapointed results:
>
> servlet properties still has old values.
> It looks like new application instance uses old
> servlet instance.
>
> I just have no idea how it works.
closing the browser has no effect whatsoever on your web-app, in this respect
processRequest is a (server side) servlet method that handles a clients requests.
if you have something specefic to a user (client), so store it in the user session.
Once a borwser is closed all the previous session sttributes are lost.
Could you explain in more detail, what you want to do exactly ?
Could you provide code example how to restart web container?I develope web-poll. Select questions and answers from DB.Write answers to DB and go to next question according some logic.For example, I need to close web container by pressing button.
> Could you provide code example how to restart web
> container?
what? I think you've got the wrong end of the stick somewhere! why d'you want to do that? what's your actual problem?
> For example, I need to close web container by
> pressing button.
why? what on earth makes you think that is necessary, or will help? or even a good idea? every time someone uses your app, you want a minute+ of downtime while your container is kicked? nah!
a web container could not be restarted from within a web application since this latter is deployed in this container.
> a web container could not be restarted from within a
> web application since this latter is deployed in this
> container.
I don't think that's what he wants to do. seems he's picking up terms from the posts and misinterpeting them. nobody suggested re-starting the container would solve anything!
OP can you please be specific about what your problem is, and how the solutions given so far have failed
Yes. I work in J2EE IDE with bundled Tomcat.
I develope web poll engine.
Poll logic stored in DB (questions, answer variants, transitions between questions).
There is no specific data for each user.
One servlet call differs from other only with two parameters:
Poll_Participant_Id and Poll_Id.
I pass these parameters through GET-query (through link in JSP page)
I have indicator of the first question in poll:
private boolean firstQuestion_flag = false.
Change flag inside
processRequest() method:
firstQuestion_flag = true.
Close browser. As it was explained higher, web container still exists.
But. When I start application again (press F6), I suppose that it should be another instance of application with it's own data. But all servlet variables stored there values (the poll starts from the place where I closed browser), it looks like no new instance of application created and IDE continue interact with old instance.
Or I completely misunderstand what happening?
> Yes. I work in J2EE IDE with bundled Tomcat.
>
> I develope web poll engine.
> Poll logic stored in DB (questions, answer variants,
> transitions between questions).
> There is no specific data for each user.
> One servlet call differs from other only with two
> parameters:
> Poll_Participant_Id and Poll_Id.
> I pass these parameters through GET-query (through
> link in JSP page)
then that is data specific for each request
> I have indicator of the first question in poll:
>
> private boolean firstQuestion_flag = false.
>
> Change flag inside
> processRequest() method:
> firstQuestion_flag = true.
> Close browser. As it was explained higher, web
> container still exists.
> But. When I start application again (press F6), I
> suppose that it should be another instance of
> application with it's own data. But all servlet
> variables stored there values (the poll starts from
> the place where I closed browser), it looks like no
> new instance of application created and IDE continue
> interact with old instance.
> Or I completely misunderstand what happening?
you suppose right. there is not another instance of the servlet for the new request. I've told you this several times. you don't WANT there to be. as long as your servlet doesn't rely on instance variables, it will manage to process each request just fine
I work in IDE J2EE with bundled Tomcat.
I don't know exactly, but it looks like following:
1. I run web application (F6).
2. It calls my servlet. Do some action, init some variables etc.
3. Close browser (I realize that it doesn't affect web application)
4. Run application again (F6).
(Full servlet code example: http://brotherflame.livejournal.com/227702.html )
Servlet has variable values from previous run.
It looks like next run of application IDE find previously created web application instance and interacts with it.
This situation doesn't take place if I use "Clean and Build Main Project" (Shift+F11)
Could you give any comments on my supposition and if it's right how to avoid this problem?
See my previous postMessage was edited by: BrotherFlame
I've ment a bit different thing:
Each time I call servlet, application use the same instance.
But what happens when I run application again?
1. It creates new instance of application and uses old servlet instance.
2. It uses old instance of application with old servlet instance.
In case 1 to solve issue it's quite enough to make variables that I want to initialize each time I run application context parameters.
In case 2 I have no idea what to do.
> But. When I start application again (press F6), I
> suppose that it should be another instance of
> application with it's own data. But all servlet
> variables stored there values (the poll starts from
> the place where I closed browser), it looks like no
> new instance of application created and IDE continue
> interact with old instance.
>Or I completely misunderstand what happening?
I guess so, since you've been pondering continuously about servlet instances in spite of what people told you already.
Supposing that you're not using the deprecated SingleThreadModel here, there is always ONE servlet instance running, and each request is a thread inside that same instance.
Please read a tutorial and try understanding the basics of servlet programming.
You do not "shut down" the container.Instead you make sure that your variables are local (i.e. inside methods), for one . For example, is firstQuestion_flag declared at the class level ? If so, it will maintain its last value (the one set by the last request).
Which IDE are you using? Did you actually try to deploy your application on Tomcat (without using the IDE) and use a browser to see if your problem still exists? If not, something is wrong with your IDE settings. Otherwise the problem is probably the one mentioned above (variables scope).
Adding parameters is only half a part solving my problem.
I develope automated web poll engine.
Each time I run servlet I need to initialize first question id.
For this purpose I write following code inside processRequest() method:
...
ServletContext sc = this.getServletContext();
firstQuestion_flag = Boolean.parseBoolean(sc.getInitParameter("firstQuestion_flag"));
...
But it's wrong way. I need to initialize firstQuestion_flag only first time calling servlet from new instance of application. So I should bind a flag of first call to the servlet. But it leads to the situation when this flag will store value from previous run. I've reached deadlock.
The discussion went wrong branch.
I found.
And solve problem.
Decision was very simple.
When I call servlet first time, I check:
request.getParameter("my_parameter")
If it == null I initialize servlet variable from context parameter.
Next time I call servlet from itself and pass my_parameter through POST-query. It's not nul and all ok.
Excuse me for incorrect light of question.
Thank you all.
Still can't understand what exactly happens when I run apllication with "Clean and Build Main Project".
It destroys running servlet instance and creates new?
I use IDE J2EE (Java Studio Enterprise) with bundled Tomcat.