Link to home
Start Free TrialLog in
Avatar of prain
prainFlag for United States of America

asked on

Want to capture the selected item from a list immediately after the click, in doPost()

Hello.
I am writing a Servlet. I have a JComboBox style drop down list box. I click an Item, I want to capture the selected item and then show this in a textfield. How do I capture the selected item from the list immediately in teh doPost()?.
Avatar of ramiseesniblic
ramiseesniblic
Flag of United Kingdom of Great Britain and Northern Ireland image

In the doPost() method, you should be able to do:

String value = request.getParameter( <param name> );

This should return the value of the option selected in the JSP.

Avatar of prain

ASKER

It does not. That's the problem I have. Here is the script I create the list.

 out.println("    <select  size =\"1\" name=\"listName\" " +
               "onClick=\"submitToDoPost(this, 'listNme')\" " +
               " style=width:102;color:#0000CD;font-size:11px > ");
......
The I have code to add items into the list. That's dicplayed nicely.

 Where the submitToDoPost() function is scripted like this......

 out.println("<html> ");
   out.println("<SCRIPT type=\"text/javascript\"> ");
   out.println("");
   out.println("function submitToDoPost(theBtn, paramValue)");
   out.println("{");
   out.println("   document.usereumgrform.generic_hidden_button.value=paramValue; ");
   out.println("   document.usereumgrform.submit(); ");
   out.println("}");

And in then in doPost() I do
//Capture the Generic Hidden Button input.
  if (req.getParameter("generic_hidden_button") != null && 
       req.getParameter("generic_hidden_button").equals("listName"))
  {
    String selectedValue  =  req.getParameter("listName");

    System.out.println("AAAAA : " +  selectedValue);
  }

This print statement is not being printed. I feel that the message is not sent to doPost() when the list item is clicked.

Avatar of prain

ASKER

Sorry I typed wrong...

out.println("    <select  size =\"1\" name=\"listName\" " +
               "onClick=\"submitToDoPost(this, 'listName')\" " +
               " style=width:102;color:#0000CD;font-size:11px > ");
what is the HTML on the hidden field?

Just looking at your code again:

>>if (req.getParameter("generic_hidden_button") != null && 
>>       req.getParameter("generic_hidden_button").equals("listName"))
>>  {
>>    String selectedValue  =  req.getParameter("listName");
>>    System.out.println("AAAAA : " +  selectedValue);
>>  }

By doing req.getParameter("generic_hidden_button"), you are getting the value of the hidden field that has been submitted, and then checking that it equals "listName".

Try changing the if statement to:

if (req.getParameter("generic_hidden_button") != null && req.getParameter("generic_hidden_button").length() > 0

Open in new window

Avatar of prain

ASKER

First of all, should onClick   the correct way of doing this into a dropdown list box?. This is correct I know to a Push Button. Why I am saying is that because when I have my drop down listbox (combobox) created like this :
out.println("    <select  size =\"1\" name=\"listName\" " +
               "onClick=\"submitToDoPost(this, 'listName')\" " +
               " style=width:102;color:#0000CD;font-size:11px > ");

It does not allow me to click an "Item" in the drop. In fact it does not allow me to select drom the drop. The moment the drop down thumb is clicked it calls doPopst(). So I think for a dropdown listbox, onClik may not be the corect form.
ASKER CERTIFIED SOLUTION
Avatar of ramiseesniblic
ramiseesniblic
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of prain

ASKER

Great. That works. I have another question. I will first close this. please check the same area.