Solved

Correct syntax to insert Java variable into HTML?

Posted on 2010-09-02
6
512 Views
Last Modified: 2013-11-24
if I define a Java variable "num" in a function in the header of an HTML page, how can I pass the value of that variable in a form on the same page?

example:

<form method="POST" action="mailscript.html">
    <input type="hidden" name="java_var" value="num">
    <input class="button" type="submit" value="Submit">
</form>

what is the correct syntax to insert the java variable into the HTML?
0
Comment
Question by:ob1_
6 Comments
 
LVL 3

Expert Comment

by:alanpetersen
ID: 33588892
I'm assuming that when you say you are creating an HTML page, you really mean a JSP page. In that case, you could print the value of "num" in the input element like this:

  <input type="hidden" name="java_var" value="<%= num %>" />

If you were using JSTL, you could also use Expression Language (EL) to get the variable (and make things more readable):

  <input type="hidden" name="java_var" value="${num}" />
0
 
LVL 6

Author Comment

by:ob1_
ID: 33588924
no it is an HTML page, what is JSP?
could i use this:
 
 

<input type="hidden" name="numberof" value="<script type="text/javascript">document.write(num);</script>"

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33588952
then this num is a javscript variable.
When you said 'Java Variable' it created the confusion.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Accepted Solution

by:
alanpetersen earned 250 total points
ID: 33589009
Oh, so you are using Javascript, not Java. In your question you said you were creating a Java variable. Java != Javascript

In any case, in a Javascript function, you can set the value of a field like this:
  document.getElementById('java_var').value = num

0
 
LVL 22

Assisted Solution

by:kadaba
kadaba earned 250 total points
ID: 33589197
<input type="hidden" name="java_var" value="num">
you should use an id attribute

<input type="hidden" id="java_var" name="java_var">

then the usage of
document.getElementById('java_var').value = num;
will be proper.


0
 
LVL 6

Author Comment

by:ob1_
ID: 33589292
wow i didn't even think there would be a different between java and javascript i guess that makes sense - thanks!
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 17 28
maps stopped work unsure why 7 33
by zero exception 10 39
jsp login check 12 28
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

805 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