servlet request请求对象常用方法总结

1. request请求对象常用方法:

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html; charset = utf-8");
        this.response = response;
        out = this.response.getWriter();
        println("<ol>");
        //1. 获取请求方式、处理乱码问题
        String method = request.getMethod();

        //servletRequest中的方法
        request.setCharacterEncoding("utf-8");
        //1. 获取请求体的编码方式
        String characterEncoding = request.getCharacterEncoding();
        println("getCharacterEncoding = " + characterEncoding);
        
        //2. get body length
        int contentLength = request.getContentLength();
        println("getContentLength = " + contentLength);
        
        //3. MIME type
        String mimeType = request.getContentType();
        println("getContentType = " + mimeType);
        
        //4. 接收请求的接口的 Internet Protocol (IP) 地址
        String ip = request.getLocalAddr();
        println("getLocalAddr = " + ip);
        
        //5. 基于 Accept-Language 头,返回客户端将用来接受内容的首选 Locale 客户端语言环境
        Locale locale = request.getLocale();
        println("getLocale = " + locale);
        
        //6. 所有的语言环境
        Enumeration<Locale> locales = request.getLocales();
        while(locales.hasMoreElements()){
            Locale temp = locales.nextElement();
            println("\n Locales = " + temp);
        }
        
        //7. 接收请求的 Internet Protocol (IP) 接口的主机名
        String localName = request.getLocalName();
        println("localName = " + localName);
        
        //8. 接收请求的接口的 Internet Protocol (IP) 端口号
        int localPort = request.getLocalPort();
        println("localPort = " + localPort);
        
        //9. 返回请求使用的协议的名称和版本
        String protocol = request.getProtocol();
        println("protocol = " + protocol);
        
        //10. 读取请求正文信息
        BufferedReader reader = request.getReader();
        println("getReader = " + reader.toString());
        
        //11. 发送请求的客户端
        String remoteAddr = request.getRemoteAddr();
        println("RemoteAddr = " + remoteAddr);
        
        //12. 发送请求的客户主机
        String remoteHost = request.getRemoteHost();
        println("RemoteHost = " + remoteHost);
        
        //13. 发送请求的客户主机端口
        int remotePort = request.getRemotePort();
        println("RemotePort = " + remotePort);
        
        //14. 返回用于发出此请求的方案名称,例如:http 、 https 、 ftp 
        String scheme = request.getScheme();
        println("Scheme = " + scheme);
        
        //15. 返回请求被发送到的服务器的主机名。它是Host头值":"(如果有)之前的那部分的值。 或者解析服务器名称或服务器的IP地址
        String serverName = request.getServerName();
        println("ServerName = " + serverName);
        
        //16. 返回请求被发送到的端口。他是"Host"头值":" (如果有)之后的那部分的值,或者接受客户端连接的服务器端口。
        int serverPort = request.getServerPort();
        println("ServerPort = " + serverPort);
        
        //17. 返回一个boolean值,指示此请求是否是使用安全通道(比如HTTPS) 发出的。
        boolean secure = request.isSecure();
        println("isSecure = " + secure);
        
        //以上方法为 ServletRequest 接口提供的
        
        
        //以下方法为 HttpServletRequest 接口提供的
        
        /*
         * 18. 返回用于保护servlet的验证方法名称。 所有的servlet容器都支持
         * basic、 form和client certificate验证, 并且可能还支持digest验证
         */
        String authType = request.getAuthType();
        println("authType = " + authType);
        
        //19. getDateHeader ??
        request.getDateHeader("");
        
        //20. 返回请求头包含的所有头名称的枚举。
        Enumeration<String> headerNames = request.getHeaderNames();
        println("<hr/>");
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            println(" headerNmea = " + name + ";   getHeader = " + request.getHeader(name));
        }
        
        println("<hr/>");
        //21. 以int的形式返回指定请求头的值。 ???
        request.getIntHeader("123");
        
        //22. 返回与客户端发出此请求时发送的URL相关联的额外路径信息。
        String pathInfo = request.getPathInfo();
        println("PathInfo = " + pathInfo);
        
        //23. 返回包含在请求RUL中路径后面的查询字符串。如果没有查询字符串返回null
        String remoteUser = request.getRemoteUser();
        println("RemoteUser = " + remoteUser);
        
        //24. 返回客户端制定的回话ID
        String requestedSessionId = request.getRequestedSessionId();
        println("requestSessionId = " + requestedSessionId);
        
        //25. 返回请求调用servlet的URL部分
        String servletPath = request.getServletPath();
        println("servletPath = " + servletPath);
        
        //26. 返回与此请求关联的当前HttpSession,如果没有当前会话并且参数为true,则返回一个新会话。
        HttpSession session = request.getSession(true);
        println("getSession(true) = " + session);
        
        //27. 返回包含当前已经过验证的用户的名称的java.security.Principal对象。如果用户没有经过验证,则该方法返回null
        Principal userPrincipal = request.getUserPrincipal();
        println("userPrincipal = " + userPrincipal);

        
        //28. 检查会话的id是否作为Cook进入的
        boolean sessionIdFromCookie = request.isRequestedSessionIdFromCookie();
        println("sessionIdFromCookie = " + sessionIdFromCookie);
        
        //29. 检查请求的会话ID是否作为请求的URL的一部分进入的
        boolean sessionIdFromURL = request.isRequestedSessionIdFromURL();
        println("sessionIdFormURL = " + sessionIdFromURL);
        
        //30. 
        
        println("</ol>");
        out.flush();
        out.close();

    }
    
    public void println(Object obj){
        this.response.setContentType("text/html;charset=utf-8");
        try {
            
            out.println("<li>");
            out.println(obj);
            out.println("</li>\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


2. 运行结果

    1. getCharacterEncoding = utf-8
    2. getContentLength = -1
    3. getContentType = null
    4. getLocalAddr = 127.0.0.1
    5. getLocale = zh_CN
    6. Locales = zh_CN
    7. Locales = zh
    8. Locales = en_US
    9. Locales = en
    10. localName = lm.licenses.adobe.com
    11. localPort = 8080
    12. protocol = HTTP/1.1
    13. getReader = org.apache.catalina.connector.CoyoteReader@17b8d3d
    14. RemoteAddr = 127.0.0.1
    15. RemoteHost = 127.0.0.1
    16. RemotePort = 57814
    17. Scheme = http
    18. ServerName = localhost
    19. ServerPort = 8080
    20. isSecure = false
    21. authType = null

    22. headerNmea = host;   getHeader = localhost:8080
    23. headerNmea = user-agent;   getHeader = Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0
    24. headerNmea = accept;   getHeader = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    25. headerNmea = accept-language;   getHeader = zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
    26. headerNmea = accept-encoding;   getHeader = gzip, deflate
    27. headerNmea = cookie;   getHeader = JSESSIONID=30256CEB48E2BF6050BF6E122635EAC4
    28. headerNmea = connection;   getHeader = keep-alive

    29. PathInfo = null
    30. RemoteUser = null
    31. requestSessionId = 30256CEB48E2BF6050BF6E122635EAC4
    32. servletPath = /req
    33. getSession(true) = org.apache.catalina.session.StandardSessionFacade@1fcf1ba
    34. userPrincipal = null
    35. sessionIdFromCookie = true
    36. sessionIdFormURL = false

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值