[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

Accessing html form values using pure Java?

Currently, I have a jsp that implements a dropdown list in html:

<form action="" method="POST" onSubmit=/*doesStuff*/) name="aForm">
    <SELECT NAME="paymentTypeList" onChange="getValue()">
        <OPTION value="1" selected>VISA</OPTION>
        <OPTION value="2">MasterCard</OPTION>
        <OPTION value="3">American Express</OPTION>
        <OPTION value="4">COD</OPTION>
    </SELECT>


The function getValue() is as follows:


function getValue()
{
  globalPaymentType = document.aForm.paymentTypeList.value;
}  

...where globalPaymentType is a global variable.




This is all well and good, but when it comes time to make an update  to the server (done in Java) I can't access the JavaScript value globalPaymentType, which is a problem. As such, I need a function written in Java that will accomplish roughly the same thing. Other good methods are welcome, also.


As always, any comments/examples are greatly appreciated.
0
Atomskish
Asked:
Atomskish
  • 7
  • 4
  • 3
  • +1
3 Solutions
 
Ryan ChongCommented:
You form need to be SUBMITTED, so that the JSP itself can get the value you want, else you need to access it via client side technology like Javascript, as what you're using above. For your server part, you can use the request.getParameter method to get the post data value, like:

<%

...
String globalPaymentType = request.getParameter("globalPaymentType");
...

%>
0
 
AtomskishAuthor Commented:
After form submission I tried request.getParameter("globalPaymentType"), but the printout just says "null". Is request.getParameter suppoed to work with javascript variables?
0
 
archrajanCommented:
In the onChange() event the form should be submitted for JSP to access the value selected from the Dropdown list. The JSP cannot access the globalPaymentType variable in javascript. JSP can only access the values of Form Elements. You could access the value selected in the Drop down list bu using

<%

....
String globalPaymentType = request.getParameter("paymentTypeList");
....

%>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AtomskishAuthor Commented:
^^ Submitting the form onChange() gets me the correct value when I request it, but ends up refreshing the page (resetting the dropdown selection back to the default selected, VISA). Is there any way to prevent this?
0
 
archrajanCommented:
u can use javascript init function to set the value back.

or else
u can use a submit button and submit the form to prevent the refresh.
jsp just needs the form to be submitted to get the values
0
 
archrajanCommented:
can i know more on what u r tryin to do...
bcos its not necessary to submit the form repeatedly
u can use javascript to achieve some dynamic solutions in the front end itself and i can help u with that
0
 
AtomskishAuthor Commented:
Well, the payment dropdown list is not the real form I need to submit - all I need from it is a way to grab the currently selected value whenever the user changes it, which is a pain using java. In javascript I don't have to submit the whole form to get the value.

Submitting my main form calls a validing function (in javascript) which in turn calls other functions. Once I start into those functions, document.aForm.submit() doesn't return the correct value anymore; I need to submit the form containing the dropdown information before I submit my main form. Is there some way to do this without refresing the page?
0
 
RMaruszewskiCommented:
Use the hidden field on you main form, holding the information you need. And whenever user will change value on the dropdown list, call the JS function, that copies selected value to the hidden field on the main form.

> resetting the dropdown selection back to the default selected, VISA

You will have to get this attribute back from the server. If server is just forwarding the request, the request attributes should be still there when you get back to the JSP page. Just try to read the request variable with the scriptlet, and set the dropdown list to appropriate value. However, if server is redirecting the request, you will have to copy request variable to the newly created request object.

Or, alternatively, you can use a framework like Struts, which allows you to automatize this process.
0
 
archrajanCommented:
try havin them in different forms
0
 
AtomskishAuthor Commented:
I tried splitting up the forms, but it's the same result.

The strange thing is that the form already has a "submit" button (input type="submit"), but if I use only that, the requested values will be null when I print them out. Another odd thing is that I have several text fields in the same form for the purposes of inputting an address. If I submit the form prior to clicking on the "submit" button (through onChange) and request the values the same way as I do for the payment table  (request.getParameter), ONLY the fields consisting totally of digits (i.e. the zip code field) will print out a non-null value. I also see that the payment type values (the only other non-null values) are integers only as well (0 to 5). Some relation?  
0
 
AtomskishAuthor Commented:
Uh..disregard that last part about only numbers printing...I forgot to add "" inside the alert(), stupid me.
0
 
RMaruszewskiCommented:
So... How does it look now, to sum it up? Do you have two forms (the "main" form, and the "dropdown" form)? Do you have text (or hidden) field on the main form? What's the "action" parameter of the main form? What's the "method" parameter of the main form? How does your "onChange" method looks like? How do you display the requested values?
0
 
AtomskishAuthor Commented:
When the dropdown list changes, the selected value is assigned to a hidden text box in the same (and only) form, aForm, to eliminate the page refreshing on each list change. There no longer is an "onChange" modifier. Form paramaters are as follows:

<form action="" method="POST" onSubmit="return CheckForm(this)" name="aForm">  


Submit button that doesn't work:

<input type=submit Value="Submit Order">


I still have the problem of having to "submit the form before I submit it." I added a checkbox to test this:

<INPUT TYPE=CHECKBOX NAME="stupidCrap" onclick="aForm.submit()">SUBMIT<P>


I only get non-null values if I check the box before submitting the form, and that ends up reloading the page. I can't figure out why the form isn't submitted with the "Submit" button before it calls "return CheckForm".
0
 
RMaruszewskiCommented:
Ok. Some answers for you:

1. Your CheckForm function have to return 0 or 1. If it return 1, the form will be submitter. If it return 0, the form won't be submitted. This method is always called when you click on the "submit" type button (you can check it with "alert"). The "onSubmit" method is used for form validation, so first the method is executed, and then (when it returns 1) the form will be submitted. It's not possible to submit the form first, and then call this function.

2. When you call "aForm.submit", the function assigned to "onSubmit" won't be checked (at least - not on Firefox). The form will be always submitted.

3. If the form "method" is set to "get", you should obtain the form values with "request.getParameter(name)". The parameters will be concatenated to the requested uri, and you'll have to encode them, if they contain any special chars. If the form "method" is set to "post", you should obtain the form values with "request.getAttribute(name)". The attributes will be send in the body of the request. There will be no need to encode these attributes, and there's no limitation of attribute lenght.

Is it any clearer now?
0
 
AtomskishAuthor Commented:
As of today I've basically changed the way that the pages display so this question isn't as relevant anymore, but I pretty much understand why the way I had it before wouldn't work right. Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now