过滤器
所谓的过滤器就是用来过滤前端发送过来的请求的数据信息的,就比如现在我们想要改变响应内容的编码,我们肯定是在Servlet中设置编码格式,但是这样的话,我们每次都要写那几句话,我们当然希望有一个中间件帮我们做了一劳永逸,这个时候过滤器就来了,包括什么密码校验都是通过这个做的
这里我们简单写了一个Demo
package com.mrli.servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import java.io.IOException;
public class Demo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, IOException {
resp.getWriter().write("你好世界!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
现在我们通过过滤器来做,我们写Servlet我们需要实现Servlet接口,我们要使用过滤器当然也要实现Filter接口
这里需要注意,如果你使用的Tomcat10以上的版本,所有的javax包都被改成了jakarta,具体请看这一篇 https://blog.csdn.net/APCSZDDXM/article/details/122607014?spm=1001.2014.300
package com.mrli.servlet;
import jakarta.servlet.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class CharacterEncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.printf("CharacterEncodingFilter初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, UnsupportedEncodingException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
System.out.println("前");
//这里一定要用filterChain调用它自己的doFilter对象让代码往下走,否则代码执行到这里就会停住,因为我们是可以有多个过滤器的
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("后");
}
@Override
public void destroy() {
System.out.printf("CharacterEncodingFilter销毁");
}
}
注册一下过滤器
使用了jakarta包之后,filter-class会报红,目前我也暂时没有找到原因,但是Tomcat可以正常运行。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>demo</servlet-name>
<servlet-class>com.mrli.servlet.Demo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/servlet/demo</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.mrli.servlet.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
</web-app>
监听器
package com.mrli.servlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;
public class ListenerServlet implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer onlinePeople= (Integer) servletContext.getAttribute("OnlinePeople");
if(onlinePeople==null){
onlinePeople=new Integer(1);
}else{
int i = onlinePeople.intValue();
onlinePeople=new Integer(i+1);
}
servletContext.setAttribute("OnlinePeople",onlinePeople);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer onlinePeople= (Integer) servletContext.getAttribute("OnlinePeople");
if(onlinePeople==null){
onlinePeople=new Integer(0);
}else{
int i = onlinePeople.intValue();
onlinePeople=new Integer(i-1);
}
servletContext.setAttribute("OnlinePeople",onlinePeople);
//session的销毁方式有两种:
//1.手动销毁 invalidate()
//2.自动销毁 在web.xml中配置<session-config>
}
}
这个监听器也是需要注册的
<listener>
<listener-class>com.mrli.servlet.ListenerServlet</listener-class>
</listener>
给一个窗口添加事件监听
package com.mrli.servlet;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();//创建一个窗口
Panel panel = new Panel(null);//创建一个面板,并使用null布局
frame.setLayout(null);//窗口的布局使用null
frame.setBounds(0,0,400,400);//设置窗口的初始位置和大小
frame.setBackground(new Color(125,200,0));//设置窗口的背景颜色
panel.setBounds(50,50,200,200);//设置面板的初始位置和大小
panel.setBackground(new Color(125,0,125));//设置面板的背景颜色
frame.add(panel);//将面板添加到窗口中
frame.setVisible(true);//是窗口可见
//给窗口添加监听
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭ing");
System.exit(0);//正常推出程序
}
});
}
}