How to get the XML translation to HTML dropdownlist with ajax? I send the parameter with GET method but the JSP file that generates the XML don’t receive it.
var url = "responsexml.jsp";
url = url + "?projectCode=" + prj.options[prj.selectedIndex].value;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
And then in responsexml.jsp I do like that:
<%
String projectcode = (String) request.getParameter("projectCode");
System.out.println("++++projectCode:=" + projectcode);
Session s = null;
Transaction tx;
try {
s = HibernateUtil.currentSession();
tx = s.beginTransaction();
Query query = s.createQuery("SELECT from Wa wa where wa.ProjectCode='" + projectcode + "'");
response.setContentType("text/xml");
PrintWriter output = response.getWriter();
output.write( "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
//response.setHeader("Cache-Control", "no-cache");
if (projectcode != null) {
for (Iterator it = query.iterate(); it.hasNext();) {
if (it.hasNext()) {
Wa object = (Wa) it.next();
//out.print( "<item id=\"" + object.getIdWA() + "\" name=\"" + object.getWAName() + "\" />");
output.write("<wa>");
output.write( "<item id=\"" + object.getIdWA() + "\" name=\"" + object.getWAName() + "\" />");
output.write("</wa>");
}
}
}
} catch (HibernateException e) {
e.printStackTrace();
}
%>
</body>
</html>
With this code I don’t have my XML file. I got this error:
The server did not understand the request, or the request was invalid. Erreur de traitement de la ressource http://www.w3.o…
To be sure: is that the entire JSP file? The error message namely suggests that you’ve a
<!DOCTYPE>in top of it which points to a DTD at w3.org, but that the webbrowser in question can’t load it. The error message also suggests that you’re using IE to test this all, this webbrowser is known to have odd restrictions and quirks with regard to opening XML files fromhttp://localhost. Try a more decent webbrowser instead, for example Firefox.Further on I discover several flaws in this approach:
</body></html>really doesn’t belong there at bottom of the JSP. Remove them.if (it.hasNext())piece is superfluous as it’s already handled by theforstatement.<wa>elements to it.Not really a problem, but more a suggestion, I recommend to have a look at jQuery to fire ajaxical requests and do DOM manipulation in a nice, concise and crossbrowsercompatible way.