Skip to content

Instantly share code, notes, and snippets.

@levancho
Created May 30, 2023 13:18
Show Gist options
  • Save levancho/388bda22da1bb7200e77e9ad1dca76d9 to your computer and use it in GitHub Desktop.
Save levancho/388bda22da1bb7200e77e9ad1dca76d9 to your computer and use it in GitHub Desktop.
get url
public static String getURL(HttpServletRequest curRequest) {
if(curRequest ==null) {
return "https://treespond.com/";
}
String scheme = curRequest.getScheme(); // http
String serverName = curRequest .getServerName(); // hostname.com
int serverPort = curRequest .getServerPort(); // 80
String contextPath = curRequest .getContextPath(); // /mywebapp
String servletPath = curRequest.getServletPath(); // /servlet/MyServlet
String pathInfo = curRequest .getPathInfo(); // /a/b;c=123
StringBuffer sb = new StringBuffer() ;
// if(serverName !=null && serverName.equalsIgnoreCase("spingun.com")){
// scheme="https";
// }
sb.append(scheme) ;
sb.append("://") ;
sb.append(serverName) ;
if(serverPort!=80 && serverPort!=443) {
sb.append(":") ;
sb.append(serverPort) ;
}
sb.append(contextPath) ;
return sb.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment