How to assign the value in a text field to a variable in jsp using Java
Hi all. I was wondering if there's a way to assign the value in a text field to a variable in jsp using Java (not Javascript). Normally in Javascript you might do something like: document.formName.fieldName.value to access the value. How can this be achieved using Java? :icon_confused:
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
If you want to use java it must be done at the server. Meaning before you render the page.
For example.
Put a value at the request request.setAttribute("key","The Value")
Then submit, redirect or whatever and at jsp, get it and put it at the field:
String something = (String)request.getAttribute("key");
...
<input type="text" name="someName" value="<%= something %>" />
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Cool, so what about if it's from form to form on the same page?
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
By that I mean going from textfield --> variable --> textfield all on the same page.
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
If you have a value in a textfield and want to put it in a java variable, you need to submit the form (or pass it as parameter at a hyperlink)
If you want to put a java variable to a textfield, when you go to the page you need to take it from the request and put it to the field when the page renders.
Remember first the code executes at the server and then the page is loaded. So you take the value from the request and then the field with the value will be displayed:
String something = (String)request.getAttribute("key");
...
<input type="text" name="someName" value="<%= something %>" />
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
At the server huh, so what should I do if I wanted that process to take place in after the page loaded?
CodeBoy101
Junior Poster in Training
71 posts since Dec 2007
Reputation Points: 8
Solved Threads: 0
Meaning put some value to a field and then push something so that this value would be put to a java variable?
Yes, submit the page.
When you see the JSP page, java code has already been executed. If you want to execute some more you need to go to the server by submitting.
If you have java code in a jsp page, that code is first executed at the server and then page is displayed.
So when you submit, the code that gets executed (getting values from the request) is at the server. When this is done, you see the page
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448