google app engine - Usage of Static variables in Java Servlets (e.g. in AppEngine) -
I have an application where the servlet is a method called update (ReqIn, ReqOut). I get it from
doGet
& amp; doPost
and pass the request and response variable, and then it is up to update (...)
to fill the following fixed variable:
... public class server HttpServlet {public static = zero in HttpServletRequest; Public Static HttpServletResponse Out = Faucet; Public static boolean is debug = true; Public Static Boolean isPost = false; Public fixed string url = ""; Public static string ip = "0.0.0.0"; Public Stable Cookie [] Cookies = Faucet; Public Stable UserClass User = Faucet; Public Static Boolean isLoggedIn = false; ...}
Actually the most used stuff and the essence. Updating it on every request It also gives me IP address & amp; The current user data from anywhere in the website, only the server. Code entered by the device ();
Uneded to create a new class instance using a page to load and using too many access codes: server.getUser (). GetUsername ();
The question now is: when in multi-user environment (Jetty on appangin), can it present a problem? Like some threading / racing issue users are being logged in as a different user suddenly looking at the wrong IP address or extreme situation?
Or should I re-write the code and instead it should be changed to public user class
public stable user class user
, etc.?
The use of statics is very bad idea, because if you get two requests at the same time, then they Take a look at this small example to see what might be wrong:
1: public class server HTTPServlet {2: public static int requestNo = 0; 3: Public Zero doGet (HttpServletRequest request, HttpServletResponse resp) 4: {5: requestNo ++; 6: resp.getWriter (). Println (requestNo); Now imagine the following timeline: Appear in 1, and processes, and this includes, including line 5.
Requests 2, and processes completely.
The Request 1 process continues.
Instead of obtaining both requests "1" and receiving "2", the text "2" will be found.
Or I should rewrite the code and change it to the public.
Instead of user class user, public Static user class user, etc.?
No, this is also not good enough, because J2 EEE Space Servicelet Container allows everyone to use one instance of a class to request that sublet mapping, That is, the example level variables will have the same effect as the situation, they are shared among all requests.
This leaves only three real options:
- Press everything in HTTpsession Here's the problem that it's a map, so you lose type protection, and It is difficult to see how things are being used.
- Create holder classes to catch all your kingdoms. It's a little better, because at least you do not want to lose type of security, but you still do not have full visibility.
- Pass the personalized necessities around.
Comments
Post a Comment