黑马程序员技术交流社区

标题: 【广州校区】+【原创】listener [打印本页]

作者: Mylo    时间: 2019-2-21 15:03
标题: 【广州校区】+【原创】listener
filter过滤器Filter(筛请求的)public class QuickFilter implements Filter {
    public void destroy() {
        System.out.println("QuickFilter1销毁");
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("QuickFilter1 doFilter调用");
        chain.doFilter(request, response);
    }
    public void init(FilterConfig fConfig) throws ServletException {
        System.out.println("QuickFilter1初始化");
    }
}<!--web.xml-->
<filter>
    <filter-name>QuickFilter</filter-name>
    <filter-class>com.itcast.filter.QuickFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>QuickFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>filter的访问流程filter作用filter的APIFilter的配置登陆的基本实现<!--用于提示用户名和密码错误-->
<div>
    <span style="color: red">${info }</span>   
</div>//web层
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
   
    String username = request.getParameter("username");
    String password = request.getParameter("password");
   
    UserService service = new UserService();
    User user = service.login(username,password);
   
    if (user == null) {
        //登陆失败
        request.setAttribute("info", "用户名或密码错误");
        request.setAttribute("username", username);//把用户名带回去页面
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }else {
        //登陆成功,把用户对象存放在session中
        request.getSession().setAttribute("user", user);
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }
}<!--index.jsp-->
<c:if test="${sessionScope.user == null }"> <!--${empty user }-->
    <li><a href="login.jsp">登录</a></li>
    <li><a href="register.jsp">注册</a></li>
</c:if>
<c:if test="${sessionScope.user != null }"> <!--${not empty user }-->
    <li>欢迎:${sessionScope.user.realname }</li>
    <li><a href="javascript:void(0)">退出</a></li>
</c:if>自动登录//登陆成功后
String autoLogin = request.getParameter("autoLogin");//null or "autoLogin"
if(autoLogin != null && autoLogin.equals("autoLogin")) {
    Cookie cookie_username = new Cookie("username", username);
    Cookie cookie_pwd = new Cookie("password", password);
    cookie_username.setMaxAge(60 * 24 * 3);
    cookie_pwd.setMaxAge(60 * 24 * 3);
    response.addCookie(cookie_username);
    response.addCookie(cookie_pwd);
}//filter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    User user = (User) req.getSession().getAttribute("user");
    if(user != null) {
        chain.doFilter(request, response);
        return;
    }
    String usrename = null;
    String password = null;
    Cookie[] cookies = req.getCookies();
    if (cookies == null) {
        chain.doFilter(request, response);
        return;
    }
    for (Cookie co : cookies) {
        if ("username".equals(co.getName())) {
            usrename = co.getValue();
        }
        if ("password".equals(co.getName())) {
            password = co.getValue();
        }
    }
    if(usrename != null && password != null) {
        UserService service = new UserService();
        user = service.login(usrename, password);
    }
    if(user == null) {
        chain.doFilter(request, response);
        return;
    }else {
        req.getSession().setAttribute("user", user);
        chain.doFilter(req, response);
    }
}解决全局乱码问题class EncoderRequest extends HttpServletRequestWrapper {
    private HttpServletRequest request = null;
    private boolean isGet = false;
   
    public EncoderRequest(HttpServletRequest request) {
        super(request);
        this.request = request;
        isGet = "get".equals(request.getMethod().toLowerCase())? true:false;
    }
   
    @Override
    public String getParameter(String name) {
        String parameter = request.getParameter(name);
        if (isGet) {
            try {
                parameter = new String(parameter.getBytes("iso8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return parameter;
    }
}public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    req.setCharacterEncoding("utf-8");
    res.setContentType("utf-8");
    EncoderRequest enRequest = new EncoderRequest(req);
    chain.doFilter(enRequest, response);
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2