The Request object also comes with a bunch of other useful methods - the following example demonstrates some of them
<html>
<head>
<basefont face="Arial">
</head>
<body>
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<td><b>Variable</b></td>
<td><b>Value</b></td>
</tr>
<tr>
<td>Request protocol</td>
<td>
<%
// protocol
out.println(request.getProtocol());
%>
</td>
</tr>
<tr>
<td>Hostname</td>
<td>
<%
// server name
out.println(request.getServerName());
%>
</td>
</tr>
<tr>
<td>Port</td>
<td>
<%
// server port
out.println(request.getServerPort());
%>
</td>
</tr>
<tr>
<td>Remote username</td>
<td>
<%
// username if using HTTP authentication
// null if no authentication
out.println(request.getRemoteUser());
%>
</td>
</tr>
<tr>
<td>Remote address</td>
<td>
<%
// get IP address of client
out.println(request.getRemoteAddr());
%>
</td>
</tr>
<tr>
<td>Client browser</td>
<td>
<%
// client browser identification
out.println(request.getHeader("User-Agent"));
%>
</td>
</tr>
</table>
</body>
</html>And when you view the file in your browser, you'll probably see something like this:
Variable Value
Request protocol HTTP/1.0Hostname localhostPort 80Remote username nullRemote address 192.168.0.143Client browser Mozilla/4.0 (compatible; MSIE 5.5; Windows 95) All these variables come in handy if you need to make decisions on the basis of remote variables - as the following example demonstrates:
<%
String browser = request.getHeader("User-Agent");if(browser.indexOf("MSIE") >= 0){ // IE-specific code}else if(browser.indexOf("Mozilla") >= 0){ // Mozilla-specific code}else{ // any other browser}%>Note our usage of the indexOf() method - you may remember this from previous articles in this series.
Please enable JavaScript to view the comments powered by Disqus. blog comments powered by