开发者

I find a problem with sending receiving parameter

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 from http://localhost. Try a more decent webbrowser instead, for example Firefox.

Further on I discover several flaws in this approach:

  1. The </body></html> really doesn't belong there at bottom of the JSP. Remove them.
  2. The JSP is the wrong tool for this job. Make use of a Servlet.
  3. The if (it.hasNext()) piece is superfluous as it's already handled by the for statement.
  4. The XML should have one root element. Right now you're writing multiple <wa> elements to it.
  5. The SQL is prone to SQL injection attacks. Make use of named queries.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜