You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@jakarta.apache.org by Clark Slater <cl...@redtail.net> on 1999/12/22 02:30:03 UTC
servlet question
This question doesn't directly relate to jakarta, but there are so many
knowledgeable people on this list that I can't resist.
I'm working on a shopping cart app where the idea is that one servlet acts
like a controller by receiving a value that represents an event, and then
calls an appropriate static function with the customer's session and any
collected form fields as input.
The service method looks something like this:
HttpSession session = request.getSession(false);
Customer c = (Customer)session.getValue("Customer");
if(event == ADD_TO_BASKET) {
String prodID = request.getParameterValues("prodID")[0];
Basket.add(c, prodID);
}
else if(event == SUBMIT_ORDER) {
Order.submit(c);
}
...
Question: If two Customers enter the service method with the
ADD_TO_BASKET event at the same time, does this model potentially break?
Thanks.
Re: servlet question
Posted by "Craig R. McClanahan" <cm...@mytownnet.com>.
Clark Slater wrote:
> This question doesn't directly relate to jakarta, but there are so many
> knowledgeable people on this list that I can't resist.
>
> I'm working on a shopping cart app where the idea is that one servlet acts
> like a controller by receiving a value that represents an event, and then
> calls an appropriate static function with the customer's session and any
> collected form fields as input.
>
> The service method looks something like this:
>
> HttpSession session = request.getSession(false);
> Customer c = (Customer)session.getValue("Customer");
>
> if(event == ADD_TO_BASKET) {
> String prodID = request.getParameterValues("prodID")[0];
> Basket.add(c, prodID);
> }
> else if(event == SUBMIT_ORDER) {
> Order.submit(c);
> }
> ...
>
> Question: If two Customers enter the service method with the
> ADD_TO_BASKET event at the same time, does this model potentially break?
>
As long as you use only local variables, instead of instance variables, you
should not have a problem -- local variables are stored on the stack, and
there is a stack for each active thread. Thus, variables like "c" and
"event" are thread safe. As long as the other methods you call (like
Basket.add() and Order.submit()) are also thread safe, you will be fine.
>
> Thanks.
>
Craig McClanahan