Sunday, December 24, 2017

Login Authentication program (Demonstrate the use RequestDispatcher and its method such as include() and forward() )



Create Table with following field:
TableName: USERINFO
id                    name                 password
101                 rupendra            a@b.com
102                 rahul                  x@y.com

Program save with Name: index.html

<html>
<head><title>Login Page</title></head>
<body>
<form method="get" action="loginServlet">
UserId : <input type="text" name="txtUserId"><br>
Password : <input type="password" name="txtPassword"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

Program save with Name: LoginServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class LoginServlet extends HttpServlet
{
          public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
          {
                   try
                   {
                   response.setContentType("text/html");
                   PrintWriter out=response.getWriter();
                   String id=request.getParameter("txtUserId");
                   String pwd=request.getParameter("txtPassword");
                   Class.forName("oracle.jdbc.driver.OracleDriver");
                   Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
                   PreparedStatement stmt=con.prepareStatement("select * from userInfo where id=? and password=?");
                   stmt.setString(1,id);
                   stmt.setString(2,pwd);
                   ResultSet rset=stmt.executeQuery();
                   if(rset.next())
                   {
                             out.println("<b>Welcome, "+rset.getString(2)+"</b>");
                   }
                   else
                   {
                             out.println("<b> Invalid UserId or Password....<b><br>");
                             RequestDispatcher rd=request.getRequestDispatcher("/index.html");
                                    rd.include(request,response);
                   }
                   con.close();
                   out.close();
                   }
                   catch(Exception e)
                   {
                             System.out.println(e);
                   }
          }
}

Program save with Name: web.xml

<web-app>

<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>loginServlet</url-pattern>
</servlet-mapping>

</web-app>
Output :
UserId :
Password :

If valid userId and password then display:      
Welcome, rupendra

If invalid userId or password then display:      
Invalid UserId or Password....
UserId :
Password :

No comments:

Post a Comment

Java Functional Interface

Java Lambda expression Lambda expression came to enable to use functional programming feature in java. It provides the facility t...