Olá Gujeiros, estou tentando desenvolver um formulário onde o usuário entra com um código de um orçamento e quando clicar no botão "Carregar Dados Orçamento" o servlet captura o valor deste campo, chama uma DAO que executa uma consulta no banco e retorna o código do cliente daquele orçamento, após isso o servlet seta no campo "Cod_cliente" do formulário o valor retornado. Acontece que preciso que o campo "Cod_cliente" que está no mesmo formulário que o cliente informou o orçamento e clicou no botão "Carregar Dados Orçamento" seja preenchido com o código do cliente que a servlet retornou. Minhas consultas já funcionam mas minha dificuldade está em fazer o campo "Cod_cliente" receber este valor.
Meu JSP:<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="venda" method="post" action="ServletPesquisaOrcamentoVenda" >
<table width="540" border="0" align="center">
<tr>
<td width="76">Cod Orcamento:</td>
<td width="538"><input name="cod_orcamento" type="text"
id="cod_orcamento" size="10" maxlength="10" /> <input name="enviar"
type="submit" id="enviar" value="Carregar Dados Orcamento"/>
</tr>
</table>
<table style="width: 100%;" >
<c:forEach items="${pesquisaVendaOrcamento}" var="pesquisa">
<tr>
<td width="76">Cod Cliente:</td>
<td width="538"><input name="cod_cliente" type="text" id="cod_cliente" size="10" maxlength="10" value="${pesquisa.Cod_cliente}"/> <!-- Preciso que o cod_cliente retornado pelo servlet seja setado aqui nesta caixa de texto-->
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
package br.bmweb.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.bmweb.pojo.Cliente;
import br.bmweb.pojo.Funcionario;
import br.bmweb.pojo.Orcamento;
import br.bmweb.pojo.Venda;
import br.bmweb.pojo.VendaOrcamento;
import br.bmweb.util.Conexao;
public class PesquisaOrcamentoVendaDao {
Connection conn = new Conexao().conecta();
private Funcionario funcionario;
public Funcionario getFuncionario() {
return funcionario;
}
public void setFuncionario(Funcionario funcionario) {
this.funcionario = funcionario;
}
public List OrcamentoVenda (int cod_orcamento) throws SQLException{
String sql = "SELECT tb_orcamento.or_data_orcamento, tb_orcamento.or_data_fechamento, tb_orcamento.or_valor_unit, tb_orcamento.or_valor_total, " +
"tb_orcamento.or_tipo_orcamento, tb_orcamento.or_status_orcamento, tb_cliente.cl_cod_cliente, tb_cliente.cl_nome, " +
"tb_funcionario.fu_cod_funcionario, tb_funcionario.fu_nome " +
"FROM tb_orcamento " +
"INNER JOIN tb_cliente " +
"ON tb_orcamento.or_cod_cliente = tb_cliente.cl_cod_cliente " +
"INNER JOIN tb_funcionario " +
"ON tb_orcamento.or_cod_funcionario = tb_funcionario.fu_cod_funcionario " +
"WHERE tb_orcamento.or_cod_orcamento = ?";
try {
PreparedStatement smt = conn.prepareStatement(sql);
List<VendaOrcamento> arrayLista = new ArrayList<VendaOrcamento>();
smt.setInt(1, cod_orcamento);
ResultSet rs = smt.executeQuery();
while (rs.next()) {
VendaOrcamento vendaorcamento = new VendaOrcamento();
vendaorcamento.setCod_cliente(Integer.parseInt(rs.getString("tb_cliente.cl_cod_cliente")));
vendaorcamento.setNome_cliente(rs.getString("tb_cliente.cl_nome"));
System.out.println("Nome do cliente do orçamento: " + vendaorcamento.getNome_cliente());
vendaorcamento.setNome_funcionario(rs.getString("tb_funcionario.fu_nome"));
System.out.println("Vendedor: " + vendaorcamento.getNome_funcionario());
vendaorcamento.setData_orcamento(rs.getDate("tb_orcamento.or_data_orcamento"));
vendaorcamento.setData_fechamento(rs.getDate("tb_orcamento.or_data_fechamento"));
vendaorcamento.setValor_unitario(rs.getDouble("tb_orcamento.or_valor_unit"));
vendaorcamento.setValor_total(rs.getDouble("tb_orcamento.or_valor_total"));
vendaorcamento.setTipo_orcamento(rs.getString("tb_orcamento.or_tipo_orcamento"));
vendaorcamento.setStatus_orcamento(rs.getString("tb_orcamento.or_status_orcamento"));
arrayLista.add(vendaorcamento);
}
return arrayLista ;
} finally {
}
}
}
package br.bmweb.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import br.bmweb.dao.PesquisaOrcamentoVendaDao;
import br.bmweb.pojo.Funcionario;
import br.bmweb.pojo.Venda;
/**
* Servlet implementation class Venda
*/
public class ServletPesquisaOrcamentoVenda extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletPesquisaOrcamentoVenda() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Valor cod_orcamento na pesquisa: " + request.getParameter("cod_orcamento"));
PrintWriter writer = response.getWriter();
HttpSession session = request.getSession(true);
PesquisaOrcamentoVendaDao pesquisa = new PesquisaOrcamentoVendaDao();
pesquisa.setFuncionario((Funcionario) (session.getAttribute("funcionario")));
List<Venda> pesquisaVendaOrcamento;
try {
pesquisaVendaOrcamento = pesquisa.OrcamentoVenda(Integer.parseInt(request.getParameter("cod_orcamento")));
if(pesquisaVendaOrcamento.isEmpty()){
writer.println("<SCRIPT language='JavaScript'> alert('Nenhum orçamento encontrado!!'); </SCRIPT>");
}else{
request.setAttribute("Venda", pesquisaVendaOrcamento);
request.getRequestDispatcher("/testeVenda.jsp").forward(request, response);
//writer.println("<SCRIPT language='JavaScript'> document.location=('Venda.jsp'); </SCRIPT>");
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Preciso que o cod_cliente retornado pela Servlet seja setado na linha 23 do meu JSP. Alguém tem alguma idéia??