过滤器的使用

Posted jokerzou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过滤器的使用相关的知识,希望对你有一定的参考价值。

过滤器Filter的工作原理:

技术图片

 

过滤前英文登录界面:

技术图片

过滤前英文登录结果:

技术图片

过滤后中文登录界面:

技术图片

过滤后中文登录结果:

技术图片

 

项目结构:

技术图片

 

技术图片
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP ‘index.jsp‘ starting page</title>
13     <meta http-equiv="pragma" content="no-cache">
14     <meta http-equiv="cache-control" content="no-cache">
15     <meta http-equiv="expires" content="0">    
16     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
17     <meta http-equiv="description" content="This is my page">
18     <!--
19     <link rel="stylesheet" type="text/css" href="styles.css">
20     -->
21   </head>
22   
23   <body>
24       <div>
25     请登录
26         <div>
27             <form action="ShowLoginInfo" id="eee" method="get">
28                 <br><br>
29                 账号:<input type="text" name="uname">
30                 <br><br><br>
31                 密码: <input type="password" name="upassword">
32                 <br><br>
33                 <button type="submit" id="btn">登录</button>
34             </form>
35         </div>
36     </div>
37   </body>
38 </html>
index.jsp
技术图片
 1 package com.a;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 /**
13  * Servlet implementation class ShowLoginInfo
14  */
15 @WebServlet("/ShowLoginInfo")
16 public class ShowLoginInfo extends HttpServlet {
17     private static final long serialVersionUID = 1L;
18        
19     /**
20      * @see HttpServlet#HttpServlet()
21      */
22     public ShowLoginInfo() {
23         super();
24         // TODO Auto-generated constructor stub
25     }
26 
27     /**
28      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
29      */
30     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31         //从请求对象中获得指定参数的值
32         String strName = request.getParameter("uname");
33         String strPassWord = request.getParameter("upassword");
34         PrintWriter out = response.getWriter();
35         out.println("你的名字是:"+strName+"<br>");
36         out.println("你的密码是:"+strPassWord);
37     }
38 
39     /**
40      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
41      */
42     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
43         // TODO Auto-generated method stub
44         doGet(request, response);
45     }
46 
47 }
ShowLoginInfo.java(Servlet)
技术图片
 1 package com.b;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10 import javax.servlet.annotation.WebFilter;
11 
12 /**
13  * Servlet Filter implementation class myFilter
14  */
15 @WebFilter(filterName="myFilter",urlPatterns="/ShowLoginInfo")
16 public class myFilter implements Filter {
17 
18     /**
19      * Default constructor. 
20      */
21     public myFilter() {
22         // TODO Auto-generated constructor stub
23     }
24 
25     /**
26      * @see Filter#destroy()
27      */
28     public void destroy() {
29         // TODO Auto-generated method stub
30     }
31 
32     /**
33      * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
34      */
35     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
36         request.setCharacterEncoding("UTF-8");
37         response.setContentType("text/html; charset=UTF-8");
38         chain.doFilter(request, response);
39     }
40 
41     /**
42      * @see Filter#init(FilterConfig)
43      */
44     public void init(FilterConfig fConfig) throws ServletException {
45         // TODO Auto-generated method stub
46     }
47 
48 }
myFilter.java(Filter)

 

 

链接:https://pan.baidu.com/s/1SkcrCk3E-ApV58MGEeTqHA
提取码:28qh
复制这段内容后打开百度网盘手机App,操作更方便哦

以上是关于过滤器的使用的主要内容,如果未能解决你的问题,请参考以下文章

Huawei_Netconf_Ncclient

基于内部片段的graphql过滤器(gatsbyJS)

CPNtools协议建模安全分析---实例变迁标记

在底部导航栏中保存片段状态

js简洁代码片段

在Pandoc lua过滤器中连接字符串片段