Solved

How to get server variables in JSP

Posted on 2004-04-29
6
7,027 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article, I will show you HOW TO: Suppress Configuration Issues and Warnings Alert displayed in Summary status for ESXi 6.5 after enabling SSH or ESXi Shell.
Use of TCL script on Cisco devices:  - create file and merge it with running configuration to apply configuration changes
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now