十三、过滤器,监听器的常见应用
-
用户登录之后,向Session中放入用户的数据
-
进入主页面的时候要判断用户是否已经登录;要求:在过滤器中实现
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; if (request.getSession().getAttribute(Constant.USER_SESSION) == null) { response.sendRedirect("/error.jsp"); } filterChain.doFilter(request,response); }
小项目代码实现:
-
编写工具类
package com.lyh.util; public class Constant { public final static String USER_SESSION = "USER_SESSION"; }
-
写登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登录页面</h1> <form action="/servlet/login" method="post"> <input type="text" name="username"> <input type="submit"> </form> </body> </html>
-
写登录页面的servlet(LoginServlet)
package com.lyh.servlet; import com.lyh.util.Constant; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取前端请求参数 String username = req.getParameter("username"); //如果你输入admin就会登录成功 if (username.equals("admin")){ req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId()); resp.sendRedirect("/sys/success.jsp"); }else {//登录失败 resp.sendRedirect("/error.jsp"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
-
在web.xml 中注册servlet镜像
<!-- 当/servlet/*路径下的所以运行的服务器都会被过滤--> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.lyh.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.lyh.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/servlet/login</url-pattern> </servlet-mapping>
-
登录成功,登录失败,跳转页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登录成功</h1> <a href="/servlet/logout">注销</a> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>错误</h1> <a href="/Login.jsp">返回登录页面</a> </body> </html>
-
注销会有一个按钮(对应一个Servlet,编写注销的Servlet为LogoutServlet)
package com.lyh.servlet; import com.lyh.util.Constant; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class LogoutServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //这时我们还有会话记录 if (req.getSession().getAttribute(Constant.USER_SESSION)!=null){ //把会话记录销毁 req.getSession().removeAttribute(Constant.USER_SESSION); //重定向到注册页面 resp.sendRedirect("/Login.jsp"); }else { //重定向到注册页面 resp.sendRedirect("/Login.jsp"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
-
注销的servle(LogoutServlet)t,也在web.xml 中注册一下
<servlet> <servlet-name>LogoutServlet</servlet-name> <servlet-class>com.lyh.servlet.LogoutServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LogoutServlet</servlet-name> <url-pattern>/servlet/logout</url-pattern> </servlet-mapping>
-
成功页面中注销点击就跳转到Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>登录成功</h1> <a href="/servlet/logout">注销</a> </body> </html>
-
写过滤器Filter
package com.lyh.filter; import com.lyh.util.Constant; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class SysFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; if (request.getSession().getAttribute(Constant.USER_SESSION) == null) { response.sendRedirect("/error.jsp"); } filterChain.doFilter(request,response); } @Override public void destroy() { } }
-
在web,xml中注册过滤器
<filter> <filter-name>SysFilter</filter-name> <filter-class>com.lyh.filter.SysFilter</filter-class> </filter> <filter-mapping> <filter-name>SysFilter</filter-name> <url-pattern>/sys/*</url-pattern> </filter-mapping>
-
补充一点:我们这个过滤器是过滤页面的,sys这个包下面的页面都会被我们的过滤器过滤一遍