码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-19 19:14:46      阅读:132      评论:0      收藏:0      [点我收藏+]

标签: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对象的增强 装饰设计模式学习笔记二

    标签:request对象的增强 装饰设计模式学

    原文地址:http://blog.51cto.com/357712148/2105506

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!