Está en la página 1de 17

Desarrollo de Aplicaciones Web

LABORATORIO Nº 2

Java Server Pages (JSP)

Alumno(s): MAMANI.MAMANI.renzo.junior Nota

Grupo: A Ciclo: VI

Requiere No
Excelente Bueno Puntaje
Criterio de Evaluación mejora acept.
(4pts) (3pts) Logrado
(2pts) (0pts)

Comprende el uso de arreglos y matrices en JSP.

Crea objetos en JSP.

Trabaja con variables, arreglos/ listas y


matrices.

Implementa el formulario propuesto en JSP.

Es puntual y redacta el informe adecuadamente


Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 2/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

TEMA: JAVA SERVER PAGES


OBJETIVOS
● Trabajar variables, arreglos y matrices.
● Creación de un formulario en JSP.

REQUERIMIENTOS
● Tener el Netbeans, Java SDK y el Apache Tomcat instalados.
● Diapositivas de la semana.

PROCEDIMIENTO
(** El laboratorio se ha diseñado para ser desarrollado en grupos de 2 o 3 personas**)

1. Explique con sus propias palabras, ¿qué es un JSP?

2. Cree un proyecto web y pruebe el siguiente código. No se olvide de configurar el archivo


pom.xml de Maven.

En el archivo Project Files>pom.xml

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 3/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

○ ¿Qué hace el scriptlet en el JPS?


Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 4/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<html>
<head>
<title>Arreglo en JSP</title>
</head>
<body>
<%
// Declare and initialize an array of integers
int[] numeros = {1, 2, 3, 4, 5};
%>

<h1>Arreglo en JSP</h1>
<p>Elementos de un arreglo:</p>
<ul>
<%
// Iterate over the array and print each element
for (int i = 0; i < numeros.length; i++) {
%>
<li><%= numeros[i] %></li>
<%
}
%>
</ul>
</body>
</html>

Este es un arreglo el cual el código anterior lo ejecuta para luego en el localhost dar la respuesta al
mismo y escribiendo los dos textos del código
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 5/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

3. Cree un proyecto web y pruebe el siguiente código ¿Qué hace el código?

<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<html>
<head>
<title>Matriz en JSP</title>
</head>
<body>
<%
// Define a class for the matrix
class Matrix {
private int[][] data;

// Constructor
public Matrix(int rows, int cols) {
data = new int[rows][cols];
}

// Set value at specified position


public void set(int row, int col, int value) {
data[row][col] = value;
}

// Get value at specified position


public int get(int row, int col) {
return data[row][col];
}
}

// Create a matrix object


Matrix matrix = new Matrix(3, 3);

// Populate the matrix


matrix.set(0, 0, 1);
matrix.set(0, 1, 2);
matrix.set(0, 2, 3);
matrix.set(1, 0, 4);
matrix.set(1, 1, 5);
matrix.set(1, 2, 6);
matrix.set(2, 0, 7);
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 6/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

matrix.set(2, 1, 8);
matrix.set(2, 2, 9);
%>

<h1>Matriz en JSP</h1>
<table border="1">
<%
// Iterate over the matrix and print each element
for (int i = 0; i < 3; i++) {
%>
<tr>
<%
for (int j = 0; j < 3; j++) {
%>
<td><%= matrix.get(i, j) %></td>
<%
}
%>
</tr>
<%
}
%>
</table>
</body>
</html>

En este captura se puede apreciar la salida o respuesta del código anterior ya que la misma ejecuta
una matriz enumerándola del 1 al 9 y también haciendo el respectivo grafico para dentro de el poner
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 7/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

los números en orden en un cuadro de 3 x 3

4. Cree un proyecto web y pruebe el siguiente código ¿Qué hace el código? ¿Por qué el resultado
original array es igual resulting array?

<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<html>
<head>
<title>Array in JSP</title>
</head>
<body>
<%
// Define an array of integers
int[] numbers = {5, 8, 12, 4, 7};

// Define a variable with a value


int variableValue = 3;

for (int i = 0; i < numbers.length; i++) {


numbers[i] -= variableValue;
}
%>

<h1>Array in JSP</h1>
<p>Original Array: <%= java.util.Arrays.toString(numbers) %></p>
<p>Variable Value: <%= variableValue %></p>
<p>Resulting Array: <%= java.util.Arrays.toString(numbers) %></p>
</body>
</html>
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 8/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

En este array lo que hace es con los valores anteriores que son 5, 8, 12, 4, 7 les resta menos 3 para
luego ponerlos en un nuevo array y botar la respuesta que se ve en la captura
Con respecto ala pregunta ¿Por qué el resultado original array es igual resulting array? Es por que se
evaluan 2 expresiones los cuales serian el array que bota que es original array que es la respuesta
original y el resulting array que es para mostrar el mismo array solo que después de la operación de
la resta

5. Cree un proyecto web y pruebe el siguiente código ¿Qué hace el código?

<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<html>
<head>
<title>Array in JSP</title>
</head>
<body>
<%
// Define an array of integers
int[] numbers = {1, 2, 3, 4, 5};

// Define a variable with a value


int variableValue = 3;

// Create a list to store the extracted elements


java.util.ArrayList<Integer> extractedList = new java.util.ArrayList<>();

// Extract elements from the array based on the variable value


for (int i = 0; i < numbers.length; i++) {
if (numbers[i] > variableValue) {
extractedList.add(numbers[i]);
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 9/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

}
}

// Convert the ArrayList to an array


Integer[] extractedArray = extractedList.toArray(new Integer[extractedList.size()]);
%>

<h1>Array in JSP</h1>
<p>Original Array: <%= java.util.Arrays.toString(numbers) %></p>
<p>Variable Value: <%= variableValue %></p>
<p>Extracted Array: <%= java.util.Arrays.toString(extractedArray) %></p>
</body>
</html>

En este array se puede ver como la respuesta del mismo es 1,2,3,4,5 para luego quitar lo valores
menores o iguales a 3 y botar la respuesta final que seria 4,5

6. Cree un proyecto web y pruebe el siguiente código ¿Qué hace el código?

<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%@ page import="java.util.ArrayList" %>

<%!
// Define the Student class
public class Student {
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 10/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

private String name;


private String lastName;
private int value;

// Constructor
public Student(String name, String lastName, int value) {
this.name = name;
this.lastName = lastName;
this.value = value;
}

// Getters and setters


public String getName() {
return name;
}

public void setName(String name) {


this.name = name;
}

public String getLastName() {


return lastName;
}

public void setLastName(String lastName) {


this.lastName = lastName;
}

public int getValue() {


return value;
}

public void setValue(int value) {


this.value = value;
}
}
%>

<html>
<head>
<title>Student Array in JSP</title>
</head>
<body>
<%
// Create an ArrayList to store students
ArrayList<Student> students = new ArrayList<>();
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 11/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

// Create instances of Student and add them to the ArrayList


students.add(new Student("John", "Doe", 90));
students.add(new Student("Jane", "Smith", 85));
students.add(new Student("Alice", "Johnson", 95));
%>

<h1>Students</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Value</th>
</tr>
<%
// Iterate over the ArrayList and display student information
for (Student student : students) {
%>
<tr>
<td><%= student.getName() %></td>
<td><%= student.getLastName() %></td>
<td><%= student.getValue() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 12/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

En este código se ejecuta una matriz que esta encargada de ordenar lo siguiente la sección nombres
con los respectivos nombres, la sección del segundo nombre también con lo mismo, así como los
valores también con sus respectivos números y todo y todo ordenado en un cuadro 3 x 4

7. Cree un proyecto web y pruebe el siguiente código ¿Qué hace el código?

<%@ page language="java" contentType="text/html; charset=UTF-8" %>


<%@ page import="java.util.Arrays" %>

<%!
// Define the Student class
class Student {
private String name;
private String lastname;
private int value;

// Constructor
public Student(String name, String lastname, int value) {
this.name = name;
this.lastname = lastname;
this.value = value;
}

// Getters
public String getName() {
return name;
}

public String getLastname() {


return lastname;
}

public int getValue() {


return value;
}
}
%>
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 13/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

<%
// Create an array to store Student objects (matrix)
Student[][] students = new Student[2][3];

// Populate the matrix with Student objects


students[0][0] = new Student("John", "Doe", 90);
students[0][1] = new Student("Alice", "Smith", 85);
students[0][2] = new Student("Bob", "Johnson", 95);
students[1][0] = new Student("Emily", "Davis", 88);
students[1][1] = new Student("Michael", "Brown", 92);
students[1][2] = new Student("Sophia", "Miller", 87);
%>

<html>
<head>
<title>Student Matrix</title>
</head>
<body>
<h1>Student Matrix</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Value</th>
</tr>
<%
// Iterate over the matrix and display student information
for (int i = 0; i < students.length; i++) {
for (int j = 0; j < students[i].length; j++) {
Student student = students[i][j];
%>
<tr>
<td><%= student.getName() %></td>
<td><%= student.getLastname() %></td>
<td><%= student.getValue() %></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 14/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

En esta matriz se puede apreciar lo mismo que el anterior cuadro solo que con algunos datos
agregados tanto en el código como en la matriz en si ya que se añadió algunos nombres nuevos
como se quito uno del cuadro anterior el cual sería toda la línea de jane Smith

8. Crear una clase Producto y poner los atributos que usted quiera, luego almacene los productos en
un arreglo o en una matriz que se llame tienda.
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 15/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

9. Desarrolle un aplicativo que desde un formulario envíe dos números y muestre el resultado de la
suma, resta, multiplicación y división en un archivo JSP.

Ejemplo
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 16/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01
Nro. xxx

DISEÑO Y DESARROLLO DE SOFTWARE Página 17/17


CURSO: Desarrollo de Aplicaciones Web Laboratorio Nro. 01

Conclusiones:
Indicar las conclusiones que llegó después de los temas tratados de manera práctica en este laboratorio.
1.se pudo crear y ejecutar algunos de los ejemplos y también se pudo familiarizar con apache tomcat y la
creación de aplicaciones web
2.gracias a algunos ejemplos pude resolver los ejercicios y aplicar algunas cosas aprendidas en anteriores
cursos aquí como modificaciones en HTML, así como algunos lenguajes de programación con Python, etc.
3. se considero el uso de los ejemplos para realizar nuestros ejercicios de programación y así poder
familiarizarnos con todo este de programación web

También podría gustarte