Wednesday, 8 October 2014

Basic Login functionality in Servlet with static username and password

LoginCls.java
/**********************************************************************/
package LoginPkg;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginCls
 */
@WebServlet("/LoginCls")
public class LoginCls extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginCls() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            PrintWriter pw=response.getWriter();
            String userName=request.getParameter("username");
            String password=request.getParameter("password");
            String str=new String("");
            str=  "<html>"+
                        "<body>"+
                              "<h1><marquee>Login Form</marquee></h1>"+
                              "<form action=\"LoginCls\">"+
                              "<table align=\"center\">"+
                                    "<tr>"+
                                          "<td>Login</td>"+
                                          "<td><input type=\"text\" name=\"username\"></td>"+
                                    "</tr>"+
                                    "<tr>"+
                                          "<td>Passoword</td>"+
                                          "<td><input type=\"password\" name=\"password\"></td>"+
                                    "</tr>"+
                                    "<tr>"+
                                          "<td ><input type=\"submit\"></td>"+
                                    "</tr>"+
                              "</table>"+
                                   
                              "<h3 align=\"center\"> Login not Successful </h3>"+
                        "</body>"+
                  "</html>";

            if(userName.equals("Lavish") && password.equals("Kothari"))
            {
                  pw.println("Login Successful");
                 
            }
            else
            {
                  pw.println(str);
            }
      }

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
      }

}

/**********************************************************************/


login.html
/***********************************************************************/


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

      <h1><marquee>Login Form</marquee></h1>
      <form action="LoginCls">
      <table align="center">
            <tr>
                  <td>Login</td>
                  <td><input type="text" name="username"></td>
            </tr>
            <tr>
                  <td>Passoword</td>
                  <td><input type="password" name="password"></td>
            </tr>
            <tr>
                  <td ><input type="submit"></td>
            </tr>
      </table>
           
      </form>
</body>
</html>

/***********************************************************************/

No comments:

Post a Comment