Servlet过滤器

一、实验目的:

1. 了解过滤器的作用;

2. 掌握过滤器的开发与部署的步骤;

3. 了解过滤器链。

 

二、实验环境:

MyEclipse10+Tomcat 7.0+Java EE 6.0

 

三、实验内容:

   编写一个过滤器改变请求编码

【步骤 1】编写一个 loginform.html 文件,代码如下:

<!DOCTYPE html>

<html>

  <head>

    <title>使用过滤器改变请求编码</title>

    <meta http-equiv="Content-Type" content="text/html;charset=GB2312">

  </head>



  <body>

     <div align="center">

            <h2>请输入用户名和口令</h2>

            <form action="servlet/CheckParamServlet" method="post">

             <table>

               <tr>

                 <td>用户名:</td>

                 <td><input type="text" name="name" ></td>

               </tr>

               <tr>

                 <td>口令:</td>

                 <td><input type="password" name="pass"></td>

               </tr>

               <tr>

                 <td></td>

                 <td>

                 <input type="submit" name="ok" value="提交">

                 <input name="cancel" type="reset" value="重置">

                 </td>

               </tr>

             </table>

          </form>

     </div>

  </body>

</html>

【步骤 2】编写处理请求参数的 Servlet,新建CheckParamServlet.java文件,包名为servlet,代码如下:

//CheckParamServlet.java

package 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;



public class CheckParamServlet extends HttpServlet {



       /**

        * Constructor of the object.

        */

       public CheckParamServlet() {

              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 {



              String name=request.getParameter("name");

              String pass=request.getParameter("pass");

              response.setContentType("text/html;charset=gb2312");

              PrintWriter out=response.getWriter();

              out.println("<html><head><title>Param Test</title></head>");

              out.println("<h3 align=center>你的用户名为:"+name+"</h3>");

              out.println("<h3 align=center>您的口令为:"+pass+"</h3>");

              out.println("</body></html>");

       }



       /**

        * 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 {



              doGet(request, response);

       }



       /**

        * Initialization of the servlet. <br>

        *

        * @throws ServletException if an error occurs

        */

       public void init() throws ServletException {

              // Put your code here

       }



}

下面通过编写一个过滤器改变请求编码。

【步骤 3】新建EnCodingFilter.java文件,包名为filter,过滤器代码如下:

//EnCodingFilter.java

package 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 EnCodingFilter implements Filter {

       protected String encoding=null;

       protected FilterConfig config;

       public void init(FilterConfig filterConfig) throws ServletException {

              this.config = filterConfig;

              //得到在web.xml中配置的编码

              this.encoding = filterConfig.getInitParameter("Encoding");

       }

       public void doFilter (ServletRequest request,ServletResponse response,FilterChain chain)

              throws IOException,ServletException {

              if(request.getCharacterEncoding()==null) {

                     //得到指定的编码

                     String encode=getEncoding();

                     if(encode!=null) {

                            //设置request的编码

                            request.setCharacterEncoding(encode);

                            response.setCharacterEncoding(encode);

                     }

              }

              chain.doFilter(request, response);

       }

       protected String getEncoding() {

              return encoding;

       }

       public void destroy() {

       }

}

【步骤 4】修改 web.xml 文件,加入下面代码:

<servlet>

    <servlet-name>CheckParamServlet</servlet-name>

    <servlet-class>servlet.CheckParamServlet</servlet-class>

  </servlet>

<filter>

     <filter-name>EnCodingFilter</filter-name>

     <filter-class>filter.EnCodingFilter</filter-class>

     <init-param>

            <param-name>Encoding</param-name>

            <param-value>gb2312</param-value>

     </init-param>

  </filter>

<servlet-mapping>

    <servlet-name>CheckParamServlet</servlet-name>

    <url-pattern>/servlet/CheckParamServlet</url-pattern>

  </servlet-mapping>

<filter-mapping>

     <filter-name>EnCodingFilter</filter-name>

     <url-pattern>/*</url-pattern>

  </filter-mapping>

运行结果:

点击提交

运行成功,不会出现中文乱码 

全部评论

相关推荐

new和malloc的区别?c++里new了数组怎么释放?32位系统的指针变量占多大字节?int*呢?空类呢?c++多态?为什么需要虚析构?虚析构实现原理?c++可以不写构造函数吗?编译器默认除了构造还会有什么?深拷贝和浅拷贝区别?智能指针哪个c++版本引入的,作用?用过哪些容器?项目里用了哪些容器?为什么用qlist?qmap什么是key?模板的好处?什么时候会用?设计模式了解哪些?自己写过吗?单例模式怎么写?单例要返回非静态指针类型怎么弄?队列和栈的区别?osi七层以及相应协议?网络层的协议?应用层?一些项目问题...网络加密算法知道哪些?tcp的三次握手四次挥手回收写过吗?写服务器基本模...
AI牛可乐:哇,你分享了好多面试经验呢,听起来你准备得很充分呢!👍 关于你提到的技术问题,我来帮你解答一下吧: 1. `new` 和 `malloc` 的区别?`new` 会自动调用构造函数,而 `malloc` 只负责分配内存。`new` 数组释放时用 `delete[]`,比如 `delete[] myArray;`。 2. 在32位系统中,指针变量通常占4个字节,`int*` 也是如此。 3. 空类的实例通常占1个字节,但具体实现可能不同。 4. C++多态允许我们通过基类指针或引用来调用派生类的函数。需要虚析构是因为基类指针可能指向派生类对象,不使用虚析构可能导致派生类析构函数不被调用。 5. 如果不写构造函数,编译器会提供一个默认构造函数。默认构造函数会进行成员变量的默认初始化。 6. 深拷贝和浅拷贝的区别在于,深拷贝会复制对象的所有字段,包括指向的内存内容;浅拷贝只复制对象的字段值,不复制指向的内存内容。 7. 智能指针是在C++11版本引入的,用于自动管理内存,防止内存泄漏。 8. 常用的容器有 `vector`、`list`、`map` 等。具体使用哪个容器取决于项目需求。 关于你问的其他问题,我简单回答一下: - `QList` 通常用于需要频繁插入和删除的场景。 - `QMap` 的 `key` 是映射表中的键值。 - 模板的好处是提高代码复用性,减少代码冗余。 - 设计模式有很多种,比如单例模式、工厂模式等。单例模式可以通过静态成员函数实现。 现在,悄悄告诉你一个小秘密,点击我的头像,我们可以私信聊天哦~🎉 那么,你平时在项目中都用过哪些容器呢?有没有什么特别的经验或者技巧可以分享的呢?😊
点赞 评论 收藏
分享
剑桥断刀:找啥工作,牛客找个比如大厂软开或者随便啥的高薪牛马,大把没碰过妹子的技术仔,狠狠拿捏爆金币
点赞 评论 收藏
分享
02-23 00:10
湖南大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务