过滤器

来源:互联网 发布:网络灰色推广 编辑:程序博客网 时间:2024/04/28 00:55

这个程序调了两天才调出来,并不是有多难,而是很多小地方都没有注意。以后一定要细心,注意不要打错字,不要忘了载入工程,等等这些细节,有一个没注意到就会搞的焦头烂额。所以,一定要细心,不然会花上一百倍的时间来调试。

总结一下:

(1)首先,千万记得首先Deploy project to server,这次就是忘了部署了...真是惨痛的教训啊...重新载入工程都需要重新部署。

(2)public class FormFilter implements Filter

实现一个接口时一定要实现它的所有方法,不然会报错。The type FormFilter must implement the inherited abstract method Filter.init(FilterConfig)

(3)不能写成public abstract class FormFilter implements Filter

因为容器不会初始化抽象类,也就不能提供服务。

(4)ctrl+shift+O导入类,如果要自己选择的话需要仔细看,以免选错了。

 

代码如下:

FormFilter.java

 

FormCheck.jsp

 

<%@ 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>表单检验页面</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 method="post" action="CheckSuccess.jsp">
    <div align="center"><font size="5"><strong>
      使用过滤器检验表单数据</strong></font>
    </div>
    <p>
    <br>
    商品名称:
    <input type="text" name="name">
    <br>
    <br>
    商品数量:
    <input type="text" name="number">
    <br>
    <br>
    <input type="submit" value="提交">
    <input type="reset" value="取消">
   </form>
  </body>
</html>

 

CheckSuccess.jsp

 

<%@ 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>成功通过过滤校验</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>
    <div align="center"><font size="5"><strong>
      您提交的数据如下:<br><br><br></strong></font>
    </div>
    商品名称:
    <input type="text" name="name" value=<%=request.getParameter("name") %>
    readonly="true">
    <br><br>
    商品数量:
    <input type="text" name="number" value=<%=request.getParameter("number") %>
    readonly="true">
    <br><br><div align="center"><strong>输入数据成功通过了过滤器校验!</strong><br>
   
  </div></body>
</html>

 

CheckFail.jsp

 

<%@ 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>过滤</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>
    <div align="center"><font size="5"><strong>
      您提交的数据如下:<br><br><br></strong></font>
    </div>
    商品名称:
    <input type="text" name="name" value=<%=request.getParameter("name") %>
    readonly="true">
    <br><br>
    商品数量:
    <input type="text" name="number" value=<%=request.getParameter("number") %>
    readonly="true">
    <br><br><div align="center"><strong>输入数据没有通过过滤器校验!</strong><br>
   
  </div></body>
</html>

 

web.xml

到这里还没有结束,这个程序还有点小瑕疵,就是如果表单中输入中文就会出现乱码。解决方法就是在doFilter方法开头加上request.setCharacterEncoding("gb2312");因为FormCheck.jsp提交的表单信息先要经过过滤器FormFilter.java,然后才能到CheckSuccess.jsp或CheckFail.jsp。

 

原创粉丝点击