Day47-过滤器、监听器常见应用
过滤器、监听器常见应用
监听窗口:
package com.xiaozhao.listener;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
/**
* @author 小龚
* @create 2022-09-08 9:47
*/
public class Test {
public static void main(String[] args) {
Frame frame = new Frame("中秋节快乐");//新建一个窗体
Panel panel = new Panel(null);//面板
frame.setLayout(null);//设置窗体的布局
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(0,0,255));//设置背景颜色
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(0,255,0));//设置背景颜色
frame.add(panel);
frame.setVisible(true);
//监听事件,监听关闭事件
frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("打开");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭中");
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("已关闭");
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("激活");
System.exit(0);
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("未激活");
}
});
}
}
测试
缩小化
用户登录之后才能进入主页!用户注销后就不能进入主页了!
- 用户登录之后,向Session中放入用户的数据
- 进入主页的时候要判断用户是否已经登录(在过滤器中实现)
代码实现:
控制器:
LoginOutServlet(控制注销)
package com.xiaozhao.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 小龚
* @create 2022-09-08 22:39
*/
public class LoginOutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session = req.getSession().getAttribute("USER_SESSION");
//如果user_session不为空,说明在登陆中
if(user_session!=null){
req.getSession().removeAttribute("USER_SESSION");//移除这个对象
resp.sendRedirect(req.getContextPath()+"/login.jsp");//重定向到登录页面
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
LoginServlet(控制登录)
package com.xiaozhao.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 小龚
* @create 2022-09-08 21:49
*/
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取前端请求的参数
String username = req.getParameter("username");
if(username.equals("admin")){
//登录成功
req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
resp.sendRedirect(req.getContextPath()+"/sys/success.jsp");
}
else{
//登录失败
resp.sendRedirect(req.getContextPath()+"/error.jsp");
//因为重定向的根目录是站点目录,因此要在路径前面加上当前web应用的路径:req.getContextPath(),不然就会出现404错误
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
过滤器:
SysFilter(用户登录之后才能进入主页)
package com.xiaozhao.filter;
import com.mysql.fabric.jdbc.FabricMySQLConnection;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 小龚
* @create 2022-09-08 23:10
*/
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 {
//HttpServlet和Servlet不一样,需要强转
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
Object user_session = req.getSession().getAttribute("USER_SESSION");
//如果user_session为空,说明没有登录成功,那就不能直接进入success.jsp界面
if(user_session==null){
resp.sendRedirect("../error.jsp");
}
filterChain.doFilter(servletRequest,servletResponse);//这句话必写,不然会在这里卡死
}
@Override
public void destroy() {
}
}
视图:
错误页面:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>错误</h1>
<h3>没有权限,用户名错误</h3>
<a href="login.jsp">返回登录页面</a>
</body>
</html>
成功页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>小赵欢迎你</h1>
<p><a href="../servlet/loginout">注销</a></p>
</body>
</html>
登录页面
<%@ 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>
web.xml设置
过滤器:
<filter>
<filter-name>SysFiltler</filter-name>
<filter-class>com.xiaozhao.filter.SysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SysFiltler</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>
控制器
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.xiaozhao.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginOutServlet</servlet-name>
<servlet-class>com.xiaozhao.servlet.LoginOutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginOutServlet</servlet-name>
<url-pattern>/servlet/loginout</url-pattern>
</servlet-mapping>
测试:
登录界面
登录成功界面
点击注销后再次访问成功页面
显示失败(过滤器的作用)