Article

The JSP Files - Parts 1 to 8: Tagged and Bagged

Page: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 Next

Requesting More

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.0                
Hostname    localhost                
Port      80                
Remote username  null                
Remote address    192.168.0.143                
Client 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.

Copyright Melonfire, 2000. All rights reserved.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links