🎃过滤器与监听器
目录
一、过滤器Filter 二、监听器 三、两种配置方式
一、过滤器Filter
?作用:编码过滤和权限控制 ?实现方式:继承类HttpServlet 或实现接口Filter ?配置方式:通过注解或web.xml中进行配置 ?注意:web.xml中配置和注解配置里不要加“/"
1、编码过滤
案例演示: EncodingFilter.java过滤器文件:》》》
@WebFilter(value = "*")
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化..................");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) servletRequest;
HttpServletResponse resp=(HttpServletResponse) servletResponse;
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
filterChain.doFilter(req,resp);
}
@Override
public void destroy() {
System.out.println("销毁................");
}
}
测试文件Servlet:>>>测试中文是否乱码
@WebServlet("/test1")
public class Servlet01 extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name=req.getParameter("name");
System.out.println(name);
resp.getWriter().println(name);
}
}
结果:》》》
2、权限控制
- 通过权限的控制,使得不登录无法访问其他页面以及后台信息,登录成功则转发至成功界面,否则重新转发至login.jsp登录界面。
权限控制文件PowerFilter.java:>>>
@WebFilter("*")
public class PowerFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化权限设置");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) servletRequest;
HttpServletResponse resp=(HttpServletResponse) servletResponse;
String name=req.getParameter("uname");
String psw=req.getParameter("psw");
if(name!=null && psw!=null){
filterChain.doFilter(req,resp);
}
String uri=req.getRequestURI();
if(uri.contains("login.jsp")){
filterChain.doFilter(req,resp);
}
if(uri.contains("Login")){
filterChain.doFilter(req,resp);
}
if(uri.contains("test1")){
filterChain.doFilter(req,resp);
}
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
@Override
public void destroy() {
}
}
登录界面login.java>>>
@WebServlet("/login")
public class Login extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name=req.getParameter("uname");
String psw=req.getParameter("psw");
if("admin".equals(name)&& "admin".equals(psw)){
req.setAttribute("name",name);
req.setAttribute("psw",psw);
req.getRequestDispatcher("true.jsp").forward(req,resp);
}else{
req.getRequestDispatcher("login.jsp?msg=登录失败").forward(req,resp);
}
}
}
登录界面login.jsp>>>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<h4>${param.msg}</h4>
<hr>
<form action="login" method="post">
用户名:<input type="text" name="uname">
密码:<input type="password" name="psw">
<input type="submit" value="登录">
</form>
</body>
</html>
测试结果:>>>
二、监听器
案列演示:》》》监听在线人数
Linterner.java监听器:>>>
@WebListener
public class Listerner implements HttpSessionListener {
private Integer num=0;
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
num++; httpSessionEvent.getSession().getServletContext().setAttribute("num",num);
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
num--;
httpSessionEvent.getSession().getServletContext().setAttribute("num",num);
}
}
在线显示页面:>>>
@WebServlet("/login")
public class Login extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
String key= req.getParameter("key");
if(key!=null && "out".equals(key)){
req.getSession().invalidate();
return;
}
Integer num=(Integer) req.getSession().getServletContext().getAttribute("num");
resp.getWriter().println("<h4>目前在线人数:"+num+"</h4><a href='login?key=out'>退出</a>");
}
}
测试结果:
三、两种配置方式
1.方式一:注解
eg:很简单
@WebServlet("/login")
2.方式二:web.xml文件配置
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>filter1</filter-name>
<filter-class>EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
</web-app>
|