Basically, we want to create a taglib that can possibly forward request execution to开发者_如何学Go another page, similar to <jsp:forward />
. How can we prevent the rest of the JSP page from executing from within the taglib?
You shouldn't be doing this in a taglib. Rather do it in a Servlet or Filter, before any bit is been sent to the response. Otherwise you will possible get into IllegalStateException: response already committed
troubles.
For me the following lines worked quite well:
String url = "http://google.com";
response.reset();
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader("Location", url);
response.getWriter().close();
response.getWriter().flush();
If you don't want to continue processing the original page after passing control to another you are better off using a redirect rather than a forward.
response.sendRedirect("foo.jsp");
This will redirect to the new page and stop processing the old one.
However - redirects are only usable if you have not written anything to the response body yet (i.e. not sent any data back to the client).
精彩评论