zoukankan      html  css  js  c++  java
  • java获取请求的url地址

    1.获取全路径
    request.getRequestURL(); //得到http://localhost:8888/CRM/loginController/login

    2.获取协议名和域名
    request.getScheme(); //得到协议名 例如:http
    request.getServerName(); //得到域名 localhost

    3.获取请求所有参数 //map类型
    request.getParameterMap()

    4.获取项目名
    request.getContextPath(); // /CRM

    5.获取请求方法
    request.getServletPath(); // /loginController/login

    /**
         * 获取当前访问URL (含协议、域名、端口号[忽略80端口]、项目名)
         * @param request
         * @return: String
         */
        public static String getServerUrl(HttpServletRequest request) {
            // 访问协议
            String agreement = request.getScheme();
            // 访问域名
            String serverName = request.getServerName();
            // 访问端口号
            int port = request.getServerPort();
            // 访问项目名
            String contextPath = request.getContextPath();
            String url = "%s://%s%s%s";
            String portStr = "";
            if (port != 80) {
                portStr += ":" + port;
            }
            return String.format(url, agreement, serverName, portStr, contextPath);
    
        }
  • 相关阅读:
    c++ isdigit函数
    c++ swap函数
    1.2Hello, World!的大小
    1.2整型与布尔型的转换
    1.2打印ASCII码
    leetcode[170]Two Sum III
    leetcode[167]Two Sum II
    leetcode[1]Two Sum
    leetcode[2]Add Two Numbers
    leetcode[3]Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/pxblog/p/10523013.html
Copyright © 2011-2022 走看看