Sample login and error pages for JEE form-based authentication

In JEE form-based authentication, you can specify a customized login page and an error page. The login page, which prompts the user to enter a user id and password, refers to a special j_security_check servlet. Two HTTP request parameters (form input fields) must always be in the request, one called j_username and the other, j_password.

When the web container receives a request for the j_security_check servlet, it passes the request to the security mechanism of the application server to perform the authentication. If authentication fails, the error page is displayed. Below is code for a sample login page. Copy and save this code in login.jsp under the WebContent folder.

<%@ 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>Sample Login Page for JEE Security</title>
<style type="text/css">H1 {color: navy}</style>
</head>
<body>
<table width="500" border="0">
   <tbody>
      <tr>
         <td colspan="3" width="80%" align="center"><b><font face="Verdana"size="+2" 
             color="#15406a">Sample Login</font></b><hr>
         </td>
      </tr>
      <tr>
         <td colspan="3" width="560" height="65">
         <form method="POST" action="j_security_check">
         <div>
         <table width="100%" border="1" bgcolor="#e9e9e9">
            <tbody>
               <tr>
                  <td align="right" width="169" 
                      bgcolor="#e9e9e9"><b>
                      <font face="Verdana">User id:</font></b></td>
                  <td width="315"><input type="text" name="j_username"></td>
               </tr>
               <tr>
                  <td align="right" width="169" bgcolor="#e9e9e9">
                      <font face="Verdana"><b>Password:</b></font></td>
                  <td width="315"><input type="password" name="j_password"></td>
               </tr>
               <tr bgcolor="white">
                  <td align="right" width="169" bgcolor="white"></td>
                  <td width="315"><input type="submit" value="Login"></td>
               </tr>
            </tbody>
         </table>
         </div>
         </form></td>
      </tr>
      <tr>
         <td colspan="3" width="560" align="center" height="58" valign="top">
            <script> document.write(Date()+".")
            </script>
         </td>
      </tr>
   </tbody>
</table></body>
</html>

The following code is for a sample error page. Copy and save this code in error.jsp under the WebContent folder.

<%@ 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>Sample Error Page for JEE Security</title>
<style type="text/css">H1 {color: navy}</style>
</head>
<body>
<table width="500" border="0">
   <tbody>
      <tr>
         <td colspan="3" width="80%" align="center"><b><font face="Verdana" size="+2" 
             color="#15406a">Sample Login Error</font></b><hr>
         </td>
      </tr>
      <tr>
         <td colspan="3" width="560" align="center" height="58" 
             valign="top"><br>Authentication error. 
             Please check your user id and password, and try again.</td>
      </tr>
   </tbody>
</table></body>
</html>