dev.kanngard.net make sure you visit my new blog at: johankanngard.net

Short tip: Getting the protocol name from a request in a servlet

by Johan Känngård / [Java] / 2002-10-23 / #61


This is a very simple and little function that gets the name of the protocol used when accessing the servlet. Great if you want to construct a URL...

/**
 * Returns the protocol name (http/https etc)
 *that the request was made with.
 *
 * @author Johan Känngård, http://dev.kanngard.net
 * @param request the request that should be examined.
 * @return the protocol string in lowercase
 * @see HttpServletRequest#getProtocol()
 */
private String getProtocolName(HttpServletRequest request) {
	String s = request.getProtocol().toLowerCase();
	return s.substring(0, s.indexOf("/")).toLowerCase();
}