Wad Assn2.1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Saran S E

717821F148
III-yr IT –A
WEB APPLICATION
DEVELOPMENT USING JAVA
21PE18
ASSIGNMENT - 2
The Mexican Restaurant Website

Source code:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

private static final String JDBC_URL =


"jdbc:mysql://localhost:3306/yourdb";
private static final String DB_USER = "youruser";
private static final String DB_PASSWORD = "yourpassword";

protected void doGet(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException {
// Handle GET requests to display the user registration form
showRegistrationForm(response);
}

protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String password = request.getParameter("password");

// Validate user input


if (name == null || name.isEmpty() || email == null ||
email.isEmpty() || password == null || password.isEmpty()) {
showRegistrationFormWithErrorMessage("All fields are
required.", response);
return;
}

// Database connection
Connection connection = null;
PreparedStatement statement = null;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(JDBC_URL,
DB_USER, DB_PASSWORD);

// Check if the email is already in use


String checkEmailQuery = "SELECT id FROM users
WHERE email = ?";
statement =
connection.prepareStatement(checkEmailQuery);
statement.setString(1, email);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
showRegistrationFormWithErrorMessage("Email is
already in use.", response);
return;
}

// Insert the new user into the database


String insertUserQuery = "INSERT INTO users (name,
email, password) VALUES (?, ?, ?)";
statement =
connection.prepareStatement(insertUserQuery);
statement.setString(1, name);
statement.setString(2, email);
statement.setString(3, password);
statement.executeUpdate();

// Registration successful, show a success message


showRegistrationSuccess(response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (statement != null) statement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

private void showRegistrationForm(HttpServletResponse


response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><head><title>User
Registration</title></head><body>");
out.println("<h1>User Registration</h1>");
out.println("<form method='post' action='UserServlet'>");
out.println("Name: <input type='text' name='name'><br>");
out.println("Email: <input type='text' name='email'><br>");
out.println("Password: <input type='password'
name='password'><br>");
out.println("<input type='submit' value='Register'>");
out.println("</form>");
out.println("</body></html>");
}

private void showRegistrationFormWithErrorMessage(String


errorMessage, HttpServletResponse response) throws
IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><head><title>User
Registration</title></head><body>");
out.println("<h1>User Registration</h1>");
out.println("<p style='color: red;'>" + errorMessage +
"</p>");
out.println("<form method='post' action='UserServlet'>");
out.println("Name: <input type='text' name='name'><br>");
out.println("Email: <input type='text' name='email'><br>");
out.println("Password: <input type='password'
name='password'><br>");
out.println("<input type='submit' value='Register'>");
out.println("</form>");
out.println("</body></html>");
}

private void showRegistrationSuccess(HttpServletResponse


response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><head><title>Registration
Successful</title></head><body>");
out.println("<h1>Registration Successful</h1>");
out.println("<p>Your account has been created
successfully.</p>");
out.println("</body></html>");
}
}
OUTPUT

You might also like