1. 程式人生 > >request對象的增強 裝飾設計模式學習筆記二

request對象的增強 裝飾設計模式學習筆記二

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對象的增強 裝飾設計模式學習筆記二