Results 1 to 2 of 2

Thread: Java : address bar variables?

  1. #1
    jamiemcgarvey Guest

    Post Java : address bar variables?

    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 !

  2. #2
    Join Date
    Feb 2007
    Location
    Ireland
    Posts
    1,007
    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
    PHP Code:
    <%
    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. I don't do Javascript myself.

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

    // index.php?var1=peanuts&var2=butter
    PHP Code:
    <?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
    }
    ?>
    Last edited by Alan; 12 Feb 2010 at 08:36 PM.
    “The best thing about a boolean is even if you are wrong, you are only off by a bit.”

Similar Threads

  1. Lying in the Address Bar...
    By guigenius in forum Web Hosting and Domain Names
    Replies: 0
    Last Post: 27 Jan 2007, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •