No es un bug, es una característica no documentada

domingo, 22 de noviembre de 2015

Acceso a datos. Aplicaciones web servidor. JSP. Ejemplos

20:48 Posted by Inazio , No comments

Hola mundo

<%@ 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>
             <% out.println("Hola Mundo"); %>
       </body>
</html>

Comentarios

<%@ page language="java" %>
<html>
  <head>
    <title>Ejemplo de comentarios ocultos</title>
  </head>
  <body>
    <h2>Ejemplo de comentarios ocultos</h2>
    <%-- Este comentario no se incluye en el response --%>
    <!--  Fecha de  compilacion: <%= new java.util.Date()%> -->
  </body>
</html>

Consultas de páginas

<%@ 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>
             <%! private int accessCount = 0; %>
             Número de veces que se consultó a la página:
             <%= ++accessCount %>
       </body>
</html>

BgColor

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
       <HEAD>
         <TITLE>Color de Fondo</TITLE>
       </HEAD>
      
       <%
       String bgColor = request.getParameter("bgColor");
       boolean hasExplicitColor;
       if (bgColor != null) {
         hasExplicitColor = true;
       } else {
         hasExplicitColor = false;
         bgColor = "WHITE";
       }
       %>
       <BODY BGCOLOR="<%= bgColor %>">
             <H2 ALIGN="CENTER">Color de Fondo</H2>
       </BODY>
</HTML>

Scriptlet

<%@ 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>
             <title> Página de ejemplo de scriptlet</title>
       </head>
       <body>
             <h1> Página de ejemplo de scriptlet </h1>
             <%
                    for (int i=0; i<10; i++){
                           out.println("<b> Hola a todos. Esto es un ejemplo de scriptlet " + i + "</b>");
                           System.out.println("Esto va al stream System.out" + i );
                           //Esto último va a la consola del Java, no al cliente.
                           //out a secas es para la respuesta al cliente.
                    }
             %>
       </body>
</HTML>

Combinando HTML y Java

<%@ 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>
             <% if (Math.random() < 0.5) { %>
             Tiene un <B>día</B> feliz!
             <% } else { %>
             Tiene un <B>dia</B> aburrido!
             <% } %>
       </body>
</html>

Expresiones

<%@ 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>
             <title> Página de ejemplo de expresiones</title>
       </head>
       <body>
             <h1> Página de ejemplo de expresiones </h1>
             Hola a todos, son las <%= new java.util.Date().toString()%>
             Hoy estamos a <%=(new java.util.Date())%>
       </body>
</HTML>

Expresiones II

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
       <HEAD>
             <TITLE>Ejemplo1: Expresiones</TITLE>
       </HEAD>
      
       <BODY>
             <H2>Ejemplos de expresiones JSP</H2>
             <UL>
                    <LI>Hora actual en el servidor: <%= new java.util.Date() %></LI>
                    <LI>Nombre del host: <%= request.getRemoteHost() %></LI>
                    <LI>Identificador de sesion: <%= session.getId() %></LI>
                    <LI>Valor del parametro testParam: <%= request.getParameter("testParam") %></LI>
             </UL>
       </BODY>
</HTML>

Parámetros

<%@ 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> entrega el nombre como parámetro, de lo contrario lo pide y saluda</title>
       </head>
       <body>
             <%
                    String nombre = request.getParameter("nombrePila");
                    if (nombre != null) {
             %>
             <h1>Hola <%= nombre %>, <br/>¡ Bienvenido a la página !</h1>
             <%
                    }
                    else
                    {
             %>
             <form action="ejemplo05.jsp" method = "get">
                    <p>Escriba su nombre y pulse Enviar </p>
                    <p><input type = "text" name = "nombrePila" />
                    <input type = "submit" value = "Enviar" />
                    </p>
             </form>
             <%
                    }
             %>
       </body>
</html>

Parámetros en otro JSP

Archivo ejemplo06a.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
       <head>
             <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
             <title>Ejercicios saludos</title>
       </head>
       <body>
             <p>Nos devuelve un saludo con el nombre que le pasemos en el formulario</p>
             <form action="ejemplo06b.jsp" method="post">
                    <input type="text" name="nombre">
                    <input type="submit" value ="enviar">
             </form>
       </body>
</html>

Archivo ejemplo06b.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>
             <%
                    if ( request.getParameter("nombre") != null && request.getParameter("nombre") !="" ){
             %>
             Hola
             <%= request.getParameter("nombre") %>
             <%
                    }else{
             %>
             Hola Desconocido
             <%
                    }
             %><br/>
             <a href="ejemplo06a.jsp">Volver</a>
       </body>
</html>

Control de errores

Archivo velocidad.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
       <HEAD>
             <TITLE>Ejemplo 3: Directiva page</TITLE>
       </HEAD>
       <BODY>
             <%@ page errorPage="VelocidadError.jsp" %>
             <%!
                    // Observa que se lanzará un  excepcion NumberFormatException si el valor es nulo o
                    // esta mal formateado
                    private double toDouble(String value) {
                           return(Double.valueOf(value).doubleValue());
                    }
             %>
             <%
                    double espacio = toDouble(request.getParameter("espacio"));
                    double tiempo = toDouble(request.getParameter("tiempo"));
                    double speed = espacio/tiempo;
             %>
             <UL>
                    <LI>Espacio: <%= espacio %>.</LI>
                    <LI>Tiempo: <%= tiempo %>.</LI>
                    <LI>Velocidad: <%= speed %>.</LI>
             </UL>
       </BODY>
</HTML>

Archivo VelocidadError.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
       <HEAD>
             <TITLE>Página de error</TITLE>
       </HEAD>
      
       <BODY>
             <%@ page isErrorPage="true" %>
             <TABLE BORDER=5 ALIGN="CENTER">
                    <TR>
                           <TH CLASS="TITLE">Error al calcular la velocidad</TH>
                    </TR>
             </TABLE>
             <P>
                    Hay un error en la página Velocidad.jsp:
                    <I><%= exception %></I>.
                    <PRE><% exception.printStackTrace(new java.io.PrintWriter(out)); %></PRE>
       </P>
       </BODY>

</HTML>

0 comentarios:

Publicar un comentario