集册 Java实例教程 从HttpServletRequest获取应用程序主机名

从HttpServletRequest获取应用程序主机名

欢马劈雪     最近更新时间:2020-01-02 10:19:05

743
从HttpServletRequest获取应用程序主机名

/*

 * AMERICA ONLINE CONFIDENTIAL INFORMATION

 *

 *

 * Copyright (c) 2014 AOL LLC

 *

 * All Rights Reserved.  Unauthorized reproduction, transmission, or

 * distribution of this software is a violation of applicable laws.

 *

 *

 */

import java.text.ParseException;/**来自 时 代 J a v a**/

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;


public class Main{

    private final static Logger LOG = Logger.getLogger(CommonUtils.class);

    public static String getAppHostName(HttpServletRequest request) {

        String hostName = null;

        try {

            String requestURL = request.getRequestURL().toString();

            String host = requestURL.substring(0,

                    requestURL.indexOf(request.getContextPath()));

            host = host

                    + (request.getContextPath().startsWith("/") ? request

                            .getContextPath() : "/"

                            + request.getContextPath()) + "/";

            hostName = host.endsWith("/") ? host : (host + "/");

            LOG.error("returning url : " + hostName);
            /** fro
展开阅读全文