Galera,
Estou desenvolvendo uma aplicação web e sou novato no assunto. Estou com um problema no seguinte trecho do codigo, onde o metodo doPost está sendo executado duas vezes clicando uma unica vez em qualquer botao do formulario JSP.
Minha implementação não está essas coisas, pois copiei de outra pessoa e estou adaptando ....
Valeu!
Código do JSP
<form style="margin-top: 50px; margin-left: 240px;" method="post"
action="EmprestarController" name="emprestar"><input type="hidden"
name="operacao" value="consultar">
<table width="300" border="0" cellspacing="0" cellpadding="0"
style="width: 522px">
<tr>
<td width="80"><span class="style6">RA / RE</span></td>
<td width="144"><label> <input type="text" name="RARE"
id="RARE"> </label></td>
<td width="20"> </td>
<td><input type="image" src="Imagens/botaoValidar.png"
name="validar" value="validar" alt="Validar"
onclick="executar(this.form,'validar');" /></td>
</tr>
<tr>
<td><span class="style6">Tombo</span></td>
<td><label> <input type="text" name="tombo"
id="tombo"> </label></td>
</tr>
</table>
<table style="margin-left: 10px; margin-top: 30px;" width="400"
border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="120"><input type="image"
src="Imagens/botaoConsultar.png" name="consultar" value="consultar"
alt="Consultar" onclick="executar(this.operacao,'consultar')" /></td>
<td width="120"><input type="image"
src="Imagens/botaoEmprestar.png" name="emprestar" value="emprestar"
alt="Emprestar" onclick="executar(this.form,'emprestar')" /></td>
<td width="107"><a href="Principal.jsp"><img border="0"
src="Imagens/botaoVoltar.png" alt="Voltar" width="120" height="30"></a></td>
</tr>
</table>
<table style="margin-top: 20px; margin-left: 5px" width="713" border="1" id="table1">
<tr>
<td width="53"> </td>
<td width="171" align="center"><span class="style6">Tombo</span></td>
<td width="317" align="center"><span class="style6">Título</span></td>
<td width="150" align="center"><span class="style6">Qtd Disponível</span></td>
<td width="134" align="center"><span class="style6">TIPO</span></td>
</tr>
<%
if((List)request.getSession().getAttribute("listaObras") != null){
List retorno = (List)request.getSession().getAttribute("listaObras");
for (Iterator<Obra> it = retorno.iterator(); it.hasNext();) {
Obra c = it.next();
%>
<tr>
<td width="10px" class="tdDados"><input type="radio" name="item"
value="<%=c.getId()%>"></td>
<td width="40px" class="tdDados"><%=c.getTombo()%></td>
<td width="40px" class="tdDados"><%=c.getTitulo()%></td>
<td width="40px" class="tdDados"><%=c.getQuantidadeDisponivel()%></td>
<td width="200px" class="tdDados"><%=(c instanceof Livro)?"LIVRO":"FILME"%></td>
</tr>
<%
}
}
%>
</table>
</form>
Código do Servlet
public class EmprestarController extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
executar(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
executar(request, response);
}
private void executar(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
List<Obra> retorno = null;
LoginTO loginTO = new LoginTO();
if ("consultar".equals(request.getParameter("operacao"))) {
Obra filtro = new Obra();
filtro.setTitulo(request.getParameter("titulo"));
retorno = filtro.consultarObras(request.getParameter("tombo"),
request.getParameter("autor"));
request.getSession().setAttribute("listaObras", retorno);
request.getRequestDispatcher("Emprestar.jsp").forward(request,
response);
} else if ("validar".equals(request.getParameter("operacao"))) {
LoginDAO login = new LoginDAO();
loginTO = login.validar(request.getParameter("RARE"));
if (loginTO != null) {
request.getSession().setAttribute("login", loginTO);
if (request.getSession().getAttribute("listaObras") == null) {
retorno = new ArrayList<Obra>();
request.getRequestDispatcher("Emprestar.jsp").forward(
request, response);
} else {
Obra filtro = new Obra();
filtro.setTitulo(request.getParameter("titulo"));
retorno = filtro.consultarObras(request
.getParameter("tombo"), request
.getParameter("autor"));
request.getSession().setAttribute("listaObras", retorno);
request.getRequestDispatcher("Emprestar.jsp").forward(
request, response);
}
} else {
request.getRequestDispatcher("Erro/EmprestarErro.jsp").forward(
request, response);
}
} else if ("emprestar".equals(request.getParameter("operacao"))) {
if (request.getSession().getAttribute("login") != null) {
if (selecionarObra(request, response) != null) {
loginTO = (LoginTO) request.getSession().getAttribute(
"login");
EmprestimoDAO emprestar = new EmprestimoDAO();
String idObra = request.getParameter("item");
int id = Integer.parseInt(idObra);
emprestar.emprestar(loginTO.getIdLogin(),
loginTO.getTipo(), id);
String comprovante = emprestar.comprovanteEmprestimo(
idObra, loginTO.getTipo());
request.getSession().setAttribute("comprovanteEmprestimo",
comprovante);
request.getRequestDispatcher("comprovanteEmprestimo.jsp")
.forward(request, response);
} else {
request.getRequestDispatcher("validacaoEmprestarObra.jsp")
.forward(request, response);
}
} else {
Obra filtro = new Obra();
filtro.setTitulo(request.getParameter("titulo"));
retorno = filtro.consultarObras(request.getParameter("tombo"),
request.getParameter("autor"));
request.getSession().setAttribute("listaObras", retorno);
request.getRequestDispatcher("validacaoEmprestarUsuario.jsp")
.forward(request, response);
}
} else if ("voltar".equals(request.getParameter("operacao1"))) {
request.getRequestDispatcher("Emprestar.jsp").forward(request,
response);
request.getSession().invalidate();
} else {
}
retorno = new ArrayList<Obra>();
}
}
}
