java - How we can use Servlet Filter for sessioncheck? -
this question has answer here:
i new in java. try use servlet filters session check.my code given below. got error while run. please correct error. error code is
http status 500 - cannot forward after response has been committed type exception report message cannot forward after response has been committed description server encountered internal error prevented fulfilling request.
and code is
my web.xml
<servlet> <servlet-name>login</servlet-name> <servlet-class>com.sample.wordnik.servlet.loginservlet</servlet-class> </servlet> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <filter> <filter-name>sessionfilter</filter-name> <filter-class> com.sample.wordnik.filter.sessionfilter </filter-class> <init-param> <param-name>avoid-urls</param-name> <param-value>login.jsp,signup.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>sessionfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
my filter class is
import java.io.ioexception; import java.util.arraylist; import java.util.stringtokenizer; import javax.servlet.filter; import javax.servlet.filterchain; import javax.servlet.filterconfig; import javax.servlet.servletexception; import javax.servlet.servletrequest; import javax.servlet.servletresponse; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; public class sessionfilter implements filter { private arraylist<string> urllist; @override public void destroy() { } @override 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; if(urllist.contains(url)) { allowedrequest = true; } if (!allowedrequest) { httpsession session = request.getsession(false); if (null == session) { response.sendredirect("login"); } } chain.dofilter(req, res); } @override 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()); } } }
and loginservlet is
public class loginservlet extends httpservlet { @override public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { request.getrequestdispatcher("/web-inf/jsp/login.jsp").forward(request, response); } }
problem had redirected user on login page
invalid session using response.sendredirect("login")
. had tried continue request chain after redirect chain.dofilter(req, res)
.
try using return
keyword here:
if (null == session) { response.sendredirect("login"); return; // added }
Comments
Post a Comment