Estou usando um formulário jsp que envia a solicitação via ajax para o servlet, até ai tudo bem.
O problema que estou enfrentando é retornar a resposta (response) do servlet via ajax para o jsp.
Estou usando jQuery para as requisões ajax.
Não sei como chamar o html gerado pelo servlet para meu callback.
Servlet
// meu servlet
String pesquisa = request.getParameter("param");
Collection paciente = new ArrayList();
clsIntegracao clsin = new clsIntegracao();
String sql = ("Select * from tblpaciente where nomePaciente like '%"+pesquisa+"%'");
System.out.println(sql);
ResultSet res = clsin.query(sql);
try{
System.out.println("um");
while (res.next()){
clsin.setIdPaciente(res.getInt(1));
clsin.setCartaoSus(res.getInt(2));
clsin.setCartaoProvisorio(res.getInt(3));
clsin.setNomePaciente(res.getString(6));
paciente.add(clsin);
clsi = null;
clsi = new clsIntegracao();
}
}catch (Exception e){
System.out.println("Usuarios Erro");
e.printStackTrace();
}
request.setAttribute("pacientes",paciente);
request.getRequestDispatcher("relPaciente.jsp").forward(request, response);
Ajax com Jquery
$(document).ready(function() {
var options = {
target: '#output2',
beforeSubmit: showRequest,
success: showResponse, // post-submit callback
// other available options:
url: 'svtControle?opta=10', // override for form's 'action' attribute
type: 'get' // 'get' or 'post', override for form's 'method' attribute
//dataType: null // 'xml', 'script', or 'json' (expected server response type)
//clearForm: true // clear all form fields after successful submit
//resetForm: true // reset the form after successful submit
// $.ajax options can be used here too, for example:
//timeout: 3000
};
// bind to the form's submit event
$('#myForm2').submit(function() {
// inside event callbacks 'this' is the DOM element so we first
// wrap it in a jQuery object and then invoke ajaxSubmit
$(this).ajaxSubmit(options);
// !!! Important !!!
// always return false to prevent standard browser submit and page navigation
return false;
});
});
// pre-submit callback
function showRequest(formData, jqForm, options) {
// AKI QUE TENHO QUE PEGAR O HTML DO SERVLET
alert('About to submit: \n\n' + queryString);
return true;
}
// post-submit callback
function showResponse(responseText, statusText) {
// for normal html responses, the first argument to the success callback
alert('status: ' + statusText + '\n\nresponseText: \n' + responseText +
'\n\nThe output div should have already been updated with the responseText.');
}
}
Desde já agradeço, qualquer dica já me é válida.