java - Display forwarded JSP with url-pattern "/*" -
to improve java skills, i'm trying build simple j2ee framework (mvc).
i built handle every request in frontservlet. here mapping used :
web.xml : <servlet> <servlet-name>front</servlet-name> <servlet-class>test.frontservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>front</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
my problem when forward request frontservlet jsp, obviously, jsp request handle frontservlet , view isn't rendered.
- how can resolve problem keeping url-pattern "/*" ?
- is there way render jsp in servlet without performance losses ?
thanks in advance reply !
- solution 1 (@bryan kyle)
i'm trying follow advise. created filter :
public void dofilter(servletrequest request, servletresponse response, filterchain chain) throws ioexception, servletexception { httpservletrequest req = (httpservletrequest) request; if(!req.getrequesturl().tostring().endswith("jsp")) { // changed servlet url-pattern "/front.controller" req.getrequestdispatcher("/front.controller").forward(req, response); /*chain.dofilter(req, resp);*/ } }
<filter> <filter-name>filter</filter-name> <filter-class>test.filter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
- is right?
thanks !
a filter
inappropriate solution front controller approach.
you want refine url-pattern
of servlet matches e.g. /pages/*
or *.do
. don't want front controller kick in on irrelevant requests css/js/images/etc. take /pages/*
example, assuming you've jsp in /web-inf/foo.jsp
, following in servlet
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.getrequestdispatcher("/web-inf" + request.getpathinfo() + ".jsp").forward(request, response); }
should display jsp in question on http://localhost:8080/contextname/pages/foo.
Comments
Post a Comment