Login page using jsp and servlet with mysql database


        This login example was developed using Servlet, Jsp and mysql database connectivity

Environment used

    Servlet 3.1 jar

    Mysql 5.1 server or greater version and its mysql connector jar

    Eclipse luna EE or greater version

    tomcat 8


Login page (index.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

Simple login Example using servlet jsp and mysql database connectivity
<br> Create a test database, student table and insert some user
information in it.
<br>
<br>

<form action="LoginController" method="post">
Enter username :<input type="text" name="username"> <br>
Enter password :<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>

</body>
</html>

web.xml mapping

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>login</display-name>
<servlet>
<description></description>
<display-name>LoginController</display-name>
<servlet-name>LoginController</servlet-name>
<servlet-class>com.candidjava.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/LoginController</url-pattern>
</servlet-mapping>

</web-app>

Servlet code for login page with database mysql

package com.candidjava;

import java.io.IOException;
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class LoginController
*/
public class LoginController extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String un=request.getParameter("username");
String pw=request.getParameter("password");

// Connect to mysql and verify username password

try {
Class.forName("com.mysql.jdbc.Driver");
// loads driver
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); // gets a new connection

PreparedStatement ps = c.prepareStatement("select userName,pass from student where userName=? and pass=?");
ps.setString(1, un);
ps.setString(2, pw);

ResultSet rs = ps.executeQuery();

while (rs.next()) {
response.sendRedirect("success.html");
return;
}
response.sendRedirect("error.html");
return;
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


Success Page

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Login success
</body>
</html>

error Page

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Invalid username or password, Please try again with valid
</body>
</html>

Output screenshot


Download

        Servlet Jsp Login example with mysql war

        Servlet Jsp Login example with mysql eclipse zip





Related Post

Comments


©candidjava.com