A Controller servlet may perform either a forward or a redirect operation at the end of processing a request. It is important to understand the difference between these two cases, in particular with respect to browser reloads of web pages.
Forward
Redirect
There are several ways to perform a Redirect, here are a few common ones:
HTTP/1.1 301 moved permanently Location: http://www.example.org/
HTTP/1.1 200 ok Refresh: 0; url=http://www.example.com/
<meta http-equiv="refresh" content="0; URL=http://www.example.org/" />
<script type="text/javascript">location.;</script>
In general, a forward should be used if the operation can be safely repeated upon a browser reload of the resulting web page; otherwise, redirect must be used. Typically, if the operation performs an edit on the datastore, then a redirect, not a forward, is required. This is simply to avoid the possibility of inadvertently duplicating an edit to the database.
More explicitly :
In HTML, a <FORM> tag can either GET or POST its data. In this context, a GET corresponds to a SELECT-then-forward, and a POST corresponds to an edit-then-redirect.
It is strongly recommended that forms for the input of search criteria should use GET, while forms for editing database records should use POST.
Example
This example is after the style of the WEB4J Controller class. The important methods of the Servlet API are :
import java.io.IOException;import javax.servlet.*;import javax.servlet.http.*;import hirondelle.web4j.action.Action;import hirondelle.web4j.action.ResponsePage;import hirondelle.web4j.request.RequestParser;import hirondelle.web4j.model.BadRequestException;public class RedirectForward extends HttpServlet {//..many items elided@Override public final void doGet(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {processRequest(aRequest, aResponse);}@Override public final void doPost(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {processRequest(aRequest, aResponse);}/*** Handle all HTTP <tt>GET</tt> and <tt>POST</tt> requests.*/protected void processRequest(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {RequestParser requestParser = RequestParser.getInstance(aRequest, aResponse);try {Action action = requestParser.getWebAction();ResponsePage responsePage = action.execute();if ( responsePage.getIsRedirect() ) {redirect(responsePage, aResponse);}else {forward(responsePage, aRequest, aResponse);}}catch (BadRequestException ex){//..elided //use Response.sendError() }catch (Throwable ex) {//..elided //use Response.sendError() }}// PRIVATE //private void redirect(ResponsePage aDestinationPage, HttpServletResponse aResponse) throws IOException {String urlWithSessionID = aResponse.encodeRedirectURL(aDestinationPage.toString());aResponse.sendRedirect( urlWithSessionID );}private void forward(ResponsePage aResponsePage, HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletException, IOException {RequestDispatcher dispatcher = aRequest.getRequestDispatcher(aResponsePage.toString());dispatcher.forward(aRequest, aResponse);}}
getRequestDispatcher
RequestDispatcher getRequestDispatcher(String path)
RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. This method returns null if the servlet container cannot return a RequestDispatcher.
The difference between this method and ServletContext.getRequestDispatcher(java.lang.String) is that this method can take a relative path.
path - a String specifying the pathname to the resource. If it is relative, it must be relative against the current servlet. RequestDispatcher object that acts as a wrapper for the resource at the specified path, or null if the servlet container cannot return a RequestDispatcher RequestDispatcher, ServletContext.getRequestDispatcher(java.lang.String)聯(lián)系客服