1.监听器
1.1定义:
相当于地铁安检,检测到某一事件触发后触发
是指专门用于对其他对象身上发生的事情或状态改变进行监听和相应处理的对象,当监听对象发生变化时,立即采取相应的行动。
1.2语法:
创建Servlet的监听器Listener
1.3常用监听器:
HttpSessionListener
1.4实现步骤:
1.4.1:创建工程并导入包
创建一个javaweb工程,导入jar包
1.4.2:创建Servlet文件和配置web.xml
创建一个servlet文件,
package 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 UserServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置字符
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String action = request.getParameter("action");
if (action.equals("index")){
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
}
1.4.3util/UserCountListener监听器创建
在工具类包util包中创建一个java文件,实现HttpSessionListener接口
package util;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class UserCountListener implements HttpSessionListener {
//定义一个全局变量,存储用户访问的个数,初始值0
private Integer countNumber = 0;
//session创建时触发
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("session创建时候触发");
countNumber++;
//绑定值
httpSessionEvent.getSession().getServletContext().setAttribute("countNumber",countNumber);
}
//session销毁时触发
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("session销毁时触发");
countNumber--;
httpSessionEvent.getSession().getServletContext().setAttribute("countNumber",countNumber);
}
}
1.4.4配置监听器web.xml
<listener>
<listener-class>util.UserCountListener</listener-class>
</listener>
1.4.5index.jsp
在index.jsp中添加测试语句
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<p>在线人数:${countNumber}</p>
</body>
</html>
1.4.6访问测试
http://localhost:8080/UserServlet?action=index
不同的浏览器访问会使得访问人数+1,如两个不同的浏览器访问,此时的在线人数就是2,即两个session被创建,但是现在直接关掉某个浏览器,刷新另外的浏览器,人数不会减少
1.4.7优化servlet
此时添加session失效的逻辑
package 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 UserServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置字符
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String action = request.getParameter("action");
if (action.equals("index")){
request.getRequestDispatcher("index.jsp").forward(request,response);
} else if (action.equals("loginOut")){
//使得session失效
request.getSession().invalidate();
}
}
}
<body>
<p>在线人数:${countNumber}</p>
<a href="UserServlet?action=loginOut">退出</a>
</body>
点击链接退出后,此时的session失效,使得countNumber减少1
2.过滤器
2.1定义:
向Web应用程序的请求和响应添加功能的Web服务组件,它可以统一集中请求和响应
例如过滤嘴香烟中的过滤嘴
2.2语法:
1)创建Filter接口类
2)doFilter(){chain.doFilter(request,response);}//过滤请求
3)web.xml配置过滤器
2.3实现步骤
2.3.1创建工程和导入包
先实现没有过滤器的功能
2.3.2创建UserServlet并配置web.xml和index.jsp和login.jsp
UserServlet
package 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 UserServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
request.setAttribute("name", name);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="UserServlet" method="post">
<table>
<tr>
<td>名称</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
${name}
</body>
</html>
此时传入中文会出现乱码,可以在servlet中添加设置字符集,但是servlet文件多的时候就会代码冗余
2.3.3创建过滤器改进
在util工具类中创建DemoFilter.java
导入的包要注意:import javax.servlet.*;
package util;
import javax.servlet.*;
import java.io.IOException;
public class DemoFilter 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 {
System.out.println("过滤器执行请求");
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
//销毁过滤器
@Override
public void destroy() {
System.out.println("过滤器的销毁");
}
}
2.3.4配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>DemoFilter</filter-name>
<filter-class>util.DemoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>DemoFilter</filter-name>
<url-pattern>/UserServlet</url-pattern>
</filter-mapping>
和servlet类似,最后的/后面的是要过滤的servlet文件的路径(文件名)
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
</web-app>
2.3.5访问测试
此时的中文就不出现乱码,并且不用在每个servlet文件中添加字符集等
2.3.6多servlet文件的过滤器匹配
添加通配符*
此时就可以一个过滤器过滤多个servlet文件,或者可以在路径下面添加不同的访问分类来区分,使得过滤器识别不同分类
例如:A/UserServlet和A/PersonServlet,过滤器选择A/*
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>DemoFilter</filter-name>
<filter-class>util.DemoFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>DemoFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>servlet.UserServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>PersonServlet</servlet-name>
<servlet-class>servlet.PersonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PersonServlet</servlet-name>
<url-pattern>/PersonServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/UserServlet</url-pattern>
</servlet-mapping>
</web-app>