0% found this document useful (0 votes)
20 views

Import Javax - Servlet.

This Java servlet code connects to a MySQL database to authenticate user login credentials. It retrieves user details from the student table matching the email and password parameters. If credentials are valid, user details like name, roll, email and course are displayed. Otherwise an error message is shown. The connections and result set are closed in the finally block.

Uploaded by

cha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Import Javax - Servlet.

This Java servlet code connects to a MySQL database to authenticate user login credentials. It retrieves user details from the student table matching the email and password parameters. If credentials are valid, user details like name, roll, email and course are displayed. Otherwise an error message is shown. The connections and result set are closed in the finally block.

Uploaded by

cha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import javax.servlet.

*;
import java.io.*;
import java.sql.*;
public class Login extends Generic Servlet{
public void service (ServletRequest req, ServletResponse res) throws
ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
Connection con=null;
Statement stm=null;
ResultSet rs=null;
String email=req.getParameter ("email");
String pass-req.getParameter ("pass");
try{
Class. for Name("com.mysql.jdbc.Driver");
con= DriverManager.getConnection("jdbc: mysql://localhost: 3306/demo", "root","");
String query= "select * from student where email='"+email+" and
password='"+pass+"¹";
stm= con.createStatement ();
rs=stm.executeQuery (query);
if(rs.next()) {
out.println("<h1>Welcome</h1>");
out.println("<p>Name - "+rs.getString("name")+"</p>");
out.println("<p>Roll No - "+rs.getString("roll")+"</p>");
out.println("<p>Email - "+rs.getString("email")+"</p>");
out.println("<p>Course - "+rs.getString("course")+"</p>");
}
else
out.println("<h1 style="background-color: #eb1616¹>Invalid email or password
!</h1>");
}
catch(ClassNotFoundException e) {
out.println(e);
}
catch(SQLException e) {
out.println(e);
}
finally{
try{
rs.close();
stm.close();
con.close();
}
catch (SQLException e) {
out.println(e);
}
out.println("<p><a href='login.html'>Back</a></p>");
}
}
}

You might also like