java - FreeMarker : Displaying a custom 404 page without redirection -
i have static 404 page fancy stuff in it. in case user enters wrong url of page not exist, him see 404 page , keep url in order user see mistake s/he has done typing url .
the page entered , not exist : http://localhost:10039/my.website/my/halp.html
the 404 page : http://localhost:10039/my.website/my/notfound.html
briefly, instead of using "sendredirect" here, "get content" of pagenotfoundurl , show while url still http://localhost:10039/my.website/my/halp.html
instead of redirect, tried "forward" kayaman suggested in case " cannot forward. response committed."
testservlet defined in web.xml , , class extends utilfreemarkerservlet extends freemarkerservlet.
utilfreemarkerservlet
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); } }
testservlet
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; } }
do forward instead of redirect wanted resource, , url stay same.
requestdispatcher rd = request.getrequestdispatcher("my_404.html"); rd.forward(request, response);
Comments
Post a Comment