Colocar 2 botones en JSP
Necesito colocar 2 botones en JSP, cada uno debe llamar otro programa JSP.
el programa que tengo en mas o menos asi
<%@page language = "java"%>
<%@ page import="java.sql.*"%>
<BODY BGCOLOR="white">
<form action ="xx.jsp" method="post">
<table width="100%"border="0"cellspacing="1"cellpadding="3">
<tr bgcolor="#FFFFFF">
<td colspan="7" ><B> INFORME RESUMIDO </B></A></FONT><BR>
<%
String usuario="";
String ccosto="";
String existe="";
int tipo=0;
int rango=0;
int agno=0;
int centro_costo=0;
%>
<tr bgcolor="#EEEEFF">
<td width="30%" ><I>Periodo Presupuestario:</b></td>
<td width="70%">
<select name="agno">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
</select>
<tr bgcolor="#EEEEFF">
<td width="30%" ><I>Tipo Presupuesto:</b></td>
<td width="70%">
<select name="tipo">
<option value=1>Gasto</option>
<option value=2>Inversión</option>
</select>
<input type=hidden name="usuario" value=<%=usuario%>>
<input type=hidden name="agno" value=<%=agno%>>
<input type=hidden name="tipo" value=<%=tipo%>>
</tr>
</table>
</body>
<BR><center>
<input type="submit" name="Submit1" value="Visualizar Excel">
<input type="submit" name="Submit2" value="Visualizar Web">
</center>
</form>
</html>
el programa que tengo en mas o menos asi
<%@page language = "java"%>
<%@ page import="java.sql.*"%>
<BODY BGCOLOR="white">
<form action ="xx.jsp" method="post">
<table width="100%"border="0"cellspacing="1"cellpadding="3">
<tr bgcolor="#FFFFFF">
<td colspan="7" ><B> INFORME RESUMIDO </B></A></FONT><BR>
<%
String usuario="";
String ccosto="";
String existe="";
int tipo=0;
int rango=0;
int agno=0;
int centro_costo=0;
%>
<tr bgcolor="#EEEEFF">
<td width="30%" ><I>Periodo Presupuestario:</b></td>
<td width="70%">
<select name="agno">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
</select>
<tr bgcolor="#EEEEFF">
<td width="30%" ><I>Tipo Presupuesto:</b></td>
<td width="70%">
<select name="tipo">
<option value=1>Gasto</option>
<option value=2>Inversión</option>
</select>
<input type=hidden name="usuario" value=<%=usuario%>>
<input type=hidden name="agno" value=<%=agno%>>
<input type=hidden name="tipo" value=<%=tipo%>>
</tr>
</table>
</body>
<BR><center>
<input type="submit" name="Submit1" value="Visualizar Excel">
<input type="submit" name="Submit2" value="Visualizar Web">
</center>
</form>
</html>
Yo lo veo más facil creando una función javascript. Esa función se activará cuando pulses uno de los botones y en ese momento se establecerá el action del formulario. Algo así:
...
<head>
<script>
function Envia(destino){
formulario.action=destino;
formulario.submit();
}
</script>
</head>
<body>
...
<form name="formulario" method="post">
...
<img src="..." onClick="Envia('uno.jsp')">
<img src="..." onClick="Envia('dos.jsp')">
...
</body>
</html>
Esto te sirve para todos los enlaces que quieras. Claro que en vez de poner un <input type="submit" pondría una imagen o un <input type="button" y andando.
...
<head>
<script>
function Envia(destino){
formulario.action=destino;
formulario.submit();
}
</script>
</head>
<body>
...
<form name="formulario" method="post">
...
<img src="..." onClick="Envia('uno.jsp')">
<img src="..." onClick="Envia('dos.jsp')">
...
</body>
</html>
Esto te sirve para todos los enlaces que quieras. Claro que en vez de poner un <input type="submit" pondría una imagen o un <input type="button" y andando.
Bueno, puedes hacer que el form vaya a un Servlet, en el que preguntas qué botón se ha pulsado (mediante request.getParameter()) y redirigir la petición a un .jsp distinto según sea el botón que se haya pulsado.
Conste que esto también se puede hacer desde un JSP, pero a mí me parece más propio hacerlo con un Servlet directamente.
Salu2
Ken
Conste que esto también se puede hacer desde un JSP, pero a mí me parece más propio hacerlo con un Servlet directamente.
Salu2
Ken
Hola Ken, te molesto, podrías por favor colocar un ejemplo para determinar cual es el botón presionado tal como tu indicas en tu solución? Por favor necesito urgente saer como se hace.
Coloco ejemplo:
index.jsp
JSP Page
Hello World!
BotonPresionado.jsp ( parte pertinente del servlet)
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("");
out.println("");
out.println("Servlet BotonPresionado");
out.println("");
out.println("");
//http://www.forosdelweb.com/f45/varios-botones-form-841526/
String insertar = request.getParameter("B1");//Toma El Parametro Del Boton Insertar
String eliminar = request.getParameter("B2");//Toma El Parametro Del Boton Eliminar
String modificar = request.getParameter("B3");//Toma El Parametro Del Boton Modificar
String valor = "nada";
if ("Insertar".equals(request.getParameter("B1"))) {
// do Insertar
//("Insertar".equals(null)) == { False }
valor = "Insertar";
} else if ("Eliminar".equals(request.getParameter("B2"))) {
// do Eliminar
valor = "Eliminar";
} else if ("Modificar".equals(request.getParameter("B3"))) {
// do Modificar
valor = "Modificar";
} else {
// do No usó los botones
valor = "No entro a ninguno";
}
out.println("Servlet BotonPresionado at " + request.getContextPath() + "");
out.println("El boton presionado fue : " + valor + "");
} catch (Exception e) {
System.out.println("Se ha producido un error");
out.println("Se ha producido un error: " + e + "");
} finally {
out.println("");
out.println("");
out.close();
}
}
index.jsp
JSP Page
Hello World!
BotonPresionado.jsp ( parte pertinente del servlet)
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("");
out.println("");
out.println("Servlet BotonPresionado");
out.println("");
out.println("");
//http://www.forosdelweb.com/f45/varios-botones-form-841526/
String insertar = request.getParameter("B1");//Toma El Parametro Del Boton Insertar
String eliminar = request.getParameter("B2");//Toma El Parametro Del Boton Eliminar
String modificar = request.getParameter("B3");//Toma El Parametro Del Boton Modificar
String valor = "nada";
if ("Insertar".equals(request.getParameter("B1"))) {
// do Insertar
//("Insertar".equals(null)) == { False }
valor = "Insertar";
} else if ("Eliminar".equals(request.getParameter("B2"))) {
// do Eliminar
valor = "Eliminar";
} else if ("Modificar".equals(request.getParameter("B3"))) {
// do Modificar
valor = "Modificar";
} else {
// do No usó los botones
valor = "No entro a ninguno";
}
out.println("Servlet BotonPresionado at " + request.getContextPath() + "");
out.println("El boton presionado fue : " + valor + "");
} catch (Exception e) {
System.out.println("Se ha producido un error");
out.println("Se ha producido un error: " + e + "");
} finally {
out.println("");
out.println("");
out.close();
}
}
indx.jsp: