Example usage for javax.servlet.http HttpServletRequest getServerPort

List of usage examples for javax.servlet.http HttpServletRequest getServerPort

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getServerPort.

Prototype

public int getServerPort();

Source Link

Document

Returns the port number to which the request was sent.

Usage

From source file:ServletUtils.java

/**
 * NOT UNIT TESTED Returns the base url (e.g, <tt>http://myhost:8080/myapp</tt>) suitable for
 * using in a base tag or building reliable urls.
 *///from ww w  . ja  v  a2  s . c  om
public static String getBaseUrl(HttpServletRequest request) {
    if ((request.getServerPort() == 80) || (request.getServerPort() == 443))
        return request.getScheme() + "://" + request.getServerName() + request.getContextPath();
    else
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + request.getContextPath();
}

From source file:no.kantega.commons.util.URLHelper.java

/**
 * @param request - current request/*from   w ww  . j av  a  2 s  .c om*/
 * @return scheme, servername and port. If port is 80 or 443 it is omitted.
 */
public static String getServerURL(HttpServletRequest request) {
    int port = request.getServerPort();
    String portStr = isNormalPort(port) ? "" : ":" + port;

    return request.getScheme() + "://" + request.getServerName() + portStr;
}

From source file:ServletUtils.java

/**
 * NOT UNIT TESTED Returns the URL (including query parameters) minus the scheme, host, and
 * context path.  This method probably be moved to a more general purpose
 * class.// w w w.  j  a v a  2 s  . co  m
 */
public static String getRelativeUrl(HttpServletRequest request) {

    String baseUrl = null;

    if ((request.getServerPort() == 80) || (request.getServerPort() == 443))
        baseUrl = request.getScheme() + "://" + request.getServerName() + request.getContextPath();
    else
        baseUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
                + request.getContextPath();

    StringBuffer buf = request.getRequestURL();

    if (request.getQueryString() != null) {
        buf.append("?");
        buf.append(request.getQueryString());
    }

    return buf.substring(baseUrl.length());
}

From source file:nl.b3p.gis.utils.CrossWebAppUtil.java

private static String getServerURL(HttpServletRequest request) {
    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
}

From source file:com.eryansky.common.web.utils.RequestUtil.java

public static String getAppURL(HttpServletRequest request) {
    StringBuffer url = new StringBuffer();
    int port = request.getServerPort();
    if (port < 0) {
        port = 80;//from   ww  w  .jav  a 2 s. com
    }
    String scheme = request.getScheme();
    url.append(scheme);
    url.append("://");
    url.append(request.getServerName());
    if (((scheme.equals("http")) && (port != 80)) || ((scheme.equals("https")) && (port != 443))) {
        url.append(':');
        url.append(port);
    }
    url.append(request.getContextPath());
    return url.toString();
}

From source file:com.manydesigns.elements.servlet.ServletUtils.java

public static String getApplicationBaseUrl(HttpServletRequest req) {
    String scheme = req.getScheme();
    int port = req.getServerPort();
    String portString;/*ww  w  . j a v  a 2s  .  com*/
    if ((scheme.equals("http") && port == 80) || (scheme.equals("https") && port == 443)) {
        portString = "";
    } else {
        portString = ":" + port;
    }
    return scheme + "://" + req.getServerName() + portString + req.getContextPath();
}

From source file:com.flowlogix.util.PathUtil.java

/**
 * @param request/*w  w w.ja  v a2s  . c  o m*/
 * @return complete server path i.e. https://www.glowlogix.com:8080
 */
public static String getServerPath(HttpServletRequest request) {
    String port = "";
    switch (request.getScheme()) {
    case "http":
        if (request.getServerPort() != 80) {
            port = ":" + Integer.toString(request.getServerPort());
        }
        break;
    case "https":
        if (request.getServerPort() != 443) {
            port = ":" + Integer.toString(request.getServerPort());
        }
        break;
    }

    return String.format("%s://%s%s", request.getScheme(), request.getServerName(), port);
}

From source file:com.vmware.demo.ListController.java

public static String getURLWithContextPath(HttpServletRequest request) {
    return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath() + "/sso";
}

From source file:com.jjtree.utilities.JServeletManager.java

public static JSONObject fetchFrom(HttpServletRequest request, String url) {
    JSONObject object = null;/*from   www .j a  v a2s .  com*/
    try {
        String serverName = request.getServerName();
        int portNumber = request.getServerPort();
        String contextPath = request.getContextPath();

        String accountUrl = "http://" + serverName + ":" + portNumber + contextPath + url;

        URL urldemo = new URL(accountUrl);
        URLConnection urlCon = urldemo.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            object = new JSONObject(inputLine);
        }
        in.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    return object;
}

From source file:info.magnolia.cms.util.RequestDispatchUtil.java

private static boolean isUsingStandardPort(HttpServletRequest request) {
    String requestScheme = request.getScheme();
    int serverPort = request.getServerPort();
    return (serverPort == 80 && "http".equals(requestScheme))
            || (serverPort == 443 && "https".equals(requestScheme));
}