JSP. 8 Ejemplo recojer datos
Página formularios.jsp,
recoge los datos del usuario y los manda a request.jsp:
<%@page
contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE
html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Curso</title>
</head>
<body>
<h1>Formularios</h1>
<form action="index.jsp" method="POST">
<input type="submit" value="Volver a Menu" />
</form><br/>
<form name="form1" action="request.jsp" method="POST">
txt:
<input type="text" name="txt1" maxlength="10" size="15" />
<br/>
txt:
<input type="text" name="txt2" maxlength="10" size="20" value="Hola" />
<br/>
radio:
Si <input type="radio" name="radio1" value="Si" />
No <input type="radio" name="radio1" value="No" />
<br/>
checkbox:
Rojo: <input type="checkbox" name="ck1" value="rojo" />
Verde: <input type="checkbox" name="ck1" value="verde" />
Azul: <input type="checkbox" name="ck1" checked value="azul" />
<br/>
Select:
<select name="select1" multiple>
<option value="1">Opción 1</option>
<option value="2">Opción 2</option>
<option value="3" selected>Opción 3</option>
</select><br/>
textarea:
<textarea name="textarea1" rows="4" cols="20">
Linea de texto
otra linea de texto
</textarea>
<br/>
<input value="Enviar" type="submit" /><!-- Enviar formulario -->
<input type="reset" /><!-- Limpiar campos del formulario -->
</form>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Curso</title>
</head>
<body>
<h1>Formularios</h1>
<form action="index.jsp" method="POST">
<input type="submit" value="Volver a Menu" />
</form><br/>
<form name="form1" action="request.jsp" method="POST">
txt:
<input type="text" name="txt1" maxlength="10" size="15" />
<br/>
txt:
<input type="text" name="txt2" maxlength="10" size="20" value="Hola" />
<br/>
radio:
Si <input type="radio" name="radio1" value="Si" />
No <input type="radio" name="radio1" value="No" />
<br/>
checkbox:
Rojo: <input type="checkbox" name="ck1" value="rojo" />
Verde: <input type="checkbox" name="ck1" value="verde" />
Azul: <input type="checkbox" name="ck1" checked value="azul" />
<br/>
Select:
<select name="select1" multiple>
<option value="1">Opción 1</option>
<option value="2">Opción 2</option>
<option value="3" selected>Opción 3</option>
</select><br/>
textarea:
<textarea name="textarea1" rows="4" cols="20">
Linea de texto
otra linea de texto
</textarea>
<br/>
<input value="Enviar" type="submit" /><!-- Enviar formulario -->
<input type="reset" /><!-- Limpiar campos del formulario -->
</form>
</body>
</html>
Página request.jsp,
recoge y muestra los datos enviados por formularios.jsp:
<%@page
contentType="text/html"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Curso</title>
</head>
<body>
<h1>Utilización de request</h1>
<form action="formularios.jsp" method="POST">
<input type="submit" value="Volver a formularios" />
</form>
<%
//las variables que nos llegan son: txt1, txt2, radio1, ck1, select1, textarea1
String txt1 = request.getParameter("txt1");
String txt2 = request.getParameter("txt2");
String radio = request.getParameter("radio1");
String[] ck1 = request.getParameterValues("ck1");
String[] miselect = request.getParameterValues("select1");
String txtarea = request.getParameter("textarea1");
out.println("txt1: "+txt1+"<br />");
out.println("txt2: "+txt2+"<br />");
out.println("radio1: "+radio+"<br />");
out.println("ck1:");
for(int i=0; i<ck1.length; i++){
out.println("<pre> "+ck1[i]+"</pre>");
}
out.println("select: ");
for(int i=0; i<miselect.length; i++){
out.println("<pre> "+miselect[i]+"</pre>");
}
out.println("textarea1: "+txtarea+"<br />");
%>
TXT1: <% out.println(request.getParameter("txt2")); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Curso</title>
</head>
<body>
<h1>Utilización de request</h1>
<form action="formularios.jsp" method="POST">
<input type="submit" value="Volver a formularios" />
</form>
<%
//las variables que nos llegan son: txt1, txt2, radio1, ck1, select1, textarea1
String txt1 = request.getParameter("txt1");
String txt2 = request.getParameter("txt2");
String radio = request.getParameter("radio1");
String[] ck1 = request.getParameterValues("ck1");
String[] miselect = request.getParameterValues("select1");
String txtarea = request.getParameter("textarea1");
out.println("txt1: "+txt1+"<br />");
out.println("txt2: "+txt2+"<br />");
out.println("radio1: "+radio+"<br />");
out.println("ck1:");
for(int i=0; i<ck1.length; i++){
out.println("<pre> "+ck1[i]+"</pre>");
}
out.println("select: ");
for(int i=0; i<miselect.length; i++){
out.println("<pre> "+miselect[i]+"</pre>");
}
out.println("textarea1: "+txtarea+"<br />");
%>
TXT1: <% out.println(request.getParameter("txt2")); %>
</body>
</html>
Comentarios
Publicar un comentario