It's not a secret anymore!
The moose likes Servlets and the fly likes Web Application Framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Web Application Framework" Watch "Web Application Framework" New topic

Web Application Framework

Ashwini Srivastava

Joined: Feb 01, 2006
Posts: 1
Hi all,

I have created a servlet based on the Web Application Framework as suggested by SUN
( link : [ 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
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

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

Joined: Oct 09, 2002
Posts: 294
perform can be synchronized though

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I agree. Here's the link:
subject: Web Application Framework
It's not a secret anymore!