jsp - java.lang.IllegalStateException: Cannot forward after response has been committed in servletfilter -
i using filter determine whether or not requested page has valid session or not.
this code. web.xml
:
<filter> <filter-name>sessionfilter</filter-name> <filter-class> com.imagemanagementutility.filter.sessionfilter </filter-class> <init-param> <param-name>avoid-urls</param-name> <param-value>index.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>sessionfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
filter class:
public class sessionfilter implements filter { private arraylist<string> urllist; public void destroy() { } public void dofilter(servletrequest req, servletresponse res, filterchain chain) throws ioexception, servletexception { httpservletrequest request = (httpservletrequest) req; httpservletresponse response = (httpservletresponse) res; string url = request.getservletpath(); boolean allowedrequest = false; system.out.println(url.replace("/","")); if (urllist.contains(url)) { allowedrequest = true; } if (!allowedrequest) { httpsession session = request.getsession(false); if (null == session) { system.out.println("redirect in servlet session filter"); requestdispatcher dispatcher = request .getrequestdispatcher("//index.jsp"); dispatcher.forward(request, response); } } chain.dofilter(req, res); system.out.println("end"); } public void init(filterconfig config) throws servletexception { string urls = config.getinitparameter("avoid-urls"); stringtokenizer token = new stringtokenizer(urls, ","); urllist = new arraylist<string>(); while (token.hasmoretokens()) { urllist.add(token.nexttoken()); } } }
i have login page used check whether or not user valid. if user valid control transfer servlet.
public class mainservice extends httpservlet { private static final long serialversionuid = 1l; static logger log = logger.getlogger(mainservice.class.getname()); /** * @see httpservlet#httpservlet() */ public mainservice() { super(); } /* * @see httpservlet#doget(httpservletrequest request, httpservletresponse * response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse * response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { httpsession session = request.getsession(true); response.setcontenttype("text/html;charset=utf-8"); logutil.loginfo("i m here"); list<images> imagelist = null; response.setcontenttype("text/html"); try { userdataservice userdataservice = new userdataimpl(); string userid = null; string username = null; string password = null; username = request.getparameter("user"); password = request.getparameter("password"); userid = userdataservice.checkuser(username, password); if (userid!=null) { system.out.println(userid); session.setattribute("userid", userid); imagelist = userdataservice.getimages(userid); session.setattribute("imagelist", imagelist); /*response.sendredirect("//showuserimages.jsp");*/ requestdispatcher dispatcher = request .getrequestdispatcher("//showuserimages.jsp"); dispatcher.forward(request, response); } else { /*response.sendredirect("//index.jsp");*/ requestdispatcher dispatcher = request .getrequestdispatcher("//index.jsp"); dispatcher.forward(request, response); } } catch (imageexception e) { e.getmessage(); e.printstacktrace(); } } }
this works without filter, filter shows error when forward control "//showuserimages.jsp".
put invocation of dofilter
in else block:
if (!allowedrequest) { httpsession session = request.getsession(false); if (null == session) { system.out.println("redirect in servlet session filter"); requestdispatcher dispatcher = request .getrequestdispatcher("//index.jsp"); dispatcher.forward(request, response); } }else{ chain.dofilter(req, res); }
Comments
Post a Comment