簡單的Filter實現
1.在xml檔案配置過濾器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Filter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>com.test.filter.CharsetFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.test.filter.LoginFilter</filter-class>
<init-param>
<param-name>noLoginPath</param-name>
<param-value>login.jsp;error.jsp;index.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter>
<filter-name>ErrorFilter</filter-name>
<filter-class>com.test.filter.ErrorFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ErrorFilter</filter-name>
<url-pattern>/error.jsp</url-pattern>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.建立java類實現Filter介面
1.編碼轉換案例
package com.test.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharsetFilter implements Filter{
private FilterConfig config;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String charset = config.getInitParameter("charset");
if(charset == null || charset.equals("")){
charset = "UTF-8";
}
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
config = arg0;
}
}
2.錯誤顯示案例
package com.test.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ErrorFilter implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
System.out.println("error!!!!");
chain.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
3.登入過濾案例
package com.test.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter implements Filter{
private FilterConfig config;
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
HttpSession session = request.getSession();
//獲取不用過濾的路徑
String noLoginPath = config.getInitParameter("noLoginPath");
//對於不用過濾的路徑直接放行
if(noLoginPath != null){
String[] strArray = noLoginPath.split(";");
for(String str:strArray){
if(str == null || str.equals("")) continue;
if(request.getRequestURI().indexOf(str) != -1){
chain.doFilter(arg0, arg1);
return;
}
}
}
//如果session中沒有使用者資訊就跳轉到登陸介面,否則放行
if(session.getAttribute("user") == null){
response.sendRedirect("login.jsp");
}else{
chain.doFilter(arg0, arg1);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
config = arg0;
}
}
3.建立過濾後顯示的頁面
1.編碼轉換案例
package com.test.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharsetFilter implements Filter{
private FilterConfig config;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String charset = config.getInitParameter("charset");
if(charset == null || charset.equals("")){
charset = "UTF-8";
}
request.setCharacterEncoding(charset);
response.setCharacterEncoding(charset);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
config = arg0;
}
}
2.錯誤顯示頁面
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my error page. <br>
</body>
</html>
3.登入過濾頁面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'success.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<span><%=request.getParameter("userName") %></span>
<%System.out.println("userName is "+request.getParameter("userName")); %>
<span><%=request.getParameter("password") %></span>
<%System.out.println("pssword is "+request.getParameter("password")); %>
</body>
</html>
相關文章
- 驚呆了,Servlet Filter和Spring MVC Interceptor的實現居然這麼簡單ServletFilterSpringMVC
- 實現簡單的BitMap
- ArrayList的簡單實現
- AOP的簡單實現
- 簡單的 HashMap 實現HashMap
- Promise的簡單實現Promise
- jQuery filter() 用法簡單介紹jQueryFilter
- 簡單探索Python中的filter函式PythonFilter函式
- 簡單的實現vue原理Vue
- 簡單的實現React原理React
- java實現簡單的JDBCJavaJDBC
- php實現簡單的SQLBuilderPHPSQLUI
- kd樹的簡單實現
- Promise 簡單實現Promise
- FastClick簡單實現AST
- Express 簡單實現Express
- AspectJ簡單實現
- getElementsByClassName簡單實現
- springboot專案結合filter,jdk代理實現敏感詞過濾(簡單版)Spring BootFilterJDK
- java實現簡單的單點登入Java
- jQuery實現的簡單投票簡單程式碼例項jQuery
- 互斥鎖mutex的簡單實現Mutex
- React + Antd實現簡單的todolistReact
- Unity實現簡單的物件池Unity物件
- Promise 基本方法的簡單實現Promise
- java實現棧的簡單操作Java
- 簡單、好懂的Svelte實現原理
- 前端MVC、MVVM的簡單實現前端MVCMVVM
- Promise的使用及簡單實現Promise
- Nginx反向代理的簡單實現Nginx
- 簡單通訊錄的實現
- 用go 簡單實現的LRUGo
- BAPI的簡單實現步驟API
- 精簡版 koa 簡單實現
- 感知機簡單實現
- 簡單版Promise實現Promise
- 簡單實現vuex原理Vue
- React 簡單實現(一)React