前言:
本文内容:监听器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;
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 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;
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;
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;
public class LoginFilter implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
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() { } }
|