Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value of <select> in servlet

Tags:

java

servlets

I have:

<select id="isTitles">
    <option value="true">enable</option>
    <option value="false">disable</option>
</select>

on my index.jsp. And I would like to know what had been chosen:

response.getWriter().write( "User chose: " + request.getAttribute( "isTitles" ) );

But I've got "User chose: null"... = (

like image 440
nightin_gale Avatar asked Jan 24 '14 08:01

nightin_gale


People also ask

How to get select option value in Java?

The val() method returns the value of selected attribute value. let selectedValue = $("#selectVal option:selected"). val(); The text() method returns a string of the selected option.

How to get the selected value of dropdown in jsp?

Direct value should work just fine: var sel = document. getElementsByName('item'); var sv = sel. value; alert(sv);


2 Answers

You need to pass the name of your attribute as name="isTitles"

<select name="isTitles">
</select>

request.getParameter() understands the name of the parameter

like image 56
MS- Avatar answered Sep 24 '22 23:09

MS-


Change your HTML code to:

<select id="isTitles" name="isTitles" >

The id attribute of select tag is mainly for DOM usages, and name attribute is to specify the key of a form data.

And then use request.getParameter("isTitles"), your will get the right value. getParameter is for retrieving parameters from form data and URL query string. While getAttribute is for transfer data through the process chain during the request life cycle.

like image 22
Weibo Li Avatar answered Sep 20 '22 23:09

Weibo Li