Skip to content

Instantly share code, notes, and snippets.

@jrbalsas
Last active March 1, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrbalsas/f482bae636add3b785dcb8d4d4063145 to your computer and use it in GitHub Desktop.
Save jrbalsas/f482bae636add3b785dcb8d4d4063145 to your computer and use it in GitHub Desktop.
DAW. Práctica 4. Ejemplos de uso de JSP
<%--
Ejemplo de formulario con validación simple en JSP utilizando scriptlets
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%=(request.getParameter("nombre") != null ? request.getParameter("nombre") : "")%>
<% String nombre = request.getParameter("nombre");
if (nombre == null) {
nombre = "";
}
%>
<%if (nombre != "") { //Mostramos nombre de usuario%>
<h1>Hola <%=nombre%></h1>
<%} else { // Mostramos formulario%>
<h1>Dime tu nombre</h1>
<form method="POST">
<input type="text" name="nombre" value="<%=nombre%>" placeholder="Dime tu nombre" >
<input type="submit" name="Enviar" value="Guarda nombre">
</form>
<%};%>
</body>
</html>
<%--
Ejemplo de formulario con validación simple y JSTL/Expression Language
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%-- Mostramos resultado --%>
<c:if test="${not empty param.nombre and fn:length(param.nombre)<=10}">
<h1>Hola ${param.nombre}</h1>
</c:if>
<%-- Mostramos formulario --%>
<c:if test="${empty param.nombre or fn:length(param.nombre)>10}">
<h1>Dime tu nombre</h1>
<form method="POST">
<input type="text" name="nombre" value="${param["nombre"]}" placeholder="Dime tu nombre" >
${fn:length(param.nombre)>10?"La longitud no puede ser mayor que 10":""}
<input type="submit" name="Enviar" value="Guarda nombre">
</form>
</c:if>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment