tags:

views:

299

answers:

1

I am trying to send the clicked value of a dropdown list to my servlet to run a SQL query using the value received. To do it I am using Ajax like that:

function showProject(prj) {
    xmlhttp = GetXmlHttpObject();
    if (xmlhttp == null) {
        alert ("Browser does not support HTTP Request");
        return;
    }

    var url = "ServletxmlGenerator.java";
    idprj = prj.options[prj.selectedIndex].value;
    url = url + "?idprj=" + idprj;

    xmlhttp.onreadystatechange = stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
}

The servlet when

String projectcode=(String) request.getParameter("idprj");

returns null.

When I select a value from the JSP that construct the dropdown list and from which the function showProject is handled the same thing is happned. It returns always null. So the parameter(idprj) is not passed anyway. How can I handle with this. I need to send the selected value to the servlet to run my SQL query.

+1  A: 

Just debug your Javascript code. What does for example this say?

idprj = prj.options[prj.selectedIndex].value;
alert(idprj);

For better debugging, I suggest to pick Firebug.

Also debug your Servlet by executing it independently (just enter its URL in browser address bar like http://example.com/contextname/ServletxmlGenerator.java?idprj=1) and by tracking the request and Servelt code.

BalusC
it display the correct value
kawtousse
What happens if invoke the servlet manually in webbrowser by entering the full address, e.g. `http://localhost:8080/contextname/ServletXmlGenerator.java?idprj=1` (by the way, why did you map it with `.java` extension?)
BalusC
No I'm sorry it doesn't display the right value when i run the alert
kawtousse
when I invoke the servlet it display just the following:<?xml version="1.0" encoding="utf-8" ?> <wa /> it is clear that the idprj(the parameter) was not received
kawtousse
OK, the JS code thus doesn't display the right value? Well, fix it accordingly. And the servlet result suggests that the `idprj` is not received, okay, but did you also check the outcome of `System.out.println(idprj)` when you invoke it independently by browser address bar? Learn to debug. Learn to track the request and the code. Try to think logically. This is all pretty trivial. Good luck.
BalusC
when i enter a correct idprj the xml file is generated correctly please help this is my last step to complete this successfly
kawtousse
OK, thus the servlet works fine, but Javascript not. The `prj.options[prj.selectedIndex].value` thus never returns a value? Is `prj` correct? Is `prj.selectedIndex` correct? Is `prj.options[prj.selectedIndex]` correct? Does the `<option>` element in HTML has a `value` attribute? Etcetera. That kind of little things which you can easily debug/track down using Firebug and/or the `alert()` way :)
BalusC
for the.java extension in the url I think that i must give the url with the extension of my class to switch correctly the parameter to the right place
kawtousse
Ok THINKS A LOT:)
kawtousse
So the problem is fixed? OK, you're welcome. Don't forget to vote and mark the answer accepted. Else the question will look like to remain unanswered. Keep the spirit of Stackoverflow alive :)
BalusC
I tried all what you suggested it is all ok I'am sure of the rest of the code but I still have one more thing that i dont understand is the url given in the JS MORE than that i will send a value selected to my servlet so since nothing is selecetd it is always null value for the returned parameter.I even select an item and rerun the sevlet to display the xml but it doesn't work. I am really confused at this point: In one hand I asked the servlet to built xml with a selected value and in the other hand I give the selected value from a runed JSP. Have you any explication please?
kawtousse
Even when I send an example of a variable the servlet dont receive it with JS
kawtousse
I don't understand the problem you're trying to describe. By the way, the `.java` extension is completely superflous. You can even map the servlet on for example `/getxml` and use `http://example.com/context/getxml?idprj=1`. The URL does **not** have any influence on processing of request parameters.
BalusC