zoukankan      html  css  js  c++  java
  • 机房servlet过滤器

    1.源代码

    loginform.html

    <html>
    <head>
    <title>使用过滤器改变请求编码</title>
    <meta http-equiv="Content-Type" content="text/html;charset=GB2312">
    </head>
    <body>
    <center>
    <h2>请输入用户名和口令:</h2>
    <form method="post" action="servlet/CheckParamServlet">
    <table>
    <tr>
    <td>用户名:</td>
    <td><input name="name" type="text"></td>
    </tr>
    <tr>
    <td>口 令:</td>
    <td><input name="pass" type="password"></td>
    </tr>
    <tr>
    <td></td>
    <td>
    <input name="ok" type="submit" value="提交">
    <input name="cancel" type="reset" value="重置">
    </td>
    </tr>
    </table>
    </form>
    </center>
    </body>
    </html>

    编写处理请求参数的Servlet

     

    packet servlet;

     

    import java.io.*;

     

    import javax.servlet.*;

     

    import javax.servlet.http.*;

     

     

     

    public class CheckParamServlet extends HttpServlet{

     

       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>");          

     

       }

     

       

     

       public void doPost(HttpServletRequest request,

     

                          HttpServletResponse response)

     

            throws ServletException, IOException {

     

           

     

           doGet(request,response);

     

       } 

     

    }

    修改web.xml文件,加入下面代码:

     

    <servlet>
    <servlet-name>CheckParamServlet</servlet-name>
    <servlet-class>servlet.CheckParamServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>CheckParamServlet</servlet-name>
    <url-pattern>/servlet/CheckParamServlet</url-pattern>
    </servlet-mapping>

    代码运行截图

    创建一个filter包

    package filter;

    import java.io.IOException;

    import javax.servlet.*;

    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() {

    }

    }

     在web.xml文件中配置过滤器

     

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

     

    <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

    代码运行截图:

     

  • 相关阅读:
    负载均衡(负载平衡)
    JavaScript中绑定事件监听函数的通用方法[ addEvent() ]
    有趣的浏览器检测
    IE6 bug之 href= “javascript:void(0);”
    SVN使用技巧 不要把不必要的文件版本化 *.suo,*.bin,*.obj
    CacheDependency缓存依赖里面的 absoluteExpiration(绝对到期时间),弹性到期时间(slidingExpiration)
    TimeSpan 和 DateTime
    字符串数组 string[] 转换为 字符串(用逗号,作为分隔符),linq Except的用法,linq获取两个字符串数组相同的部分
    List的ToLookup 分组方法
    mysql 返回查询结果,返回out返回值,多表联合查询的分页存储过程
  • 原文地址:https://www.cnblogs.com/frankzone/p/7771445.html
Copyright © 2011-2022 走看看