-
Download Apache Tomcat from the official website: Apache Tomcat.
-
Install and configure Apache Tomcat following the instructions provided on the website.
-
Download and install PostgreSQL from the official website: PostgreSQL.
-
Follow the installation instructions for your operating system.
-
Create a new PostgreSQL database and table. You can use tools like pgAdmin or the command line. Here's an example:
CREATE DATABASE mydatabase; \c mydatabase; CREATE TABLE users ( id serial primary key, name varchar(255), email varchar(255) ); INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
-
In your Apache Tomcat 'webapps' folder, create a new folder for your project, for example, 'myjspapp'.
-
Inside the 'myjspapp' folder, create a new file named
index.jsp
with the following content:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <title>JSP with PostgreSQL</title> </head> <body> <h1>User Data from PostgreSQL</h1> <% String url = "jdbc:postgresql://localhost:5432/mydatabase"; String user = "your_username"; String password = "your_password"; try { Class.forName("org.postgresql.Driver"); Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM users"); while (resultSet.next()) { out.println("<p>ID: " + resultSet.getInt("id") + "</p>"); out.println("<p>Name: " + resultSet.getString("name") + "</p>"); out.println("<p>Email: " + resultSet.getString("email") + "</p>"); } resultSet.close(); statement.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); } %> </body> </html>
Replace "your_username" and "your_password" with your PostgreSQL username and password.
Inside the 'WEB-INF' folder of your 'myjspapp' directory, create a 'web.xml' file with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>MyJSPApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
-
Start your PostgreSQL server.
-
Copy the 'myjspapp' folder into the 'webapps' directory of your Tomcat installation.
-
Start Tomcat.
-
Access your JSP page in the browser by visiting
http://localhost:8080/myjspapp/
.
You should see the user data retrieved from the PostgreSQL database displayed on the page.
In this section, we'll create a basic registration form and a confirmation page. Follow the steps below:
- Create a new folder for your project, for example, 'jsp-forms-navigation'.
- Inside the project folder, create a 'WEB-INF' folder.
Inside your project folder, create a new file named index.jsp
with the following content:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<h1>Registration Form</h1>
<form action="confirmation.jsp" method="post">
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" required><br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Inside the 'WEB-INF' folder of your project, create a new file named confirmation.jsp
with the following content:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Confirmation Page</title>
</head>
<body>
<h1>Confirmation Page</h1>
<p>Thank you for registering!</p>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
%>
<p>First Name: <%= firstName %></p>
<p>Last Name: <%= lastName %></p>
<p>Email: <%= email %></p>
<p><a href="index.jsp">Go back to the registration form</a></p>
</body>
</html>
Inside the 'WEB-INF' folder, create a 'web.xml' file with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>FormsNavigationApp</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- Copy your project folder into the 'webapps' directory of your Tomcat installation.
- Start Tomcat.
- Access the registration form in the browser by visiting
http://localhost:8080/jsp-forms-navigation/
.
Fill out the form, submit it, and you'll be redirected to the confirmation page showing the entered information
Certainly! Let's extend the previous example to include a simple form that allows users to update their information in the database. We'll create an additional JSP page for the update form.
Inside your project folder, create a new file named update.jsp
with the following content:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Update Information</title>
</head>
<body>
<h1>Update Information</h1>
<form action="updateConfirmation.jsp" method="post">
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName" value="<%= request.getAttribute("firstName") %>" required><br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName" value="<%= request.getAttribute("lastName") %>" required><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<%= request.getAttribute("email") %>" required><br>
<input type="submit" value="Update">
</form>
</body>
</html>
Inside the 'WEB-INF' folder of your project, update the confirmation.jsp
file to include a link to the update form:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Confirmation Page</title>
</head>
<body>
<h1>Confirmation Page</h1>
<p>Thank you for registering!</p>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
%>
<p>First Name: <%= firstName %></p>
<p>Last Name: <%= lastName %></p>
<p>Email: <%= email %></p>
<p><a href="index.jsp">Go back to the registration form</a></p>
<p><a href="update.jsp?firstName=<%= firstName %>&lastName=<%= lastName %>&email=<%= email %>">Update Information</a></p>
</body>
</html>
Create a new file named updateConfirmation.jsp
inside the 'WEB-INF' folder with the following content:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>Update Confirmation</title>
</head>
<body>
<h1>Update Confirmation</h1>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String email = request.getParameter("email");
String newFirstName = request.getParameter("newFirstName");
String newLastName = request.getParameter("newLastName");
String newEmail = request.getParameter("newEmail");
String url = "jdbc:postgresql://localhost:5432/mydatabase";
String user = "your_username";
String password = "your_password";
try {
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
PreparedStatement statement = connection.prepareStatement("UPDATE users SET name=?, email=? WHERE name=? AND email=?");
statement.setString(1, newFirstName);
statement.setString(2, newEmail);
statement.setString(3, firstName);
statement.setString(4, email);
int rowsUpdated = statement.executeUpdate();
if (rowsUpdated > 0) {
out.println("<p>Information updated successfully!</p>");
} else {
out.println("<p>No rows were updated.</p>");
}
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
<p><a href="index.jsp">Go back to the registration form</a></p>
</body>
</html>
In confirmation.jsp
, update the link to the update form to include the email parameter:
<p><a href="update.jsp?firstName=<%= firstName %>&lastName=<%= lastName %>&email=<%= email %>">Update Information</a></p>
- Copy your updated project folder into the 'webapps' directory of your Tomcat installation.
- Start Tomcat.
- Access the registration form in the browser by visiting
http://localhost:8080/jsp-forms-navigation/
.
After submitting the registration form, you can navigate to the confirmation page and then update the information by following the "Update Information" link.