zoukankan      html  css  js  c++  java
  • Java web 实验五 Servlet过滤器

    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里的CheckParamServlet .java

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

    filter里面的EncodingFilter

    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

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>meng</display-name>
    <servlet>
    <servlet-name>CheckParamServlet</servlet-name>
    <display-name>This is the display name of my J2EE component</display-name>
    <description>This is the description of my J2EE component</description>
    <servlet-class>servlet.CheckParamServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>CheckParamServlet</servlet-name>
    <url-pattern>/servlet/CheckParamServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
    <servlet-name>CheckParamServlet</servlet-name>
    <servlet-class>servlet.CheckParamServlet</servlet-class>
    </servlet>
    <servlet>
    <servlet-name>EncodingFilter</servlet-name>
    <display-name>This is the display name of my J2EE component</display-name>
    <description>This is the description of my J2EE component</description>
    <servlet-class>filter.EncodingFilter</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>CheckParamServlet</servlet-name>
    <url-pattern>/servlet/check</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>EncodingFilter</servlet-name>
    <url-pattern>/servlet/EncodingFilter</url-pattern>
    </servlet-mapping>

    <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>
    </web-app>

  • 相关阅读:
    演示一个简单的Redis队列
    Quartz.net 基于配置的调度程序实践
    阿里云OSS分片上传DEMO
    java基础 -- 关键字static的用法
    java基础 -- 关键字final的用法
    Linux中安装软件和各种常用命令
    python获取网页信息的三种方法
    jquery中获取ajax请求返回数据的方法
    Jquery为动态添加的元素添加事件
    js中时间戳转换成xxxx-xx-xx xx:xx:xx类型日期格式的做法
  • 原文地址:https://www.cnblogs.com/meng2/p/7775172.html
Copyright © 2011-2022 走看看