过滤器
filter:过滤器用来过滤网站的数据
-
处理中文乱码
-
登陆验证
-
......
导入maven包
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.31</version> </dependency> </dependencies>
编写过滤器
public class CharacterEncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { //初始化:启动web服务器的时候初始化 System.out.println("CharacterEncodingFilter初始化"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { /* 1. 过滤器中的所有代码,再过滤特定的请求时都会执行 2. 必须要让过滤器持续执行 */ servletRequest.setCharacterEncoding("utf-8"); servletResponse.setCharacterEncoding("utf-8"); servletResponse.setContentType("text/html;charset=UTF-8"); System.out.println("before CharacterEncodingFilter"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("after CharacterEncodingFilter"); } @Override public void destroy() { //销毁:关闭web服务器的时候,过滤会销毁 System.out.println("CharacterEncodingFilter销毁"); } }
编写servlet
public class showServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("你好世界"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
配置servlet和过滤器
<servlet> <servlet-name>ShowServlet</servlet-name> <servlet-class>com.gu.servlet.showServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/servlet/show</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ShowServlet</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.gu.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/servlet/*</url-pattern> </filter-mapping>
在配置时,对servlet设置两个路径/servlet/show和/show
而过滤器只过滤/servlet/*下的内容
结果
在/servlet/show可以成功转义成中文,而/show还是乱码
监听器
//统计网站在线人数,统计session public class OnlineListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { //创建session监听 //一旦创建一个session,会触发一次这个事件 ServletContext servletContext = httpSessionEvent.getSession().getServletContext(); System.out.println(httpSessionEvent.getSession().getId()); Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount"); if (onlineCount == null){ onlineCount = new Integer(1); }else { int count = onlineCount.intValue(); onlineCount = new Integer(count+1); } servletContext.setAttribute("OnlineCount",onlineCount); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { //销毁session监听 ServletContext servletContext = httpSessionEvent.getSession().getServletContext(); Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount"); if (onlineCount == null){ onlineCount = new Integer(1); }else { int count = onlineCount.intValue(); onlineCount = new Integer(count-1); } servletContext.setAttribute("OnlineCount",onlineCount); } }
展示:
<body> <h1>当前有 <span style="color: cornflowerblue"><%=request.getSession().getAttribute("onlineCount")%></span>在线</h1> </body>
over
标签:day107,public,监听器,过滤器,Integer,servlet,onlineCount,CharacterEncodingFilter From: https://www.cnblogs.com/GUGUZIZI/p/17435042.html