JSP Login Example

JSP Login Page

Project Structure




Jars required
                 1)   Mysql-connector.jar(for connecting MYSQL DB)
                 2)      Ojdbc14.jar(for Oracle DB)
Jsp pages created
                   1)      Login.jsp
                   2)      Process.jsp
                   3)      LoginFailure.jsp
                   4)      Success.jsp
                   5)      Error.jsp
             6)    Web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>LoginExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.lang.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Login Page</title>
</head>
<body>
<form action="process.jsp" method="post">

<h1>Login Form</h1>
<table border="1">
<tr><td>User Name :</td><td><input type ="text" name="firstName"/></td></tr>
<tr><td>Password :</td><td><input type ="password" name="password"/></td></tr>
<tr><td><input type="submit" value="Login"></td></tr>

</table>
</form>

</body>
</html>

Process.jsp
<%@ page import ="java.sql.*" %>
<%@ page errorPage="error.jsp" %>
<%
Connection con = null;
try{
     
      String userid = request.getParameter("firstName");   
    String pwd = request.getParameter("password");
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/learnjava",
            "username", "password");
    Statement st = con.createStatement();
    ResultSet rs;
    rs = st.executeQuery("select * from login where user_name='" + userid + "' and password='" + pwd + "'");
    if (rs.next()) {
       
        response.sendRedirect("success.jsp");
    } else {
      response.sendRedirect("LoginFailure.jsp");
    }
}
catch(Exception e){
      response.sendRedirect("error.jsp");
      e.printStackTrace();
}
finally{
      if(con != null){
      con.close();
      }
}

%>


LoginFailure.jsp



<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Failure</title>
</head>
<body>
<form action="process.jsp" method="post">
<h1><p style="color:red">Login Failure.. Please Login again</p></h1>
<table border="1">
<tr><td>User Name :</td><td><input type ="text" name="firstName"/></td></tr>
<tr><td>Password :</td><td><input type ="password" name="password"/></td></tr>
<tr><td><input type="submit" value="Login"></td></tr>

</table>
</form>

</body>
</html>

Success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META Http-Equiv="Cache-Control" Content="no-cache">
<META Http-Equiv="Pragma" Content="no-cache">
<META Http-Equiv="Expires" Content="0">
<title>Login Sucess</title>
</head>
<body>
<h1>Login Sucess</h1>
Welcome to Admin Page
<a href='Login.jsp'>Log out</a>;
</body>
</html>

Error.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error Page</title>
</head>
<body>
<%@ page isErrorPage="true" %>

<h1><p style="color:red">Error Occured while contacting to DB</p></h1>

</body>
</html>


Result:





0 comments:

Post a Comment