This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes JSP and the fly likes getting null value, from a variable being sent from JavaScript in the page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "getting null value, from a variable being sent from JavaScript in the page" Watch "getting null value, from a variable being sent from JavaScript in the page" New topic
Author

getting null value, from a variable being sent from JavaScript in the page

thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

I have a JSP file (trial.jsp). In that file, I'm taking username(any value) from user through a text box(name="username")
on submitting the form(name="form" onSubmit="call()") through submit button in HTML code, and then
accessing that username through a JavaScript function named "call()".



Now, I'm accessing/printing these name variable in the JSP Scriptlet after the form in the same file .



But I'm getting the null value of name in JSP scriptlet.
What could be the problem?

I have tried using



but problem persists. Would you help?



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

What exactly is the problem? Just posting code without an explanation isn't very helpful.

From the topic title, I assume that you may need to read through this article.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

Thank you very much, Bear Bibeault. I'm very glad to be here on JavaRanch. and I'm extremely sorry for not being reading instructions for creating new topic. I assure you I'll be taking work to be able to post a good readable topic with proper quotes. Sorry for inconvenience.


thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

My problem is solved now, I got it right..!!!

When I use the input button type, 'button' instead of 'submit' type, my code worked. But I wish if it could work when input type was submit. any suggestion for that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

A submit button will actually submit the form. To send the variable you must include it as a (hidden) input field in your form.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

But I haven't used any hidden variable here.. how did it worked? I got no idea.
so you mean, if we use submit button, I should make variable as hidden, right? viz.

<form name="myForm" onSubmit="return validate()" >
<input type="submit" value="submit" ></input>
</form>


or

<form name="myForm">
<input type="submit" value="submit" onSubmit="return validate()"></input>
</form>


and if we pass the variable to JSP Scriptlet from the JavaScirpt function validate(), will it work?
and I'm not getting what is hidden variable you're talking about, would you explain me further about it?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

Oh I see..!! Thank you Paul.. Thank you very much..!!
I think I need to get through HTML basics now specially about Hidden values and Submit button..

But would you tell me, what is the purpose for making that value as a hidden? Is it beneficial while sending values? or it has any other purpose..?
or Submit button has to anything to with Hidden value?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Submitting a form will only send values of the input fields (<input>, <select>, <textarea>) inside the form. Therefore, to send any value it needs to be the value of such an element. The <input type="hidden"> field is meant for values you want included when the form is submitted but that should not be visible on the screen.
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

oh I see. I got it now what hidden meant..!! Thanks Rob..But, why couldn't my code worked when using form's onSubmit event while sending the values to scriptlet, but it worked fine when I used button's onClick event. you have any idea or suggestion?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Buttons by default don't do anything (they're meant to have onclick event handlers), so when you clicked the button, all it did was execute the code you've attached to its onclick event.
Submit buttons by default submit the form, so when you clicked that button it executed the code and then still submitted the form. It wouldn't surprise me if your server logs would have shown an extra request for trial.jsp - as in, your code change the location, but then the browser changed it again because of the form.

You can stop the form from submitting by not using onclick but onsubmit for the form, and returning false from that method. Like you probably already do with validate().
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

Thank you very much Rob for the reply. Yes. Now, I'm getting a clearer difference between submit and click, and it has helped in this program too, like as the first way when using onClick button event on button itself and the second way while using submit button with form submit event way. I haven't tried second( submit button with form submit event ) thoroughly and knowingly, but now I'll try it to work correct. Thanks again.
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

So now, this is my permanent JavaScript function in (login.jsp) is as



and now remaining JSP page in login.jsp, (1st way, i.e. using Button input type with onClick event) looks like,



and it works fine, values(username and password) are getting printed through scriptlet.
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

But when I tried the same using 2nd way ( submit button with form's onSubmit event) as provided JAVASCRIPT function remains same,
login.jsp would change as,



it doesn't give me the values(username and password), rather they are shown as null. what could possibly go wrong above??
is their anything to do with action attribute in form, giving the same page link(login.jsp) to action as below,

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

The form field names are called "username" and "password", not "uname" and "pass". Also, when using the form's own submit functionality, you don't need to call that method - the form does that for you. Well, assuming this page is login.jsp. Without an explicit "action" attribute it will open the current page.
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

are you saying, we don't have to call form's on Submit event explicitly, that event by default gets called when we hit submit button?? I didn't get it right. So how does Submit button come to know about the exact function name written in JavaScript? I'm confused now so much. please would you tell me in some other way.

I would re-write my exact code or my aim in the code, to be more explanatory. it's as follow,


actually, its not so very intentional program but just to make sure that we can pass variables from javascript to jsp. But specifically I would like to state step by step what I have coded in trial.jsp,
1.form field text box values, as named(username,password) are accessed through javascript in validate_form() function.

2.stored in param1 and param2 variables their in validate_form() javascript function

3. their, they are being sent to the JSP scriptlet written after <form>, using window.location.replace
(param1, param2-> uname, pass)

4.in JSP scriptlet, they are being accessed as ( uname, pass).


So, I have been trying successfully run the code and getting printed values of form fields in JSP scriptlet when I use button's onclick event. But when I use form's submit method, it doesn't work.
i was trying for submit button as

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

When you submit a form, the page is automatically forwarded to the form's action (login.jsp) with all input fields, text area's and selects added as parameter key-value pairs. In this you would need to remove the onsubmit function, and change login.jsp to read parameters "username" and "password" instead of "uname" and "pass". Right now you're doing stuff manually that the form will already do for you.
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

Thanks Rob for the reply. I get what you're saying, like, we won't need onSubmit action to get input parameters. We just get it automatically with submit button. and yes, I've completely cleared that concept with your replies.
But, what you're saying is not my objective here, I don't want that input parameters to be printed directly in the jsp scriptlet. I'm sorry that I might have not cleared enough to know you the objective of my program.
My goal was to access input parameters in javascript function. and then send that input parameters to same or any other jsp and print their. In this case, I've been trying to print them on the same jsp(login.jsp). In case, parameters could have send to other jsp page too, to get printed.
I've attached the file for your ref in case.


[Thumbnail for login.png]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

In that case don't use the form's submit functionality but instead use a regular button with an onclick event like you've done previously. You still need to make sure that the names of the parameters you send through JavaScript match the names of the form fields.
thor aniket
Ranch Hand

Joined: Jan 21, 2012
Posts: 40

Ok. You're right!! I think that its not possible at all while using the submit's functionality to reload the same page. and its of no further use. rather AJAX and Co. functionalists can solve my simliar problems. Thank you very much Rob for your kindly help. :)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getting null value, from a variable being sent from JavaScript in the page