Ex No: 18 PERMISSION CHECKER USING JSP
Aim: To create a JSP page to check permission for a user.
Algorithm
Step1: Create a folder “Ex18” to place the HTML and JSP files.
Step2: Create files “index.html”, “ex18.jsp”, “adminpage.jsp” and “logout.jsp”.
Source Code

index.html


<!DOCTYPE html>
<html>
 <head>
  <title>JSP Permission Checker</title> 
   <meta charset="UTF-8"> 
 </head>
 <style>
  header {
   text-align:center;
   height: 100px;
   background-color: Tomato;
   padding: 10px; 
   border-radius: 10px 10px 0px 0px;
  }
  footer {
   text-align:center;
   font-size: 12px;
   height: 50px;
   line-height: 50px;
   background-color: Tomato;
   padding: 10px; 
   border-radius: 10px;  
  }
  section{
   width: auto; 
   border: 2px solid gray;
   padding: 10px; 
   border-radius: 10px;
   margin:5px;
  }
  input{
   border-radius: 5px;
   margin: 5px;
  }
 </style>
 <body>  
  <header>
   <h1>JSP Permission Checker</h1>  
  </header>  
  <article>
    <section>  
     <form method="POST" action="ex18.jsp">
      <label>Enter Name:</label>
      <input type="text" name="txtname" /><br>
      <label>Enter Password:</label>
      <input type="password" name="txtpwd" /><br>
      <input type="submit" value="Submit"/>
     </form> 
    </section>
  </article>
  <footer>
   Developed by K. Anbarasan
  </footer>
 </body>
</html>

ex18.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.string.*" %>
<%
  String name, pwd;
  name = request.getParameter("txtname");
  pwd = request.getParameter("txtpwd");
  if(name.equals("admin") && pwd.equals("admin123"))
  {
   session.setAttribute("usrtype", "admin");
   response.sendRedirect("adminpage.jsp");
  }
  else
  {
   response.sendRedirect("index.html");
  }
%>

adminpage.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.string.*" %>
<!DOCTYPE html>
<html>
 <head>
  <title>JSP Permission Checker</title> 
   <meta charset="UTF-8"> 
 </head>
 <style>
  header {
   text-align:center;
   height: 100px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px 10px 0px 0px;
  }
  footer {
   text-align:center;
   font-size: 12px;
   height: 50px;
   line-height: 50px;
   background-color: Olive;
   padding: 10px; 
   border-radius: 10px;  
  }
  section{
   width: auto; 
   border: 2px solid gray;
   padding: 10px; 
   border-radius: 10px;
   margin:5px;
  }
  input{
   border-radius: 5px;
   margin: 5px;
  }
 </style>
 <body>  
  <%
    String usrtype;
    usrtype = (String)session.getAttribute("usrtype");
    if(usrtype != null && usrtype.equals("admin"))
    {
  %> 
  <header>
   <h1>Welcome to <%= usrtype%> page</h1>  
  </header>  
  <article>
    <section>
      <a href="logout.jsp">Logout</a>
    </section>
  </article>
  <%   
    }
    else
    {
  %> 
  <header>
   <h1>You are not allowed Here</h1>  
  </header>  
  <article>
    <section>
      <a href="index.html">Login Here</a>
    </section>
  </article>
  <%
    }
  %>
  <footer>
   Developed by K. Anbarasan
  </footer>
 </body>
</html>

logout.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %>
<%@ page import ="java.sql.*" %>
<%
  session.setAttribute("usrtype", null);
  session.invalidate();
  response.sendRedirect("index.html");
%>
Result: Thus JSP page is created to check permission for a useer.