开发者

is request.getRequestDispatcher.forward() asynchronous?

When I called one servlet from another inside single if statement I was shocked, that even if my forwarding method was called the flow didnt stopped and next methods were invoked (I had to put them into else statemen开发者_开发技巧t to stop this).

How come? Does this mean that forward is asynchronous (if so, are there any more such a "tricky" methods)?

I've used "standard" forwarding: request.getRequestDispatcher(JSPFileName).forward(request, response);


No, it doesn't mean it's asynchronous. It just means that forward is a regular Java method, and that there's no reason for the rest of the code not to execute. It's just of your responsibility to avoid modifying the response after the request has been forwarded. But why wouldn't the following code log everything to the standard output?

System.out.println("About to forward the request to " + jspFileName + "...");
long t0 = System.currentTimeMillis();
request.getRequestDispatcher(jspFileName).forward(request, response);
long t1 = System.currentTimeMillis();
System.out.println("The forward took " + (t1 - t0) + " to complete");


No it's not asynchronous, but if you call the forward method in your service method and do not return, you can perform other operations (it's the same thread), but remember the forward remove all uncommitted output in the response added before it was called. If you want to include the output of a servlet in your response use the include method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜