Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Web Application Framework

 
Ashwini Srivastava
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have created a servlet based on the Web Application Framework as suggested by SUN
( link : http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html [ Look under Web Tier MVC Controller Design ].

The controller has been designed using the Hashmap ( as discussed in the above link and Code is as below )

public class ControllerServlet extends HttpServlet {
private HashMap actions;
public void init() throws ServletException {
actions = new HashMap();
CreateUserAction cua = new CreateUserAction(model);
actions.put(cua.getName(), cua);
//... create and add more actions
}
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws IOException, ServletException {

// First identify operation "op" from URL.
// method getOperation() is defined elsewhere.
String op = getOperation(req.getRequestURL());
// Then find and execute corresponding Action
Action action = (Action)actions.get(op);
Object result = null;
try {
result = action.perform(req);
} catch (NullPointerException npx) {
//... handle error condition: no such action
}
// ... Use result to determine next view (see next section)
}
//... other methods...
}


The question pertains to :

If two users are coming simultaneously then, they will be using the same object from ACTION HASHMAP and if "perform" is called then the same object "perform" method will be called. I do see that request and response object will be unique for each request in "doPost()". But keeping in mind that Servlet is not thread safe then one request, response object will overwrite the other persons request response object.

Can anybody explain that my understanding is correct and if 10s of request will come simultaneously then each will perform uniquely or not ???

Warm Regards
Ashwini
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwini,
Each request will have a different request/response objects. No issues. What you need to worry is if there are objects which are shared across requests(thread).

I noticed that you are catching NullPointerException.It is an unchecked exception so avoid catching NullPointerException.
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perform can be synchronized though
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic