• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to get the hidden field from JSP

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
kim pham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
kim pham
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read the article I linked to earlier? You seem to have a misunderstanding of when things execute and where.
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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?

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic