Example usage for javax.servlet.http HttpServletRequest getServerName

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

Introduction

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

Prototype

public String getServerName();

Source Link

Document

Returns the host name of the server to which the request was sent.

Usage

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

public static JSONObject fetchFrom(HttpServletRequest request, String url) {
    JSONObject object = null;// ww  w . j  av a2  s. c o m
    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: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.opendesign.utils.ControllerUtil.java

public static String getHostName(HttpServletRequest request) {

    String scheme = request.getScheme();
    String serverName = request.getServerName();
    int portNumber = request.getServerPort();
    String contextPath = request.getContextPath();

    String host = scheme + "://" + serverName + contextPath + ":" + portNumber;
    if (portNumber == 80) {
        host = scheme + "://" + serverName + contextPath;
    }//w  w  w  . ja  v  a  2  s  . c  o m

    return host;
}

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  .  j a  v  a2 s.com
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:eu.europeana.core.util.web.ControllerUtil.java

public static String getServletUrl(HttpServletRequest request) {
    String url = request.getRequestURL().toString();
    int index = url.indexOf(request.getServerName());
    url = url.substring(0, index) + request.getServerName() + ":" + request.getServerPort()
            + request.getContextPath();/*from   w w  w  . j  a  va 2 s  .  com*/
    return url;
}

From source file:eu.europeana.core.util.web.ControllerUtil.java

public static String getFullServletUrl(HttpServletRequest request) {
    String url = request.getRequestURL().toString();
    int index = url.indexOf(request.getServerName());
    url = url.substring(0, index) + request.getServerName() + ":" + request.getServerPort()
            + request.getRequestURI();//  w w w . jav a 2 s. co  m
    return url;
}

From source file:org.magnum.dataup.VideoServiceController.java

private static String getUrlBaseForLocalServer() {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();/*from  ww  w .j  a  v  a2s .  c  om*/
    String base = "http://" + request.getServerName()
            + ((request.getServerPort() != 80) ? ":" + request.getServerPort() : "");
    return base;
}

From source file:dk.dma.msinm.common.util.WebUtils.java

/**
 * Returns the base URL of the request/*from   w  w  w.  j  a  va  2s . c o m*/
 * @param request the request
 * @return the base URL
 */
public static String getWebAppUrl(HttpServletRequest request, String... appends) {
    String result = String.format("%s://%s%s%s", request.getScheme(), request.getServerName(),
            request.getServerPort() == 80 || request.getServerPort() == 443 ? ""
                    : ":" + request.getServerPort(),
            request.getContextPath());
    for (String a : appends) {
        result = result + a;
    }
    return result;
}

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./*from w  ww.  ja  va2s .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:org.sventon.util.WebUtils.java

/**
 * Extracts the full request URL, including scheme, server name,
 * server port (if not 80) and context path.
 *
 * @param request The request./*from  w  w w . j  a  v a  2  s.  co  m*/
 * @return The full URL, ending with a forward slash (/).
 */
public static String extractBaseURLFromRequest(final HttpServletRequest request) {
    final StringBuilder sb = new StringBuilder();
    sb.append(request.getScheme());
    sb.append("://");
    sb.append(request.getServerName());
    if (request.getServerPort() != 80) {
        sb.append(":");
        sb.append(request.getServerPort());
    }
    sb.append(request.getContextPath());
    sb.append("/");
    return sb.toString();
}