ssh框架还需要学filter吗

2024-12-16 23:32:12
推荐回答(1个)
回答1:

要学的
在你项目中的web.xml中加入filter,如下:

SessionFilter

com.dc.portal.system.sessionmgr.SessionFilter



filter-class的值是你自己创建的类,给你个大致的例子:
public class SessionFilter implements Filter{

public void destroy() {
}
//这个doFilter方法必须有
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

HttpServletRequest httpreq = (HttpServletRequest) request;
HttpServletResponse httpres = (HttpServletResponse) response;
if(httpreq.getSession().getAttribute("userInfo") == null){
((HttpServletResponse)response).sendRedirect("你要重定向的地址");
}else{
chain.doFilter(httpreq, httpres);
}
}

}