3

I want to use a dropdown menu on my JSP but... I have no idea how to capture the Value of the Selected Item and Pass it on to my Servlet and have some QUERY to add the value to my database.

Can you give me some idea or clue how to code it?

PS. I also need items at dropdown menu converted into an Integer cause i'll be adding it to the stored data at my database.

Will this be hard for a starter like me? should I use Textbox and let the user input an INTEGER manually instead of a Dropdown menu?

Thanks a lot in advance :)

My Jsp Menu is like this:

<body>
    <form action="AddPoints">
      <table width="408" border="0">
        <tr>
          <td width="402"><h3 align="center">Enter Points:</h3>
            <h3 align="center">
              <label for="Points"></label>
              <select name="Points" size="1" id="Points">
                <option value="5" selected>5</option>
                <option value="10">10</option>
                <option value="15">15</option>
                <option value="20">20</option>
                <option value="25">25</option>
              </select>
              <br/>
            </h3>
            <h3 align="center"><strong></strong>
              <input type="submit" name="AddPoints" id="AddPoints" value="Add Points">
          </h3></td>
        </tr>
      </table>
</form>
</body>

Also I am wondering if the value at this line: <option value="25">25</option> is the real value that my servlet can capture?

Sorry if i have so many questions... :)

iamanapprentice
  • 411
  • 5
  • 19
  • 37
  • 1
    Better design would be to maintain option values and their labels in server side like in `ServletContextListener` or some kind. Check [http://stackoverflow.com/questions/8840655/how-get-selected-option-label-from-a-dropdown-list] – Kibrom Gebre Aug 24 '16 at 12:07

3 Answers3

3
int selectedItem;
if(request.getParameter("Points")!=null)
{
   selectedItem=Integer.ParseInt(request.getParameter("Points"));
}
Novice
  • 2,447
  • 3
  • 27
  • 33
0

First up you probably want to add a Method='post' to your form tag so that is passes data on to the jsp.

As for actually retrieving the selected value your code will probably want to look something like this:

 var selection = request.getParameter('Points');

You then have the selected value stashed in a variable that you can use in an SQL query.

Something like:

var sQL = "Select * From xxx where Points="+selection

Making sure that you have an integer can be accomplished in jsp with the handy parseInt() function

As to your last question. the value attribute is what will actually be caught yes, the number between the option tags is just what is actually displayed to the user

celem
  • 404
  • 3
  • 10
-1
int selectedItem;

if((selectedItem=Integer.ParseInt(request.getParameter("Points"))!=null)
{

         // It woud take Less Time
         // Do Your Logic
}
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Nitul
  • 997
  • 12
  • 35
  • Bad code. This throws `NumberFormatException` when parameter is `null`. Even then, it would in theory have thrown a `NullPointerException` during autoboxing on compare to `!= null` because an `int` can never be `null`. – BalusC Mar 21 '11 at 12:47