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.

10
  • Will it go to same servlet ??? post your full html form Commented May 3, 2013 at 10:50
  • what does method foodName() do? Is your select inside form? Commented May 3, 2013 at 10:50
  • #kshitj food name goes to javascript and js than calls servlet by ajax. @Baadshah yes it goes to same servlet on onChange method Commented May 3, 2013 at 10:52
  • Are you able to get other parameters from this JSP page in your servlet ? Commented May 3, 2013 at 10:58
  • i dnt have any html form i just have a select tag on my jsp @AnkitSingla i dont have ant other parameters on my jsp its jusst the select tag Commented May 3, 2013 at 10:59

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

8 Comments

its localhost:8080/Distributor/InformationServlet and its showing an error in the code u gave.. the error is cannot read property 'undefined' of undefined
nothing is wrong with the url its showing errror in js.. i have mentioned the error in my previous comment
@AnishSharma Did you try with var select.
yes i did it than shows cannot read property "options" of the undefined
@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)
|
0

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

String[] coun = request.getParameterValues("dropdown");

1 Comment

I knew somebody will post this. multiple is not true for select.
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

2 Comments

onchange=foodname(); I think you missed seeing that!
@AnishSharma Using a event handler on select is fairly common and not the problem here. You don't need to change that.
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.

5 Comments

how can i send the value with servlet link
InformationServlet?dropdown=selectedValue And use request.getParameter("dropdown"); in your servlet as you have already done.
<display-name>Distributor</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>InformationServlet</servlet-name> <servlet-class>requestData.InformationServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>InformationServlet</servlet-name> <url-pattern>/InformationServlet</url-pattern> </servlet-mapping> </web-app>
@AnishSharma Did you check my answer?
@AnishSharma : Have you given the correct package path for <servlet-class> parameter ??
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;
        }
        }
    };
}

1 Comment

can you share your web.xml file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.