做一个简单的用户登陆,如果用户输入用户名和密码正确,则提示登陆成功信息,否则返回登陆界面
------解决方案--------------------
哎 就比如你有一个文件夹person 就只有登陆的人才有权限访问 你如果不用过滤器就要每个页面去判断是否登陆了 用过滤器就会在访问每个jsp之前去判断是否登陆 省了很多事情
import java.io.IOException;
import javax.servlet.http.*;
import javax.servlet.*;
public class LoginCheck implements Filter {
protected FilterConfig filterConfig = null;
public void destroy() {
this.filterConfig = null;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
if(filterConfig==null)
return;
HttpServletRequest hreq=(HttpServletRequest)req;
HttpSession session=hreq.getSession();
if((session.getAttribute( "username ")==null)||(session.getAttribute( "username ").equals( " "))){
RequestDispatcher requestDis=req.getRequestDispatcher( "login.jsp ");
requestDis.forward(req,res);
}
else{
chain.doFilter(req,res);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}
------解决方案--------------------
别忘啦,
<filter>
<filter-name> cacheFilter </filter-name>
<filter-class> 你的class路径 </filter-class>
</filter>
<!--filter-mapping>
<filter-name> cacheFilter </filter-name>
<url-pattern> */url-pattern>
</filter-mapping-->