Solved

How to get server variables in JSP

Posted on 2004-04-29
6
7,108 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Read about the importance of offline branding in the digital age.
February 24, 2017 — On February 23, Travis Ormandy, a vulnerability researcher at Google, reported on Twitter (https://twitter.com/taviso/status/834900838837411840) that massive stores of data have been leaked by CloudFlare, a company that provide…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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