0% found this document useful (0 votes)
2 views4 pages

Assignment No 5 - WT

The document outlines the design and implementation of a Student Login system using JSP, Servlet, and MySQL. It includes the setup of a MySQL database, the creation of a login form (login.jsp), a servlet for handling login requests (LoginServlet.java), and pages for successful and failed login responses (welcome.jsp and error.jsp). Additionally, it provides a database connection class (DBConnection.java) and the necessary web.xml configuration for servlet mapping.

Uploaded by

hustleriitian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Assignment No 5 - WT

The document outlines the design and implementation of a Student Login system using JSP, Servlet, and MySQL. It includes the setup of a MySQL database, the creation of a login form (login.jsp), a servlet for handling login requests (LoginServlet.java), and pages for successful and failed login responses (welcome.jsp and error.jsp). Additionally, it provides a database connection class (DBConnection.java) and the necessary web.xml configuration for servlet mapping.

Uploaded by

hustleriitian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment No:5

Problem statement: Design and Build Student Login using JSP, Servlet and MySQL

1. MySQL Table Setup


CREATE DATABASE studentdb;
USE studentdb;
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL
);
INSERT INTO students (email, password) VALUES ('student@example.com', '12345');

2. login.jsp
<%@ page language="java" %>
<html>
<head>
<title>Student Login</title>
</head>
<body>
<h2>Student Login</h2>
<form action="LoginServlet" method="post">
Email: <input type="text" name="email" required /><br><br>
Password: <input type="password" name="password" required /><br><br>
<input type="submit" value="Login" />
</form>
</body>
</html>

3. LoginServlet.java
package com.student;
import java.io.IOException;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email");
String password = request.getParameter("password");
try (Connection conn = DBConnection.getConnection()) {
String sql = "SELECT * FROM students WHERE email = ? AND password = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, email);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
HttpSession session = request.getSession();
session.setAttribute("studentEmail", email);
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("error.jsp");
}

} catch (Exception e) {
e.printStackTrace();
}
}
}
4. DBConnection.java
package com.student;
import java.sql.*;

public class DBConnection {


public static Connection getConnection() throws Exception {
String url = "jdbc:mysql://localhost:3306/studentdb";
String user = "root"; // your MySQL username
String password = ""; // your MySQL password
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(url, user, password);
}
}
5. welcome.jsp
<%@ page language="java" session="true" %>
<html>
<head><title>Welcome</title></head>
<body>
<h2>Welcome, <%= session.getAttribute("studentEmail") %>!</h2>
</body>
</html>
6. error.jsp
<html>
<head><title>Login Failed</title></head>
<body>
<h3>Invalid email or password. Please <a href="login.jsp">try again</a>.</h3>
</body>
</html>

7. web.xml
<web-app>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.student.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy