943,772 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 2845
  • JSP RSS
Jun 21st, 2009
0

How to assign the value in a text field to a variable in jsp using Java

Expand Post »
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?
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jun 21st, 2009
0

Re: How to assign the value in a text field to a variable in jsp using Java

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:
JSP Syntax (Toggle Plain Text)
  1. String something = (String)request.getAttribute("key");
  2. ...
  3.  
  4. <input type="text" name="someName" value="<%= something %>" />
Last edited by javaAddict; Jun 21st, 2009 at 12:25 pm.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jun 21st, 2009
0

Re: How to assign the value in a text field to a variable in jsp using Java

Cool, so what about if it's from form to form on the same page?
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jun 21st, 2009
0

Re: How to assign the value in a text field to a variable in jsp using Java

By that I mean going from textfield --> variable --> textfield all on the same page.
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jun 22nd, 2009
0

Re: How to assign the value in a text field to a variable in jsp using Java

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:
JSP Syntax (Toggle Plain Text)
  1. String something = (String)request.getAttribute("key");
  2. ...
  3. <input type="text" name="someName" value="<%= something %>" />
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jun 22nd, 2009
0

Re: How to assign the value in a text field to a variable in jsp using Java

At the server huh, so what should I do if I wanted that process to take place in after the page loaded?
Reputation Points: 8
Solved Threads: 0
Junior Poster in Training
CodeBoy101 is offline Offline
71 posts
since Dec 2007
Jun 22nd, 2009
0

Re: How to assign the value in a text field to a variable in jsp using Java

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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: how to make autoID when we use string?
Next Thread in JSP Forum Timeline: How to get mysql output to table as a link?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC