request对象的增强 装饰设计模式学习笔记二

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了request对象的增强 装饰设计模式学习笔记二相关的知识,希望对你有一定的参考价值。

  • Servlet API 中提供了一个request对象的Decorator设计模式的默认实现类HttpServletRequestWrapper , (HttpServletRequestWrapper 类实现了request 接口中的所有方法,但这些方法的内部实现都是仅仅调用了一下所包装的的 request 对象的对应方法)以避免用户在对request对象进行增强时需要实现request接口中的所有方法

  • 使用Decorator模式包装request对象,完全解决get、post请求方式下的乱码问题
  • MyRequest

    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    
    /**对HttpServletRequest对象包装/装饰*/
    public class MyRequest extends HttpServletRequestWrapper{
        private HttpServletRequest request;
        public MyRequest(HttpServletRequest request) {
            super(request);
            this.request = request;
        }
        //重写父类的方法
        public String getParameter(String name) {//表单项的名字
            String value = null;
            //取得客户端的请求方式[GET/POST]
            String method = request.getMethod();
            if("GET".equals(method)){
                try {
                    value = request.getParameter(name);//乱码
                    byte[] buf = value.getBytes("ISO8859-1");
                    value = new String(buf,"UTF-8");//正码
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else if("POST".equals(method)){
                try {
                    request.setCharacterEncoding("UTF-8");
                    value = request.getParameter(name);//正码
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            value = filter(value);
            return value;
        }
    
            //转义方法
        public String filter(String message) {
            if (message == null)
                return (null);
            char content[] = new char[message.length()];
            message.getChars(0, message.length(), content, 0);
            StringBuffer result = new StringBuffer(content.length + 50);
            for (int i = 0; i < content.length; i++) {
                switch (content[i]) {
                case ‘<‘:
                    result.append("&lt;");
                    break;
                case ‘>‘:
                    result.append("&gt;");
                    break;
                case ‘&‘:
                    result.append("&amp;");
                    break;
                case ‘"‘:
                    result.append("&quot;");
                    break;
                default:
                    result.append(content[i]);
                }
            }
            return (result.toString());
        }

    GetPostServlet

    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 GetPostServlet extends HttpServlet {
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            this.doGet(request,response);
    
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String username = request.getParameter("username");
            response.getWriter().write(username);
    
        }
    
    }

    longin

    <%@ 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>
    
        <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 action="${pageContext.request.contextPath}/GetPostServlet" method="post">
           <table border="1" align="center">
           <caption>用户登陆</caption>
              <tr>
                <th>用户名</th>
                <td><input type="text" name="username"/>
                </td>        
                  <td colspan="5" align="center">
                   <input type="submit" value="提交"/>
                  </td>
               </tr>        
           </table>
        </form>
      </body>
    </html>
    

    以上是关于request对象的增强 装饰设计模式学习笔记二的主要内容,如果未能解决你的问题,请参考以下文章

    javaweb学习总结(四十三)——Filter高级开发

    九:Decorator设计模式

    [javaweb]Java过滤器与包装设计模式的实用案例.

    AOP 学习笔记

    Python学习笔记(yield与装饰器)

    设计模式学习笔记之代理模式