• java 获取HttpRequest Header 的几种方法


    Java代码获取:

    import java.util.Enumeration;
    import java.util.HashMap;
    import java.util.Map;
    import javax.servlet.http.HttpServletRequest;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.servlet.ModelAndView;
    @Controller
    @RequestMapping("/site")
    public class SiteController {
        @Autowired
        private HttpServletRequest request;
        @RequestMapping(value = "/{input:.+}", method = RequestMethod.GET)
        public ModelAndView getDomain(@PathVariable("input") String input) {
            ModelAndView modelandView = new ModelAndView("result");
            modelandView.addObject("user-agent", getUserAgent());
            modelandView.addObject("headers", getHeadersInfo());
            return modelandView;
        }
        //get user agent
        private String getUserAgent() {
            return request.getHeader("user-agent");
        }
        //get request headers
        private Map<String, String> getHeadersInfo() {
            Map<String, String> map = new HashMap<String, String>();
            Enumeration headerNames = request.getHeaderNames();
            while (headerNames.hasMoreElements()) {
                String key = (String) headerNames.nextElement();
                String value = request.getHeader(key);
                map.put(key, value);
            }
            return map;
        }
    }
    

    JSP代码获取:

    <%!
        private HttpServletRequest request;
        private HttpServletResponse response;
    %>
    <%
        System.out.println("Protocol: " + request.getProtocol() + "<br>");
        System.out.println("Scheme: " + request.getScheme() + "<br>");
        System.out.println("Server Name: " + request.getServerName() + "<br>");
        System.out.println("Server Port: " + request.getServerPort() + "<br>");
        System.out.println("Protocol: " + request.getProtocol() + "<br>");
        System.out.println("Remote Addr: " + request.getRemoteAddr() + "<br>");
        System.out.println("Remote Host: " + request.getRemoteHost() + "<br>");
        System.out.println("Character Encoding: " + request.getCharacterEncoding() + "<br>");
        System.out.println("Content Length: " + request.getContentLength() + "<br>");
        System.out.println("Content Type: " + request.getContentType() + "<br>");
        System.out.println("Auth Type: " + request.getAuthType() + "<br>");
        System.out.println("HTTP Method: " + request.getMethod() + "<br>");
        System.out.println("Path Info: " + request.getPathInfo() + "<br>");
        System.out.println("Path Trans: " + request.getPathTranslated() + "<br>");
        System.out.println("Query String: " + request.getQueryString() + "<br>");
        System.out.println("Remote User: " + request.getRemoteUser() + "<br>");
        System.out.println("Session Id: " + request.getRequestedSessionId() + "<br>");
        System.out.println("Request URL: " + request.getRequestURL() + "<br>");
        System.out.println("Request URI: " + request.getRequestURI() + "<br>");
        System.out.println("Servlet Path: " + request.getServletPath() + "<br>");
        System.out.println("Created : " + session.getCreationTime() + "<br>");
        System.out.println("LastAccessed : " + session.getLastAccessedTime() + "<br>");
        System.out.println("Accept: " + request.getHeader("Accept") + "<br>");
        System.out.println("Host: " + request.getHeader("Host") + "<br>");
        System.out.println("Referer : " + request.getHeader("Referer") + "<br>");
        System.out.println("Accept-Language : " + request.getHeader("Accept-Language") + "<br>");
        System.out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding") + "<br>");
        System.out.println("User-Agent : " + request.getHeader("User-Agent") + "<br>");
        System.out.println("Connection : " + request.getHeader("Connection") + "<br>");
        System.out.println("Cookie : " + request.getHeader("Cookie") + "<br>");
    %>
    

     

  • 相关阅读:
    PHP命令行脚本接收传入参数的三种方式
    php中include()和require()的区别
    自定义一个可复用的BaseAdapter
    Java RandomAccessFile用法
    java中两字符串比较--compareTo方法
    机房内网电脑时间自动同步器
    Java中对JSONArray中的对象的某个字段进行排序
    jquery一个元素绑定了多个 click 事件,如何取消其中的一个
    js和Java分别正则匹配小数点后多余的0
    javascript 去掉小数末尾多余的零
  • 原文地址:https://www.cnblogs.com/zhangzhixi/p/14986728.html
Copyright © 2020-2023  润新知