Solved

How to get server variables in JSP

Posted on 2004-04-29
6
7,136 Views
Last Modified: 2010-05-18
Hi,  does anyone know how to get server variables such as

remoteHost
Locale
UserAgent
Referer

using JAVA in-line in JSP?
Thanks in advance..
0
Comment
Question by:rosshind
6 Comments
 
LVL 7

Accepted Solution

by:
mark-b earned 250 total points
ID: 10948785
There is an implicit request object available in JSPs:

request.getHeader( "User-Agent" );
request.getRemoteHost();
request.getHeader( "Referer" );
request.getLocale();

-Mark
0
 
LVL 4

Assisted Solution

by:indigostark
indigostark earned 50 total points
ID: 10948947
Where servletRequest = javax.servlet.http.HttpServletRequest

//Get specific details about the User
String remoteHost = servletRequest.getRemoteHost();
String remoteAddress = servletRequest.getRemoteAddr();
java.util.Locale locale = servletRequest.getLocale();
String country = locale.getDisplayCountry();
String userAgent = servletRequest.getHeader("User-Agent");
String refererr = servletRequest.getHeader("Referer");
String encodings = servletRequest.getHeader("Accept-Encoding");

//check for null values
if(remoteHost==null || remoteHost.equals(""))
remoteHost="Unknown";
            
if(remoteAddress==null || remoteAddress.equals(""))
remoteAddress="Unknown";
            
if(country==null || country.equals(""))
country="Unknown";
            
if(userAgent==null)
userAgent="";
            
if(refererr==null)
refererr="";
0
 

Author Comment

by:rosshind
ID: 10949341
When I try to use teh imlicit variables I get this  error message:

/home/hindrm/jakarta-tomcat-4.1.29/work/Standalone/localhost/_/rmh/ProductDetails_jsp.java:493: cannot resolve symbol
symbol  : variable request
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Assisted Solution

by:searlas
searlas earned 200 total points
ID: 10949399
Have you put this code in a method as so:
<%!
void someMethod() {
  String referer = request.getHeader( "Referer" );
}
%>

If so, you need to change it to:
<%!
void someMethod(HttpServletRequest request) {
  String referer = request.getHeader( "Referer" );
}
%>

and call it with <% someMethod(request); %>


0
 

Author Comment

by:rosshind
ID: 10949423
I think this may be a problem with using thoes implicit variables within a funtion delimited by the <%! and %> tags.

I have the same problem with the "out" implicit variables.

Do you know why this might be?
0
 

Author Comment

by:rosshind
ID: 10949430
Ah I see, you have answered my question there.

Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Export JSON response data to Excel in IE9 3 81
java cyclic graphs 15 212
JavaScript parse form with colon separated values 2 78
jsp error 6 26
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This article explains the steps required to use the default Photos screensaver to display branding/corporate images
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question