title: FreeMarker : Displaying a custom 404 page without redirection
body: <p>I have a static 404 page with fancy stuff in it. 
  In case the user enters a wrong url of a page that does not exist, I would like him to see that 404 page , but also would like to keep the url as is in order for user to see what mistake s/he has done typing the url .</p>
  
  <p><strong>The page entered and that does not exist  :</strong>
  <a href="http://localhost:10039/my.website/my/halp.html" rel="nofollow">http://localhost:10039/my.website/my/halp.html</a></p>
  
  <p><strong>The 404 page :</strong> 
  <a href="http://localhost:10039/my.website/my/notfound.html" rel="nofollow">http://localhost:10039/my.website/my/notfound.html</a></p>
  
  <p>Briefly, instead of using "sendRedirect" here, I would to "get content" of pageNotFoundUrl and show it while the url is still <a href="http://localhost:10039/my.website/my/halp.html" rel="nofollow">http://localhost:10039/my.website/my/halp.html</a></p>
  
  <p>Instead of redirect, I also tried "forward" as Kayaman suggested but in this case I get <strong>" Cannot forward. Response already committed."</strong> </p>
  
  <p>TestServlet is defined in web.xml , and this class extends UtilFreemarkerServlet which extends FreemarkerServlet. </p>
  
  <p><strong>UtilFreemarkerServlet</strong> </p>
  
  <pre><code>public abstract class UtilFreemarkerServlet extends FreemarkerServlet {
      private static final long serialVersionUID = 1L;
  
      public static final String REQUEST_OBJECT_NAME = "RequestObject";
  
      private Logger logger = LoggerFactory.getLogger(getClass());
  
      @Override
      public void init() throws ServletException {
          logger.info("init started");
          super.init();
          Configuration cfg = getConfiguration();
          cfg.setLocalizedLookup(false);
  
      }
  
      @Override
      protected ObjectWrapper createObjectWrapper() {
          return ObjectWrapper.BEANS_WRAPPER;
      }
  
      @Override
      protected HttpRequestParametersHashModel createRequestParametersHashModel(HttpServletRequest request) {
          request.setAttribute(REQUEST_OBJECT_NAME, request);
          return super.createRequestParametersHashModel(request);
      }   
  
  }
  </code></pre>
  
  <p><strong>TestServlet</strong> </p>
  
  <pre><code>public class TestServlet extends UtilFreemarkerServlet{
      private static final long serialVersionUID = 1L;    
      private String website;
  
      @Override
      public void init(ServletConfig config) throws ServletException {
          super.init(config);
      }
  
      @Override
      protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {     
          super.service(req, resp);       
          boolean handleResult = handlerForRequest(req, resp);            
      }
  
      protected boolean handlerForRequest(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  
          if (resp.getStatus() == 404) {
              String pageNotFoundUrl = "http://localhost:10039/my.website/my/notfound.html";          
              RequestDispatcher rd = req.getRequestDispatcher(url);
              rd.forward(req, resp);
              // resp.sendRedirect(url);
          }
  
          return true;
      }
  
  }
  </code></pre>