JSP find server host name - getServerName()

The HttpServletRequest has method getServerName() to retrieve the server host name. Here is a sample JSP code...

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Get Server name(hostname) in JSP</title>
<link rel="stylesheet" href="/resources/themes/master.css" type="text/css" />
<div id="allContent">
<%@include file="/header.jsp"%>

<div id="myContent">
        JSP - Get Server HOST name 
<div id="myExample">
    Server host name is: <b><%=request.getServerName() %></b>

<%@include file="/footer.jsp"%>                                

Tip: You can use the same method inside a Java Servlet 

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.