practical file list question
practical file list question
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Information</title>
</head>
<body>
<h1>User Form</h1>
<form action="displayUser.jsp" method="post">
Name: <input type="text" name="name" /><br/>
Email: <input type="text" name="email" /><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Step 3: Display the Data from JavaBean (displayUser.jsp)
jsp
Copy code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="user" class="UserBean" scope="request" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Information Display</title>
</head>
<body>
<h1>User Information</h1>
<p>Name: <jsp:getProperty name="user" property="name" /></p>
<p>Email: <jsp:getProperty name="user" property="email" /></p>
</body>
</html>
Explanation:
JavaBean (UserBean.java): A simple Java class with getter and setter methods
for name and email.
jsp
: This tag is used to create or reference a JavaBean instance.
jsp
: This tag sets the properties of the JavaBean based on the form inputs.
jsp
: This tag retrieves the values from the JavaBean and displays them.
7. JSP Program: Using JSTL (JSP Standard Tag Library)
The JSTL allows us to avoid writing Java code in JSP. This program demonstrates how
to use JSTL for looping and condition checking.
Step 1: Add JSTL Dependency
Make sure to include the JSTL library in your project, which is usually available via
Maven or as a .jar file.
Step 2: Display Numbers from 1 to 10 Using JSTL (numbers.jsp)
jsp
Copy code
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSTL Example</title>
</head>
<body>
<h1>Numbers from 1 to 10</h1>
<ul>
<c:forEach var="i" begin="1" end="10">
<li>${i}</li>
</c:forEach>
</ul>
</body>
</html>
Explanation:
taglib directive: Imports the JSTL core library.
c
: This tag iterates from 1 to 10 and displays each number in a list (<li>).
8. JSP Program: Connecting to a Database (JDBC Example)
This program demonstrates how to connect to a MySQL database and retrieve data
using JDBC.
Step 1: Create Database Table
Run the following SQL commands to create a table and insert data:
sql
Copy code
CREATE DATABASE mydatabase;
USE mydatabase;
if (username != null) {
Cookie userCookie = new Cookie("username", username);
userCookie.setMaxAge(60*60); // Set cookie to expire in 1 hour
response.addCookie(userCookie);
} else {
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")) {
username = cookie.getValue();
}
}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome, <%= username %>!</h1>
</body>
</html>
Explanation:
Cookies: response.addCookie() adds the username as a cookie, and
request.getCookies() retrieves it on subsequent requests.
setMaxAge: This sets the cookie's lifespan (1 hour in this case).
10. JSP Program: Redirecting to Another Page
This example demonstrates how to redirect a user to another page in JSP.
JSP Code (redirect.jsp)
jsp
Copy code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String destination = "welcome.jsp";
response.sendRedirect(destination); // Redirects to welcome.jsp
%>
Explanation:
sendRedirect(): This method sends an HTTP response to redirect the browser
to another URL.
These JSP examples cover common use cases such as form handling, database
connection,
4o
You said:
more real life program example
ChatGPT said:
ChatGPT
Here are more real-life JSP examples that demonstrate various practical
functionalities, including user authentication, file upload, form validation, and displaying
data in a paginated manner.
1. JSP Program: User Authentication System
This program demonstrates a simple user authentication system where a user logs in
with a username and password, and their credentials are validated against a database.
Step 1: Create a Database Table for Users
sql
Copy code
CREATE DATABASE mydatabase;
USE mydatabase;
if (rs.next()) {
isValidUser = true;
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
if (isValidUser) {
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("login.jsp?error=Invalid credentials");
}
%>
Step 4: Welcome Page After Successful Login (welcome.jsp)
jsp
Copy code
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = (String) session.getAttribute("username");
if (username == null) {
response.sendRedirect("login.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Welcome</title>
</head>
<body>
<h1>Welcome, <%= username %>!</h1>
<a href="logout.jsp">Logout</a>
</body>
</html>
Step 5: Logout Page (logout.jsp)
jsp
Copy code
<%
session.invalidate();
response.sendRedirect("login.jsp");
%>
Explanation:
Database validation: User credentials are validated against the users table in
MySQL.
Session management: On successful login, the username is stored in the
session. The user is redirected to the welcome page, and session invalidation
occurs during logout.
<%
String uploadPath = application.getRealPath("") + File.separator + "uploads";
File uploadDir = new File(uploadPath);
if (!uploadDir.exists()) {
uploadDir.mkdir();
}
List<String> products