Está en la página 1de 16

JSP. Sintxis bsica. Formularios y tablas. Introduccin a Servlets. Deployment.

Servidor Web Tomcat

Java Server Pages Es una tecnologa Java que permite generar contenido dinmico para web Motor JSP lleva a cabo una fase de traduccin de esa pgina en un servlet

HTML
Form Tablas

CSS JavaScript

<%@ page errorPage="myerror.jsp" %> <%@ page import="com.foo.bar" %> <html> <head> <%! int serverInstanceVariable = 1;%> ... <% int localStackBasedVariable = 1; %>

pageContext: javax.servlet.jsp.PageContext request: javax.servlet.http.HttpServletRequest response: javax.servlet.http.HttpServletResponse session: javax.servlet.http.HttpSession config: javax.servlet.ServletConfig application: javax.servlet.ServletContext out: javax.servlet.jsp.JspWriter page: java.lang.Object exception: java.lang.Exception

<%@ directiva atributo="valor" %> Las directivas disponibles son:


include: Incluye el contenido de un fichero en la pgina mediante el atributo file. taglib: Importa bibliotecas de etiquetas (Tag Libraries)
<%@ include file="cabecera.html" %>

page: Especifica atributos relacionados con la pgina a procesar.

<%@ taglib uri="/tags/struts-html" prefix="html" %>

Atributo import

Sintaxis <%@ page import="class; class" %> <%@ page session="false" %> <%@ page contentType="class; class" %> <%@ page buffer="12KB" %>

Utilizacin Importa clases y paquetes Java para ser utilizadas dentro del fichero JSP. Especifica si utiliza los datos contenidos en sesin; por defecto "true". Especifica el tipo MIME del objeto "response"; por defecto "text/html; charset=ISO-88591". Buffer utilizado por el objeto writer "out"; puede tomar el valor de "none"; por defecto "8KB".

session

contentType

buffer

Atributo

Sintaxis
<%@ page errorPage="/path_to_err or_page" %>

Utilizacin
Especifica la ruta de la pgina de error que ser invocada en caso de producirse una excepcin durante la ejecucin de este fichero JSP. Determina si este fichero JSP es una pgina que maneja excepciones. nicamente a este tipo de pginas pueden acceder a la variable implcita "exception", que contiene la excepcin que provoc la llamada a la pgina de error.

errorPage

isErrorPage

<%@ page isErrorPage="true" %>

<% String titulo = ""; if (request.getAttribute("titulo") != null) { titulo = (String) request.getAttribute ("titulo"); } %> ... <title><%=titulo%></title>

Tomcat es un Servlet Container Corre sobre apache (web server) Es open source Cumple las especificaciones de jsp

HTML form <html> <title>Coffee Advisor></title> <body> <h1 align="center">Coffee Advisor </h1> <form method="POST" action="SelectCoffee.do"> Select coffee Type: <select name="type" size=1"> <option value="milky">Milky</option> <option value="froffy">Froffy</option> <option value="icey">Icey</option> <option value="strong">Spaced Out</option> </select> <br><br> <center> <input type="Submit"> </center> </form> </body> <html>

Deployment Descriptor (WEB-INF/web.xml)


<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/webapp_2_4.xsd" Version="2.4"> <servlet> <servlet-name>Coffee</servlet-name> <servlet-class>com.example.web.CoffeeSelect</servlet-class> </servlet> <servlet-mapping> <servlet-name>Coffee</servlet-name> <url-pattern>/SelectCoffee.do</url-pattern> </servlet-mapping> </web-app>

Controller

package com.example.web;

import com.example.model.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*;
public class CoffeeSelect extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String c = request.getParameter("type"); // Now use our Coffee Model above CoffeeExpert ce = new CoffeeExpert(); List result = ce.getTypes(c); // The results will be passed back (as an attribute) to the JSP view // The attribute will be a name/value pair, the value in this case will be a List object request.setAttribute("styles", result); RequestDispatcher view = request.getRequestDispatcher("result.jsp"); view.forward(request, response); } }

Experto

package com.example.model; import java.util.*; public class CoffeeExpert { public List getTypes(String type) { List types = new ArrayList(); if (type.equals("milky")) { types.add("latte"); types.add("cappuccino"); } else if (type.equals("froffy")) { types.add("latte"); types.add("cappuccino"); types.add("frappuccino"); } else if (type.equals("icey")) { types.add("frappuccino"); } else if (type.equals("strong")) { types.add("espresso"); types.add("double espresso"); } else { types.add("Vending Machine"); } return (types); }}

result.jsp

<%@ page import="java.util.*" %> <html> <body> <h1 align="center">Coffee Recommandation JSP View</h1> <p> <%

%> </body> </html>

List styles = (List) request.getAttribute("styles"); Iterator it = styles.iterator(); while (it.hasNext()) { out.print("<br>try: " + it.next()); }

Gracias por venir

También podría gustarte