Filter 過濾器
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配置
<?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">
<servlet>
<servlet-name>ShowServlet</servlet-name>
<servlet-class>com.srx.servlet.servlet.ShowServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShowServlet</servlet-name>
<url-pattern>/show</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowServlet</servlet-name>
<url-pattern>/servlet/show</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.srx.servlet.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-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.OnlineCountListener</listener-class>
</listener>
<session-config>
<session-timeout>1</session-timeout>
</session-config>
jsp頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1> 在線人數為 <span> <%= this.getServletConfig().getServletContext().getAttribute("onlineCount") %></span> 人</h1>
</body>
</html>
過濾器的應用
用戶登錄之后才能進入主頁!用戶注銷后就不能進入主頁了!
- 用戶登錄之后 向session中放入用戶資料
- 進入主頁的時候判斷用戶是否已經登錄
登錄jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/servlet/login" method="post">
賬號:<input type="text" name="username">
<br>
<input type="submit" value="登錄">
</form>
</body>
</html>
錯誤jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>錯誤</h1>
<h3>登錄失敗,請檢查你的賬號或密碼</h3> <br>
<a href="/Login.jsp">重新登錄</a>
</body>
</html>
成功jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>主頁</h1>
<a href="/servlet/loginout">注銷</a>
</body>
</html>
登錄的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>login</servlet-name>
<servlet-class>com.srx.servlet.servlet.LoginDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/servlet/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>loginout</servlet-name>
<servlet-class>com.srx.servlet.servlet.LoginoutDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginout</servlet-name>
<url-pattern>/servlet/loginout</url-pattern>
</servlet-mapping>
<filter>
<filter-name>sysFilter</filter-name>
<filter-class>com.srx.servlet.listener.sysFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sysFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/279242.html
標籤:java
上一篇:GUI鍵盤監聽事件(java)
