11.Filter过滤器
11.1 Filter过滤器介绍
11.2 编写过滤器步骤
public class CharacterEncoding implements Filter {
/*初始化:web服务器启动时初始化*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化");
}
/*过滤所有的代码,在过滤特定的请求时会执行
* 必须要让过滤器继续执行*/
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
/*多个过滤器链交接,让请求继续向下传送,否则被拦截在此*/
chain.doFilter(req,resp);
}
/*销毁:web服务器关闭时销毁*/
@Override
public void destroy() {
System.out.println("过滤器销毁");
}
}
- 在web.xml中配置filter过滤器
<filter>
<filter-name>CharacterEncoding</filter-name>
<filter-class>com.zk.filter.CharacterEncoding</filter-class>
</filter>
<!--尽量不用/*对所有请求过滤,可以写多个过滤路径-->
<filter-mapping>
<filter-name>CharacterEncoding</filter-name>
<url-pattern>/ds01</url-pattern>
<!-- <url-pattern>/*</url-pattern>-->
</filter-mapping>
11.3 常用场景
-
登录成功后进入主页,否则回退到登录页
-
- 用户登录之后,向session中存入用户数据
- 进入主页时判断用户是否已经登录,要求在过滤器中实现
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse resp = (HttpServletResponse)response; String session_id = (String) req.getSession().getAttribute(Constant.Session_id); if (session_id == null) { System.out.println("过滤时返回login.jsp"); resp.sendRedirect("/error.jsp"); } chain.doFilter(req,resp); }
-
优化将常量转为实体类静态常量进行维护
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
if (username.equals("admin")) {
req.getSession().setAttribute(Constant.Session_id,req.getSession().getId());
resp.sendRedirect("/sys/success.jsp");
}else {
resp.sendRedirect("/error.jsp");
}
}
public class Constant {
public static final String Session_id = "Session_id";
}
12.监听器
12.1 监听器编写
-
实现监听器接口
-
重写里面的方法
public class ListenterDemo0 implements HttpSessionListener {
/*创建session监听
* 观察者模式:一旦创建session就会触发这个事件*/
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
System.out.println(se.getSession().getId());
Integer usercount = (Integer) servletContext.getAttribute("usercount");
if (usercount==null) {
usercount = new Integer(1);
}else {
usercount +=1;
}
servletContext.setAttribute("usercount",usercount);
}
/*一旦销毁session就会触发这个事件*/
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer usercount = (Integer) servletContext.getAttribute("usercount");
if (usercount==null) {
usercount = new Integer(0);
}else {
usercount -=1;
}
servletContext.setAttribute("usercount",usercount);
}
}
- web.xml中配置监听器
<listener>
<listener-class>com.zk.listenter.ListenterDemo0</listener-class>
</listener>
- 多人在线是因为关闭浏览器session不会销毁,只是浏览器cookie存的sessionid没了,所以再访问会重新创建一个session,原来的session要等超时或者重启服务器才会消失
12.2 常见场景
- GUI编程中使用到
public class FrameDemo02 {
public static void main(String[] args) {
Frame frame = new Frame("xx游戏");
Panel panel = new Panel(null);
panel.setLayout(null);
frame.setBounds(20,20,300,300);
frame.setBackground(Color.LIGHT_GRAY);
panel.setBounds(50,50,200,200);
panel.setBackground(new Color(0,30,0));
frame.add(panel);
frame.setVisible(true);
/*frame.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("打开");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("关闭中");
System.exit(0); //0~1正常、非正常关闭
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("关闭后");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("Iconified");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("Deiconified");
}
@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.exit(0); //0~1正常、非正常关闭
}
});
}
}
13.junit单元测试
- 导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/123936.html