PDA

View Full Version : Java : address bar variables?



jamiemcgarvey
12 Feb 2010, 09:25 AM
Hi, I'm relatively new to web programming and i'm completely new to these forums (hi).

I'm looking to be able to do something specific for an HTML page. What I want to be able to do is this:

- have 2 variables in the address bar on the browser
- be able to display the value of these variables throughout an HTML page

eg.

in address bar something like this www.website.com/page.htmlvar1="peanuts";var2=Butter"

On the webpage something like this

My favorite foods are: Peanuts and Butter

(the 'Peanuts' and 'butter' terms are called from the address bar).

If I change var1 and var2 it should be reflected in the text. If anyone could help me out or point me in the right direction, that would be amazing!


Thanks !

Alan
12 Feb 2010, 06:14 PM
You mention "Java" in the title of your post. Within the content of the post you don't mention any server side technologies or client side for that matter (other than HTML I mean). So unless I make some assumptions here is what you need.

// index.jsp?var1=peanuts&var2=butter


<%
String var1 = request.getParameter("var1");
String var2 = request.getParameter("var2");

if(var1 != null && !var1.equals(""))
{
out.println("Var1 has the value: " + var1); // Prints: peanuts
}

if(var2 != null && !var2.equals(""))
{
out.println("Var2 has the value: " + var2); // Prints: butter
}
%>


I'm going to make the assumption that you meant Javascript rather than Java, as they are two completely different languages. A Javascript example can be found here (http://mattwhite.me/11tmr.nsf/D6Plinks/MWHE-695L9Z). I don't do Javascript myself.

Here's a PHP example too in case your interested.

// index.php?var1=peanuts&var2=butter


<?php
$var1 = $_GET['var1'];
$var2 = $_GET['var2'];

if($var1 != null && $var1 != "")
{
echo 'Var1 has the value: ' . $var1; // Prints: peanuts
}

if($var2 != null && $var2 != "")
{
echo 'Var2 has the value: ' . $var2; // Prints: butter
}
?>