This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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?
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.
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?
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?
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.
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?
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().
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.
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,
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.
I would re-write my exact code or my aim in the code, to be more explanatory. it's as follow,
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
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.
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.
I've attached the file for your ref in case.
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. :)