1

i have a jsp in which i have select tag and i want to get the and the value selected from the select in jsp in my Servlet

<select id="listoffood" name="dropdown" onchange="foodname();">
<option value="bg">Burger</option>
<option value="pas">pasta</option>
<option value="pi">pizza</option>
</select>
<div id='content'></div>

here is the javascript code

function foodname()
{

  var xmlHttpReq = false;
    var self = this;
    document.getElementById('content').innerHtml='';
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('GET', "InformationServlet", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send(null);

    self.xmlHttpReq.onreadystatechange= function ()
    {
        //alert(document.getElementById('content'));
        if (self.xmlHttpReq.readyState==4)
        {
        if (self.xmlHttpReq.status == 200)
        {

        document.getElementById('content').innerHTML=self.xmlHttpReq.responseText;
        }
        }
    };

}

What i have done is used a get Attribute like this but it aint working its showing null

protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException 

    {
    // TODO Auto-generated method stub

    String coun = request.getParameter("dropdown");
    PrintWriter out=response.getWriter();
    System.out.println("here : "+coun);
}

Thanks in advance and any piece of code is highly appreciated.

Michaël
  • 3,679
  • 7
  • 39
  • 64
Anish Sharma
  • 149
  • 1
  • 2
  • 10

5 Answers5

1

Just change your AJAX open() request as

var select = document.getElementById("listoffood");
self.xmlHttpReq.open('GET', "InformationServlet?dropdown=" + select.options[select.selectedIndex].value, true);
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • its http://localhost:8080/Distributor/InformationServlet and its showing an error in the code u gave.. the error is cannot read property 'undefined' of undefined – Anish Sharma May 03 '13 at 12:09
  • nothing is wrong with the url its showing errror in js.. i have mentioned the error in my previous comment – Anish Sharma May 03 '13 at 12:14
  • @AnishSharma Did you try with `var select`. – Ravi K Thapliyal May 03 '13 at 12:28
  • yes i did it than shows cannot read property "options" of the undefined – Anish Sharma May 03 '13 at 12:35
  • @AnishSharma I just tested the code and successfully did a GET for `http://www.google.com/search?q=pasta`. Can you do an `alert(document.getElementById("listoffood"));` As per your HTML this shouldn't be `undefined`. Please verify. (two "f"s here) – Ravi K Thapliyal May 03 '13 at 12:48
  • Thanks it worked i was making a mistake i commented one line by mistake.. That was all it – Anish Sharma May 06 '13 at 06:16
  • Ah, glad it worked! I was wondering what went wrong since I'd tested my solution successfully. If you found this helpful can you please consider giving an up vote as well. – Ravi K Thapliyal May 06 '13 at 07:07
  • i have one more question in case if u r interested in answering.. i am posting the link http://stackoverflow.com/questions/16397207/iterate-arraylist-in-jsp – Anish Sharma May 06 '13 at 10:55
0

Use method getParameterValues(String).
This is because <select> tag can have multiple selected values (e.g. select multiple)

String[] coun = request.getParameterValues("dropdown");
Ilya
  • 29,135
  • 19
  • 110
  • 158
0

Check this post:

How to use onClick() or onSelect() on option tag in a JSP page?

You seems to be using select tag without any action such as onchange

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

You can get selected item text from list box using

var e = document.getElementById("dropdown");
var selectedValue = e.options[selectBox.selectedIndex].value

in your JS method and send this value with servlet's link.

Ankit Singla
  • 198
  • 2
  • 15
0

get the value from select tag

var e = document.getElementById("dropdown");
var selectedValue = e.options[selectBox.selectedIndex].value;

and edit js function

self.xmlHttpReq.open('GET', "InformationServlet?dropdown="selectedValue , true);

finally your should looks like

function foodname()
{

    var e = document.getElementById("dropdown");
    var selectedValue = e.options[selectBox.selectedIndex].value;

  var xmlHttpReq = false;
    var self = this;
    document.getElementById('content').innerHtml='';
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }

    self.xmlHttpReq.open('GET', "InformationServlet?dropdown="selectedValue , true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.send(null);

    self.xmlHttpReq.onreadystatechange= function ()
    {
        //alert(document.getElementById('content'));
        if (self.xmlHttpReq.readyState==4)
        {
        if (self.xmlHttpReq.status == 200)
        {

        document.getElementById('content').innerHTML=self.xmlHttpReq.responseText;
        }
        }
    };
}
NPKR
  • 5,368
  • 4
  • 31
  • 48