Reputation: 2367
Servlets are non-threadsafe, the state could be lost between methods calls by different requests.
But can i persist the state between own Servlet's methods during the single request safely to implement composite operations?
Like this:
public class MyServlet extends HttpServletRequest {
private String var;
void addH() {
var += "h";
}
void addI() {
var += "i";
}
void doGet(...) {
addH();
addI();
// var = "hi"?
}
}
Will the var
contain the correct value in this case even with concurrent requests? In other words, does it work as Stateless Bean in EJB does?
Upvotes: 0
Views: 40
Reputation: 13946
No, you can't. Like you said, servlets are not threadsafe. There's no protected "space" that a request lives in. So multiple threads (and thus requests) can hit var
at the same time. If you want such a protected "space" you need to either synchronize doGet()
(which of course will destroy concurrency) or do something like using ThreadLocal
so that each thread (and thus request) will have its own separate copy of instance-wide state.
If you don't need the state to persist outside of requests, you could just create the state within doGet()
and pass it to the other methods:
void doGet(...) {
StringBuilder var = new StringBuilder();
addH(var);
addI(var);
resp.write(var.toString());
}
void addH(StringBuilder var) {
var.append('h');
}
void addI(StringBuilder var) {
ver.append('i');
}
Upvotes: 1
Reputation: 3174
public class MyServlet extends HttpServletRequest {
private String var = "";
void addH() {
var += "h";
}
void addI(long dontAnswerBefore) {
while(System.currentTimeMillis() < dontAnswerBefore) {
// Do nothing
}
var += "i";
}
void doGet(...) {
long now = System.currentTimeMillis();
addH();
addI(now + 10_000);
resp.write(var);
}
}
This should get you 10 Seconds to make two Requests within two tabs or two different browsers.
Upvotes: 1