Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

javascript to java

Options
  • 28-02-2008 5:37pm
    #1
    Registered Users Posts: 31


    Hi,

    Having a problem with trying to pass a variable from one html file to another in Java servlets. I know it can be done using hidden fields on forms but I'm just linking using normal <a href> so I can't use forms because they need a submit button to be clicked.

    The only other way I can find to do it is by query strings in the url bar and the only way I can get the variable back on the other side is using javascript. So now I have the variable in javascript, but I've no idea how to make this variable available to the rest of the class for use in Java methods.

    Does anyone know how to do this or any other way of passing a parameter that would be available to the rest of the Java class?

    Thanks.


Comments

  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    Here are a few ways to transfar data from one page to another:

    1. Using the querystring - you seem to have chosen this way though pulling it into javascript with the location object (I assume that's how you did it) is unnecessary unless you need to use it further in your javascript. There's a function in Java (public java.lang.String getQueryString()) that would let you get at it but you need to url decode it apparantly. You should also bear in mind that since the querystring is in the browser address bar it is the most open to abuse so if you are transferring something sensitive it's not the way to go. This basically equates to transferring the data using a form with the method set to GET.

    2. You could use a form with the POST method using a hidden field that you dynamically populate with the required value and use javascript in the onlick event of the link to populate the hidden field with the data and submit the form (document.forms[0].submit(); for instance - you don't need a submit button as long as the user has javascript enabled) and pick up the request variable on the other page. This is a tad more secure than the GET method as your data is less accessible but still available for easy abuse.

    3. You could set a cookie client side using javascript on the origin page again in the onclick event on the link and pick it up on the desitination page server side.

    4. You could use a session variable to hold the data between pages or if you want it to be more secure, some reference to it that could be verified on the destination page. (yes, I know sessions implemented using cookies).

    Take a look here - http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html


  • Registered Users Posts: 31 cinnamono


    You're a legend. The getQueryString() method was what I was looking for. Can't believe it was that simple. Thanks a million.


  • Registered Users Posts: 1,045 ✭✭✭Bluefrog


    I does my best!


Advertisement