前言:

本文内容:监听器GUI理解、Filter实现权限拦截

推荐免费JavaWeb入门到实战视频:【狂神说Java】JavaWeb入门到实战_哔哩哔哩_bilibili

监听器GUI理解

通过GUI理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.jokerdig.listener;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author Joker大雄
* @data 2022/4/11 - 13:18
**/
public class TestPage {
public static void main(String[] args) {
Frame frame = new Frame("监听器的GUI理解");// 新建窗体
Panel panel = new Panel(null);// 面板
frame.setLayout(null);// 设置窗体布局

frame.setBounds(300,300,500,500);
frame.setBackground(new Color(0,123,211));

panel.setBounds(50,50,200,200);
panel.setBackground(new Color(183,0,122));
//添加面板到窗体
frame.add(panel);
frame.setVisible(true); // 显示

// 监听事件理解 一般不这样写
/*
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口打开事件");
}

@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭事件");
//System.exit(0);
}

@Override
public void windowClosed(WindowEvent e) {
System.out.println("窗口关闭后事件");
}

@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口激活事件");
}

@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("窗口未激活事件");
}
});
*/
// 监听关闭事件 一般这样写
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭窗口");
System.exit(0);
}
});
}
}

Filter实现权限拦截

例如:用户登录后才能进入页面,注销后和未登录都不能进入页面

实现步骤

  • 用户登录后,向Session中放入用户数据
  • 进入主页的时候要判断用户是否已经登录 (用过滤器实现)

sys/initial.jsp

1
2
3
4
5
6
7
8
9
10
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>这是首页,只有用户登录的时候才能进入哦</h1>
<h2><a href="${pageContext.servletContext.contextPath}/servlet/logout">退出登录</a></h2>
</body>
</html>

login.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form method="post" action="${pageContext.servletContext.contextPath}/servlet/login">
账号:<input type="text" name="username"><br>
密码:<input type="password" name="pwd"><br>
<input type="submit">
</form>
</body>
</html>

Web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<!--    配置登录-->
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.jokerdig.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<!-- 配置退出-->
<servlet>
<servlet-name>logout</servlet-name>
<servlet-class>com.jokerdig.servlet.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logout</servlet-name>
<url-pattern>/servlet/logout</url-pattern>
</servlet-mapping>
<!-- 配置登录过滤器-->
<filter>
<filter-name>loginfilter</filter-name>
<filter-class>com.jokerdig.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginfilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>

LoginServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.jokerdig.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 Joker大雄
* @data 2022/4/11 - 13:39
**/
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取前端请求参数
String username = req.getParameter("username");
String pwd = req.getParameter("pwd");
if(username.equals("admin") && pwd.equals("123")){
System.out.println("登录成功");
req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
resp.sendRedirect("../sys/initial.jsp");
} else {
System.out.println("登录失败");
resp.sendRedirect("../login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}

LogoutServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.jokerdig.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 Joker大雄
* @data 2022/4/11 - 13:57
**/
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session = req.getSession().getAttribute("USER_SESSION");
if(user_session!=null){
req.getSession().removeAttribute("USER_SESSION");
resp.sendRedirect("../login.jsp");
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}

LoginFilter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.jokerdig.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author Joker大雄
* @data 2022/4/11 - 14:03
**/
public class LoginFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

// 未登录和注销后不能直接进入首页
// ServletRequest HttpServletRequest 转换
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;

Object user_session = req.getSession().getAttribute("USER_SESSION");
if(user_session==null){
resp.sendRedirect("../login.jsp");
}
chain.doFilter(request,response);// 链
}
public void init(FilterConfig filterConfig) throws ServletException {

}
public void destroy() {
}
}