Ancora ajax e prototype (servlet)

Continuando il post precedente, ecco un esempio di come può essere il metodo post della servlet chiamata dalla funzione javascript nella Jsp:

function miaFunzione()
{
var url = 'miaServlet';
var pars = 'param1=1&param2=2' ;
var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: showResponse, asynchronous: false });

Ajax.Responders.register(myGlobalHandlers);

}

Quello che fa il seguente metodo post è di restituire questo XML:

<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
<index>

<mioTag>valore</mioTag>

</index>

Ed ecco il codice del metodo doPost:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/xml; charset=utf-8");
try {

PrintWriter out = response.getWriter();
String tmp = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";

tmp = "<index>";
out.print(tmp);

String valore = ... leggi il valore da qualche parte...
tmp = "<mioTag>" + valore + "</mioTag>";
out.print(tmp);

tmp = "</index>";
out.print(tmp);
out.close();

} catch (Exception e) {

System.out.println("Errore: " + e);
}

}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>