Filter :过滤器,用来过滤网站的数据:
案例
public class ShowServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// resp.setCharacterEncoding("utf-8");
// req.setCharacterEncoding("utf-8");
// resp.setContentType("text/html;charset=utf-8");
resp.getWriter().write("今天是4月13日");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
package com.srx.servlet.filter;
import javax.servlet.*;
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
//初始化 web服务器启动 就已经初始化了,等待过滤的对象
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter正在初始化");
}
//Chain
// 1.过滤所有的代码,在过滤特定请求的时候会执行
// 2.必须要让过滤器维持同行
//filterChain.doFilter(servletRequest,servletResponse);
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
System.out.println("CharacterEncodingFilter执行前");
// 让我们的请求继续走 ,如果不写 程序这里就会被拦截
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("CharacterEncodingFilter执行后");
}
//销毁
public void destroy() {
System.out.println("CharacterEncodingFilter正在销毁");
}
}
xml配置
<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">
<servlet>
<servlet-name>ShowServletservlet-name>
<servlet-class>com.srx.servlet.servlet.ShowServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>ShowServletservlet-name>
<url-pattern>/showurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>ShowServletservlet-name>
<url-pattern>/servlet/showurl-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>com.srx.servlet.filter.CharacterEncodingFilterfilter-class>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/servlet/*url-pattern>
filter-mapping>
web-app>
监听器
//统计网站在线人数:统计session
public class OnlineCountListener implements HttpSessionListener {
//创建Session监听
//一单创建session就会触发这个事件
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext context1 = httpSessionEvent.getSession().getServletContext();
//System.out.println(httpSessionEvent.getSession().getId());
Integer onlineCount = (Integer)context1.getAttribute("onlineCount");
if (onlineCount!=null){
System.out.println("11111");
int count = onlineCount.intValue();
onlineCount= new Integer(count+1);
} else {
onlineCount= new Integer(1);
}
System.out.println(onlineCount);
context1.setAttribute("onlineCount",onlineCount);
}
//销毁session监听
//一旦销毁Session就会触发一次这个事件
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext context = httpSessionEvent.getSession().getServletContext();
Integer onlineCount = (Integer)context.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount= new Integer(0);
}
else {
int count = onlineCount.intValue();
onlineCount= new Integer(count-1);
}
context.setAttribute("onlineCount",onlineCount);
}
//
// session销毁
// 1. 手动销毁 getSession().invalidate();
// 2. 自动销毁
}
xml
<listener>
<listener-class>com.srx.servlet.listener.OnlineCountListenerlistener-class>
listener>
<session-config>
<session-timeout>1session-timeout>
session-config>
jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
在线人数为 <%= this.getServletConfig().getServletContext().getAttribute("onlineCount") %> 人
用户登录之后才能进入主页!用户注销后就不能进入主页了!
登录jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
错误jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
错误
登录失败,请检查你的账号或密码
重新登录
成功jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
主页
注销
登录的servlet
package com.srx.servlet.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 LoginDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String useranme = req.getParameter("username");
System.out.println(useranme);
if (useranme.equals("admin")){
System.out.println(useranme);
req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
System.out.println("01010212313131");
resp.sendRedirect("/sys/Success.jsp");
System.out.println("0101001");
}else {
resp.sendRedirect("/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注销的servlet
package com.srx.servlet.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 LoginoutDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object userSession = req.getSession().getAttribute("USER_SESSION");
if (userSession!=null){
req.getSession().removeAttribute("USER_SESSION");
resp.sendRedirect("/Login.jsp");
}
else {
resp.sendRedirect("/Login.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
过滤器的servlet
package com.srx.servlet.listener;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class sysFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest Request1 = (HttpServletRequest) servletRequest;
HttpServletResponse Response1 = (HttpServletResponse) servletResponse;
if (Request1.getSession().getAttribute("USER_SESSION")==null){
Response1.sendRedirect("/error.jsp");
}
filterChain.doFilter(Request1,Response1);
}
public void destroy() {
}
}
xml配置
<servlet>
<servlet-name>loginservlet-name>
<servlet-class>com.srx.servlet.servlet.LoginDemoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>loginservlet-name>
<url-pattern>/servlet/loginurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>loginoutservlet-name>
<servlet-class>com.srx.servlet.servlet.LoginoutDemoservlet-class>
servlet>
<servlet-mapping>
<servlet-name>loginoutservlet-name>
<url-pattern>/servlet/loginouturl-pattern>
servlet-mapping>
<filter>
<filter-name>sysFilterfilter-name>
<filter-class>com.srx.servlet.listener.sysFilterfilter-class>
filter>
<filter-mapping>
<filter-name>sysFilterfilter-name>
<url-pattern>/sys/*url-pattern>
filter-mapping>