aspose file tools*
The moose likes JSP and the fly likes How to get the hidden field from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get the hidden field from JSP" Watch "How to get the hidden field from JSP" New topic
Author

How to get the hidden field from JSP

kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
Hi All,
I have the hidden field defined as:
<form method="post" name="userform" id="userform"><input
<input type="hidden" id="movieIdHidden" name="movieIdHidden"></input>
....
When the form is reload I set this field as
document.userform.movieIdHidden.value=1000;

In the JSP I read this value as:
string strmovie3 = request.getParameter("movieIdHidden");
// convert to int
int movie3 = parseInt(strmovie3 );
Is this the right syntax to read the hidden value from the form? I think this is the right syntax to get the hidden value from JSP but
I always get the movie3 = 0 but I already set this value to 1000( document.userform.movieIdHidden.value=1000;) in
one of the JavaScript.

Thank in advance.
Kim
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

kim pham wrote:When the form is reload I set this field as
document.userform.movieIdHidden.value=1000;

Why set in JavaScript? Why not use a value clause on the element using JSP?

And if you are going to use JavaScript, the referencing mechanism you are using is antiquated and fragile. It should be modernized to:

In the JSP I read this value as:

You are submitting a form to a JSP? Bad practice! Always submit forms to servlets to process the data. Modern JSPs should never have an Java code within them!

string strmovie3 = request.getParameter("movieIdHidden");

This is the correct way to fetch the value (but should be in a servlet as noted above). If the value is properly submitted on the HTTP request, this will fetch it.

Use a tool such as HttpFox in Firefox to look at the request and see if the value is being properly submitted.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
Hi,
Thanks so much for anwsering my question.
Why set in JavaScript? Why not use a value clause on the element using JSP?

I set the value in JavaScript because it is depending what the user selects in the dropdown box. The value can be different and it is not 1000 at all the time.

What do you mean by " Why not use a value clause on the element using JSP?"
Thanks again,
Kim

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

kim pham wrote:I set the value in JavaScript because it is depending what the user selects in the dropdown box. The value can be different and it is not 1000 at all the time.

OK, that wasn't clear. It looked like a hard-coded value. Obviously a client-side value must be set using JavaScript.

Have you verified that the value is being submitted?
kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
I need to download the FireBug tool to debug this issue why reading the value (request.getParameter("movieIdHidden") is returned 0 instead of the value that I set in JavaScript. I will keep you posted as soon I am able to nail down the issue.
Thanks,
Kim
kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
Hi ,
document.getElementById('movieIdHidden') is null when the form is reloaded again after setting this value in JavaScript with the value of 1000.
Is there any way that we can modify/set the JSP's variable in JavaScript?

I have defined movieId in the JSP and want to set this variable to different value in the JavaScript when the user selects different option in the dropbox.
Thanks,
Kim
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

kim pham wrote:document.getElementById('movieIdHidden') is null when the form is reloaded again after setting this value in JavaScript with the value of 1000.

What do you mean by reloaded? If an element with the id of movieIdHidden exists, there is no way you can get a null from document.getElementById('movieIdHidden') .

Is there any way that we can modify/set the JSP's variable in JavaScript?

Of course not. Please read this article.

I have defined movieId in the JSP and want to set this variable to different value in the JavaScript when the user selects different option in the dropbox.

OK, so what part of that isn't working?
kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
Hi ,
As mentioned in previous posts I set this value in JS as
document.getElementById('movieIdHidden').value=1000;
and in JSP I read this value as
<%
String movie3Str = request.getParameter("movieIdHidden");
%>
But the movie3Str is NULL. I would like to have movie3Str is '1000' which is set in JS but this is NULL?
Have I missed anything here in JSP.
Thanks,
Kim
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
How are you "reloading" the form?

Is the user pushing a button? manipulating a control?

Is there an HTTP request being sent?
How is the request being generated? Is there a submit button somewhere?

kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
No HTTP Request at all.
Basically if the user selects different value in the selected box the JS ratingSwitch() function will be triggered as defined below. In this FS I set the movieIdHidden = 1000 and I won't able to read it back from JSP(String movie3Str = request.getParameter("movieIdHidden"); )

function ratingsSwitch() {
var regionType = $("select[name='regiontype'] option:selected").attr("name");
$(".ratings").each(function () {
$(this).css("display", "none");
});
$("."+regionType).css("display","inline");


document.getElementById('movieIdHidden').value=1000;
}

<%
String movie3Str = request.getParameter("movieIdHidden");
%>
Is this the right way to read from JSP with the hidden value.
Thanks,
Joanne
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

That line of code would be the right way to get that parameter, but it would have to be in a JSP which processes the request from the form.

But you seem to have it right next to code from something which generates that form. Are you sure you have it in the right JSP?
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

kim pham wrote:No HTTP Request at all.


you cannot get this hidden field value this way.
until include this hidden field in GET OR POST request. The request object retrieves the values that the client browser passed to the server during an HTTP request such as headers, cookies or parameters associated with the request.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
Hi,
Yes, It is the right place for JSP(mycode.js and mycode.jsp)
I attached both of my JSP(mycode.jsp) and JS(mycode.js) script.(See below)
In JSP I retrivied this movieIdHidden as highlight in orange color and this value is set in JS function ratingsSwitch as highlight in orange color Thanks,
Kim
kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
Sorry I am new to this post.
Here is my codes:


and here is my JSP


movie3Str is always empty even I set it in javascript.
Any idea.
Thanks,
Kim


Edited by Stefan:
Not pretty formatting. [code] tags. See?
Click the link from Bear's post above to find out more. There is even a handy little button when editing a post...
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Ok, step back and think about the ordering of the execution of this code in the JSP Lifecycle.

When does java code get run?
When does javascript code get run?

Hint: request.getParameter() indicates that it needs an HttpRequest made...
No request == no java code executed.

kim pham
Greenhorn

Joined: May 11, 2011
Posts: 13
Hi ,
As mentioned in previous post
Hint: request.getParameter() indicates that it needs an HttpRequest made...
No request == no java code executed.
That is not true. Java code has executed.
Thanks,
Kim
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Did you read the article I linked to earlier? You seem to have a misunderstanding of when things execute and where.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
>That is not true. Java code has executed.
You are setting the value in javascript.
You expect that value you set in javascript to be picked up by a call to request.getParameter().

But is that call to request.getParameter() executed before, or after the javascript?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get the hidden field from JSP