WEB APP (Servlets y JSP) EJB (war y jar) y JPA con JAX-WS (Parte 5)
Objetivo: Agregar funcionalidad al EJB creado en las partes anteriores para mostrar una vista WEB usando servlets y JSP's.
Estructura del proyecto:
Al final deberiamos ver:
1.- Crear un maven project pero ahora como web application.
2.- Diagrama de clases:
3.- Copiamos el source package de sga-jee4 a sources de sga-jee5-web y hacemos los mismo con la carpeta other sources que es donde se encuentra el archivo de persistencia.
4.- Agregamos al nuevo pom las dependencias del proyecto anterior.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mx.com.gm</groupId>
<artifactId>sga-jee5-web</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>sga-jee5-web</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
6.- listarPersonas.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Listado Personas</title>
</head>
<body>
<h1>Listado de Personas</h1>
<table border="1">
<tr>
<th>Nombre</th>
<th>Apellido Paterno</th>
<th>Email</th>
</tr>
<c:forEach var="persona" items="${personas}">
<tr>
<td>${persona.nombre}</td>
<td>${persona.apePaterno}</td>
<td>${persona.email}</td>
</tr>
</c:forEach>
</table>
<br>
<a href="index.jsp">Regresar al Inicio</a>
</body>
</html>
7.- creamos el controlador. ServletControlador.
package mx.com.gm.sga.web;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mx.com.gm.sga.domain.Persona;
import mx.com.gm.sga.servicio.PersonaService;
@WebServlet("/ListarPersonas")
public class ServletControlador extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
private PersonaService personaService;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Persona> personas = personaService.listarPersonas();
request.setAttribute("personas", personas);
request.getRequestDispatcher("listarPersonas.jsp").forward(request, response);
}
}
8.- Eliminamos archivos de versiones anteriores del servidor y lo ejecutamos.
Codigo Fuente
Con esto hemos creado una aplicacion web la cual tiene embebida un EJB el cual contiene conexiones a JPA, EJB, JAX-WS, JSP, y servlet.
Estructura del proyecto:
Al final deberiamos ver:
1.- Crear un maven project pero ahora como web application.
2.- Diagrama de clases:
3.- Copiamos el source package de sga-jee4 a sources de sga-jee5-web y hacemos los mismo con la carpeta other sources que es donde se encuentra el archivo de persistencia.
4.- Agregamos al nuevo pom las dependencias del proyecto anterior.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mx.com.gm</groupId>
<artifactId>sga-jee5-web</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<name>sga-jee5-web</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5.- creamos el archivo index.jsp
<!DOCTYPE html>
<html>
<body>
<h2>Sistema de Gestión de Alumnos (SGA)</h2>
<a href="ListarPersonas">Listado de Personas</a>
</body>
</html>
6.- listarPersonas.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Listado Personas</title>
</head>
<body>
<h1>Listado de Personas</h1>
<table border="1">
<tr>
<th>Nombre</th>
<th>Apellido Paterno</th>
<th>Email</th>
</tr>
<c:forEach var="persona" items="${personas}">
<tr>
<td>${persona.nombre}</td>
<td>${persona.apePaterno}</td>
<td>${persona.email}</td>
</tr>
</c:forEach>
</table>
<br>
<a href="index.jsp">Regresar al Inicio</a>
</body>
</html>
7.- creamos el controlador. ServletControlador.
package mx.com.gm.sga.web;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mx.com.gm.sga.domain.Persona;
import mx.com.gm.sga.servicio.PersonaService;
@WebServlet("/ListarPersonas")
public class ServletControlador extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
private PersonaService personaService;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Persona> personas = personaService.listarPersonas();
request.setAttribute("personas", personas);
request.getRequestDispatcher("listarPersonas.jsp").forward(request, response);
}
}
8.- Eliminamos archivos de versiones anteriores del servidor y lo ejecutamos.
Codigo Fuente
Con esto hemos creado una aplicacion web la cual tiene embebida un EJB el cual contiene conexiones a JPA, EJB, JAX-WS, JSP, y servlet.
No hay comentarios.