Preciso retornar o resultado da consulta do servlet (descrição do imovel) para a página jsp…
Pesquisando encontrei sobre o uso do jquery…Segue código que estou utilizando…
Não consigo trazer o retorno…
Estou utilizando o jquery-1.4.2.min.js
Página jsp
<%@ page import="java.sql.*" contentType="text/json"%>
<%
String empid = request.getParameter("empid");
String sql="";
Connection dbCon;
String valores = "";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/imoveis","root","xxx");
java.sql.Statement s = con.createStatement();
sql = " select descricao from imovel where idImovel = " + empid;
ResultSet rs = s.executeQuery(sql);
while(rs.next())
{
String idImovel = rs.getString("descricao").toString();
valores += idImovel+ "\n";
}
rs.close();
s.close();
} catch (ClassNotFoundException e) {
System.out.println("Erro de configuracao" + e.getException());
} catch (SQLException sqle) {
System.out.println("Erro de SQL:" + sqle.getMessage() + " - " + sql);
}
response.getWriter().println(valores);
System.out.println(valores);
%>
Página html
<html>
<head>
<title>AJAX and JSON with JQUERY </title>
<script language="javascript" src="jquery-1.4.2.min.js"></script>
<script language="javascript">
function getEmployeeDetails(){
$.getJSON( "empdetails.jsp",{empid : $("#empid").val()}, displayResult);
}
function displayResult(data) {
if ( data.error) // emp not found
{
$("#IdImovel").val("") // clear fields
$("#descricao").val("")
alert( data.error);
}
else // Found employee. Display details
{
$("#IdImovel").val( data.IdImovel);
$("#descricao").val( data.descricao);
}
}
</script>
</head>
<body>
<form id="form1">
<h2>Employee Details</h2>
<table>
<tr>
<td>Employee ID : </td>
<td><input type="text" id="empid" size="10"/> <input type="button" value="Get Details" onclick="getEmployeeDetails()" /> </td>
</tr>
<tr>
<td>Employe Name : </td>
<td><input type="text" id="IdImovel" readonly size="30"/></td>
</tr>
<tr>
<td>Salary : </td>
<td><input type="text" id="descricao" readonly size="30"/></td>
</tr>
</table>
</form>
</body>
</html>