title: suggestions on creating a admin page using jsp
body: <p>I am pretty new in developing web applications using Java. I have developed a small application which is a login &amp; registration page both of which are working fine. Now, I have decided on making a admin page(using jsp). In my java code I control the redirection of jsp pages (if user!="admin"then home.jsp else user=="admin" then admin.jsp). in my admin page what I want to do is I want the admin to be able to view all the users registered and can edit their details or delete them. Someone can please suggest me on how to achieve this.</p>
  
  <p>Login Servlet.java(code where I decide between admin &amp; regular user)</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          String email = request.getParameter("email");
          String password = request.getParameter("password");
          String errorMsg = null;
          String name;
          if(email == null || email.equals("")){
              errorMsg ="User Email can't be null or empty";
          }
          if(password == null || password.equals("")){
              errorMsg = "Password can't be null or empty";
          }
  
          if(errorMsg != null){
              RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");
              PrintWriter out= response.getWriter();
              out.println("&lt;font color=red&gt;"+errorMsg+"&lt;/font&gt;");
              rd.include(request, response);
          }else{
  
          Connection con = (Connection) getServletContext().getAttribute("DBConnection");
          PreparedStatement ps = null;
          ResultSet rs = null;
          try {
              ps = con.prepareStatement("select id, name, email,country from Users where email=? and password=?");
              ps.setString(1, email);
              ps.setString(2, password);
              rs = ps.executeQuery();
  
              if(rs != null &amp;&amp; rs.next()){
  
                  User user = new User(rs.getString("name"), rs.getString("email"), rs.getString("country"), rs.getInt("id"));
                 name=rs.getString("name");
                 System.out.println("Name:"+ name);
                  //if(rs.getString("name")!="admin")
                 if(!name.equalsIgnoreCase("admin"))
                  {
                  logger.info("User found with details="+user);
                  HttpSession session = request.getSession();
                  session.setAttribute("User", user);
                  response.sendRedirect("home.jsp");
                  }
                  // String rs1=rs.getString();
                  else if(name.equalsIgnoreCase("admin"))
                  {
                  logger.info("Admin found with details="+user);
                  HttpSession session = request.getSession();
                  session.setAttribute("User", user);
                  response.sendRedirect("admin.jsp");
                  }
  
              }else{
                  RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");
                  PrintWriter out= response.getWriter();
                  logger.error("User not found with email="+email);
                  out.println("&lt;font color=red&gt;No user found with given email id, please register first.&lt;/font&gt;");
                  rd.include(request, response);
              }
          } catch (SQLException e) {
              e.printStackTrace();
              logger.error("Database connection problem");
              throw new ServletException("DB Connection problem.");
          }finally{
              try {
                  rs.close();
                  ps.close();
              } catch (SQLException e) {
                  logger.error("SQLException in closing PreparedStatement or ResultSet");;
              }
  
          }
          }
  </code></pre>
  
  <p>home.jsp</p>
  
  <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;%@page import="com.javadbproject.util.User"%&gt;
  &lt;%@ page language="java" contentType="text/html; charset=US-ASCII"
      pageEncoding="US-ASCII"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"&gt;
  &lt;title&gt;Home Page&lt;/title&gt;
  
  &lt;link rel="stylesheet" type="text/css" href="&lt;c:url value='/loginstyle.css'/&gt;"&gt; 
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;%User user = (User) session.getAttribute("User"); %&gt;
  &lt;h3&gt;Hi &lt;%=user.getName() %&gt;&lt;/h3&gt;
  &lt;strong&gt;Your Email&lt;/strong&gt;: &lt;%=user.getEmail() %&gt;&lt;br&gt;
  &lt;strong&gt;Your Country&lt;/strong&gt;: &lt;%=user.getCountry() %&gt;&lt;br&gt;
  &lt;br&gt;
  &lt;form action="Logout" method="post"&gt;
  &lt;input type="submit" value="Logout" &gt;
  
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>AuthenticationServlet</p>
  
  <pre><code>package com.javadbproject.servlet.filters;
  
  
  import java.io.IOException;
  
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.annotation.WebFilter;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  import org.apache.log4j.Logger;
  
  @WebFilter("/AuthenticationFilter")
  public class AuthenticationFilter implements Filter {
  
      private Logger logger = Logger.getLogger(AuthenticationFilter.class);
  
      public void init(FilterConfig fConfig) throws ServletException {
          logger.info("AuthenticationFilter initialized");
      }
  
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  
          HttpServletRequest req = (HttpServletRequest) request;
          HttpServletResponse res = (HttpServletResponse) response;
  
          String uri = req.getRequestURI();
          logger.info("Requested Resource::"+uri);
  
          HttpSession session = req.getSession(false);
  
          if(session == null &amp;&amp; !(uri.endsWith("html") || uri.endsWith("Login") || uri.endsWith("Register"))){
              logger.error("Unauthorized access request");
              res.sendRedirect("login.html");
          }else{
              // pass the request along the filter chain
              chain.doFilter(request, response);
          }
  
  
      }
  
      public void destroy() {
          //close any resources here
      }
  
  }
  </code></pre>
  
  <p>I am looking to develop my admin.jsp on the similar lines as my home.jsp</p>
  
  <p>Thanks!!</p>