过滤器是什么玩意?

所谓过滤器,其实就是一个服务端组件,用来截取用户端的请求与响应信息。
过滤器的应用场景:
1.对用户请求进行统一认证,保证不会出现用户账户安全性问题
2.编码转换,可在服务端的过滤器中设置统一的编码格式,避免出现乱码
3.对用户发送的数据进行过滤替换
4.转换图像格式
5.对响应的内容进行压缩
其中,第1,2场景经常涉及。
login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'login.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> <form action="<%=path %>/servlet/LoginServlet" method="post" > 用户名:<input type="text" name="username" /> 密码:<input type="password" name="password" /> <input type="submit" value="登录" /> </form> </body> </html>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=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 'index.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> </body> </html>
failure.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'login.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> 登录失败,请检查用户名或密码! </body> </html>
LoginFilter.java
package com.cityhuntshou.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;
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
HttpSession session = request.getSession();
//过滤器实际应用场景之二-----编码转换
String charset = config.getInitParameter("charset");
if(charset == null)
{
charset = "UTF-8";
}
request.setCharacterEncoding(charset);
String noLoginPaths = config.getInitParameter("noLoginPaths");
if(noLoginPaths != null)
{
String[] strArray = noLoginPaths.split(";");
for(int i = 0; i < strArray.length; i++)
{
//空元素,放行
if(strArray[i] == null || "".equals(strArray[i]))
continue;
if(request.getRequestURI().indexOf(strArray[i]) != -1)
{
arg2.doFilter(arg0, arg1);
return;
}
}
}
if(request.getRequestURI().indexOf("login.jsp") != -1
|| request.getRequestURI().indexOf("LoginServlet") != -1)
{
arg2.doFilter(arg0, arg1);
return;
}
if(session.getAttribute("username") != null)
{
arg2.doFilter(arg0, arg1);
}
else
{
response.sendRedirect("login.jsp");
}
}
public void init(FilterConfig arg0) throws ServletException {
config = arg0;
}
}
LoginServlet.java
package com.cityhuntshou.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
//new String(username.getBytes("ISO-8859-1"),"UTF-8")
System.out.println(username);
if("admin".equals(username) && "admin".equals(password))
{
//校验通过
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect(request.getContextPath()+"/success.jsp");
}
else
{
//校验失败
response.sendRedirect(request.getContextPath()+"/failure.jsp");
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.cityhuntshou.servlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.cityhuntshou.filter.LoginFilter</filter-class>
<init-param>
<param-name>noLoginPaths</param-name>
<param-value>login.jsp;failure.jsp;loginServlet</param-value>
</init-param>
<init-param>
<param-name>charset</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
运行效果:
访问结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Java
# web
# 过滤器
# java web过滤器处理乱码
# JavaWeb之Filter过滤器详解
# Java web过滤器验证登录防止未登录进入界面
# Java Web Filter 过滤器学习教程(推荐)
# javaweb中Filter(过滤器)的常见应用
# 传智播客java web 过滤器
# 服务端
# 可在
# 之二
# 用户发送
# 大家多多
# 实际应用
# 性问题
# 请检查
# 器中
# 出现乱码
# base
# href
# head
# Transitional
# EN
# meta
# http
# equiv
# starting
# pragma
相关文章:
如何通过FTP服务器快速搭建网站?
建站之星如何开启自定义404页面避免用户流失?
建站之星官网登录失败?如何快速解决?
建站之星五站合一营销型网站搭建攻略,流量入口全覆盖优化指南
如何快速搭建FTP站点实现文件共享?
制作网站的模板软件,网站怎么建设?
开心动漫网站制作软件下载,十分开心动画为何停播?
家庭服务器如何搭建个人网站?
黑客如何通过漏洞一步步攻陷网站服务器?
东莞专业制作网站的公司,东莞大学生网的网址是什么?
如何通过IIS搭建网站并配置访问权限?
怎么将XML数据可视化 D3.js加载XML
高端智能建站公司优选:品牌定制与SEO优化一站式服务
深圳网站制作培训,深圳哪些招聘网站比较好?
头像制作网站在线制作软件,dw网页背景图像怎么设置?
整人网站在线制作软件,整蛊网站退不出去必须要打我是白痴才能出去?
如何通过宝塔面板实现本地网站访问?
制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?
定制建站方案优化指南:企业官网开发与建站费用解析
如何快速使用云服务器搭建个人网站?
义乌企业网站制作公司,请问义乌比较好的批发小商品的网站是什么?
成都网站制作报价公司,成都工业用气开户费用?
如何用花生壳三步快速搭建专属网站?
音乐网站服务器如何优化API响应速度?
网站好制作吗知乎,网站开发好学吗?有什么技巧?
免费制作海报的网站,哪位做平面的朋友告诉我用什么软件做海报比较好?ps还是cd还是ai这几个软件我都会些我是做网页的?
建站主机是什么?如何选择适合的建站主机?
建站主机如何安装配置?新手必看操作指南
活动邀请函制作网站有哪些,活动邀请函文案?
Python路径拼接规范_跨平台处理说明【指导】
如何快速搭建自助建站会员专属系统?
电商网站制作公司有哪些,1688网是什么意思?
建站之星后台管理:高效配置与模板优化提升用户体验
如何在阿里云ECS服务器部署织梦CMS网站?
如何用PHP快速搭建高效网站?分步指南
建站主机与虚拟主机有何区别?如何选择最优方案?
c++如何打印函数堆栈信息_c++ backtrace函数与符号名解析【方法】
如何用腾讯建站主机快速创建免费网站?
广东专业制作网站有哪些,广东省能源集团有限公司官网?
网站制作壁纸教程视频,电脑壁纸网站?
网站制作话术技巧,网站推广做的好怎么话术?
如何在七牛云存储上搭建网站并设置自定义域名?
深圳网站制作平台,深圳市做网站好的公司有哪些?
简易网站制作视频教程,使用记事本编写一个简单的网页html文件?
子杰智能建站系统|零代码开发与AI生成SEO优化指南
猪八戒网站制作视频,开发一个猪八戒网站,大约需要多少?或者自己请程序员,需要什么程序员,多少程序员能完成?
韩国网站服务器搭建指南:VPS选购、域名解析与DNS配置推荐
单页制作网站有哪些,朋友给我发了一个单页网站,我应该怎么修改才能把他变成自己的呢,请求高手指点迷津?
如何通过VPS建站实现广告与增值服务盈利?
广州建站公司哪家好?十大优质服务商推荐
*请认真填写需求信息,我们会在24小时内与您取得联系。