Lab04 Webs PDF
Lab04 Webs PDF
Grupo: B Ciclo: VI
Requiere
Excelente Bueno No acept. Puntaje
Criterio de Evaluación mejora
(4pts) (3pts) (0pts) Logrado
(2pts)
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**)
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
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 javax.servlet.http.HttpSession;
@WebServlet("/SessionServlet")
public class SessionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// Get the username from the request parameter (assuming it's provided)
String username = request.getParameter("username");
// Get the list of users from the session, or create a new list if it doesn't exist
List<String> userList = (List<String>) session.getAttribute("userList");
if (userList == null) {
userList = new ArrayList<>();
}
The HttpSession interface in Java provides a way to maintain state (data) across multiple HTTP
requests from a single user. It's an important part of web application development because it allows
you to:
1. Maintain User State: HTTP is a stateless protocol, meaning each request from a client to a server is
independent and does not inherently carry any information about previous requests. HttpSession
allows you to associate state information with a specific user across multiple requests. This enables
the development of interactive and personalized web applications where user-specific data can be
stored and retrieved.
2. Handle Authentication and Authorization: HttpSession can be used to store user authentication
tokens or session identifiers after a user logs in. This allows the server to identify the user across
multiple requests without requiring the user to authenticate with each request. Additionally, you can
store user roles or permissions in the session to handle authorization.
3. Implement Shopping Carts and User Sessions: HttpSession is commonly used in e-commerce
applications to maintain shopping cart data for each user. It allows users to add items to their cart,
navigate through different pages, and make purchases without losing their cart data. Similarly,
HttpSession can be used to manage user sessions, storing information such as user preferences,
browsing history, or temporary data.
4. Manage User Sessions: HttpSession provides methods to manage the lifecycle of user sessions,
including creating, invalidating, and timing out sessions. This allows you to control how long session
data should be retained and when sessions should expire, helping to optimize server resources and
improve security.
5. Cross-Context Communication: HttpSession objects can be shared across multiple servlets or JSPs
within the same web application context. This allows different components of the application to
access and manipulate session data, enabling collaboration and communication between different
parts of the application.
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
<%
// Database connection parameters
String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
String DB_URL = "jdbc:mysql://localhost:3306/students"; // Update with your database name
String USER = "root"; // Update with your database username
String PASS = ""; // Update with your database password
// Form data
String name = request.getParameter("name");
String ageParam = request.getParameter("age");
int age = 0;
// Open a connection
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO student (name, age)
VALUES (?, ?)")) {
pstmt.setString(1, name);
pstmt.setInt(2, age);
<h2>Add Student</h2>
<form action="index.jsp" method="post">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
<input type="submit" value="Add Student">
</form>
</body>
</html>
Nro. xxx
4. Cree una tabla en la misma base se datos students e ingrese otros datos vía JSP.
Nro. xxx
Conclusiones:
Indicar las conclusiones que llegó después de los temas tratados de manera práctica en este laboratorio.