Interview Questions

Web Application Auto Login

In general, every web application will have an authentication mechanism, to authenticate the users accessing the web application.

Suppose if there is a requirement to autologin to a web application from a given page, use the following code.

<html>
<head>
</head>
<script type="text/javascript">

function autoLogin()
{
var xmlhttp;
alert('loadXMLDoc');
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
         
          alert("Response received --> "+xmlhttp.responseText);
    document.write(xmlhttp.responseText);
    }
  }
   var url = "http://localhost:8080/webAppName/login.jsp?username=<userName>&password=<password>";// url to auto login a web Application
xmlhttp.open("POST",url,true);
xmlhttp.send(null);
}

autoLogin();

</script>


</html>

The above code uses ajax to make a post request to the web app with username and password. Three things we need to know for auto login are as follows

1.) url of the web application
2.) relative url to make the post request
3.) usename/password along with the parameter names for username and password for making post request.

If the login attempt was successful, the user will be redirected to the web app page after successful login.