跟着狂神学JavaWeb --> 13. 过滤器监听器常见应用
13.1 应用1:监听器在GUI编程中的应用
public class TestPanel {
public static void main(String[] args) {
//窗体
Frame frame = new Frame("ano");
//面板
Panel panel = new Panel(null);
//设置窗体布局
frame.setLayout(null);
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(0,255,255));
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(255,255,0));
frame.add(panel);
frame.setVisible(true);
//监听事件,监听关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
}
});
}
}
13.2 应用2:过滤器登录权限拦截
- 场景:用户登录之后才能进入主页,注销后就不能进入主页了!
- 思路:
- 用户登录后,向Session中放入用户的数据;
- 进入主页的时候,判断用户是否已经登录。(在过滤器中实现)
- 代码示例:
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>登录页面</h1>
<form action="/servlet/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>
</body>
</html>
sys/home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>主页</h1>
<p><a href="/servlet/logout"> 注销</a></p>
</body>
</html>
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1>错误</h1>
<h3>没有权限</h3>
<a href="/login.jsp">返回首页</a>
</body>
</html>
Constant.java
public class Constant {
public static String USER_SESSION = "USER_SESSION";
}
LoginServlet.java
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取前端请求的参数
String username = req.getParameter("username");
String password = req.getParameter("password");
if(username.equals("admin")) {
req.getSession().setAttribute(Constant.USER_SESSION,req.getSession().getId());
resp.sendRedirect("/sys/home.jsp");
}else {
resp.sendRedirect("/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
LogoutServlet.java
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object attribute = req.getSession().getAttribute(Constant.USER_SESSION);
if(attribute != null) {
req.getSession().removeAttribute(Constant.USER_SESSION);
}
resp.sendRedirect("/login.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
LoginFilter.java
public class LoginFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
Object userSession = request.getSession().getAttribute(Constant.USER_SESSION);
if(userSession == null) {
response.sendRedirect("/error.jsp");
}
filterChain.doFilter(servletRequest,servletResponse);
}
public void destroy() {
}
}
web.xml
中的配置
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.ano.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.ano.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/servlet/logout</url-pattern>
</servlet-mapping>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.ano.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>