How to Create Admin User in Java

Introduction

This article explains how to create an Admin page in Java. The Netbeans IDE is used for the sample example.

What is An Admin

An Admin is simply a user that has some extra authority/power from a normal user. When we talk in terms of a website, they have the power to manage other users, provide access to them, etcetera. About Admins, we say it is like an owner or a head of a website.

What's the Admin role

In terms of a website, an Admin creates the website architecture, its design, certain additive features in a website, etcetera. For example, when we create an "Online Website" like "online shopping", "online e-ticketing", "online examination", etcetera then an Admin has the power to control the entire website.

How to create an Admin in Java

For creating an Admin we have many ways, in this article I create an Admin user through a database table.

To create an Admin user, we need a table in a database that identifies that login user as an Admin. By determining thier role we can forward it to other pages.

Let's start creating an Admin

For creating an Admin we need to follow a specified procedure in Java.

Step 1

First create a database file named "reg"; the syntax is as in the following:

reg.sql

CREATE TABLE  "REG" (	"UNAME" VARCHAR2(20) NOT NULL ENABLE, "UPASS" VARCHAR2(20) NOT NULL ENABLE, "UTYPE" VARCHAR2(20) NOT NULL ENABLE);
Step 2
Now we insert the following data:
Oracle Database Table reg
Step 3

Open the NetBeans IDE.

NetBeans IDE

Step 4

Choose "Java web" -> "web application" as in the following:

Java Web Page

Step 5

Provide the name "AdminDemo" for your project as in the following.

AdminDemo Project

Step 6

Choose the Java version and the server wizard as in the following.

server and java version wizard

Step 7

Now delete the default "index.jsp" file and create a new "index.html" file and provide the following code for it.

index.html

<!DOCTYPE html>

<html>

    <head>

        <title>Login Page</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">

    </head><center>

        <form action="checklogin.jsp">

    <body bgcolor="khaki">

        <h1>Welcome User</h1>

        <table>

            <tr>

                <td>

                    UserName:

                </td>

                <td>

                    <input type="text" name="uname">

                </td>

            </tr>

            <tr>

                <td>

                    Password:

                </td>

                <td>

                    <input type="password" name="upass">

                </td>

            </tr>

            <tr>

                <td>

                    <input type="submit" value="Submit">

                </td>

                <td>

                    <input type="reset" value="Reset">

                </td>

            </tr>

 

        </table>

    </body>

</html>

Step 8

Now create a JSP file with the name "checklogin" and the following code.

checklogin.jsp

<%@ page language="java" import="java.sql.*"%><b><center>     

        <%

            String name = request.getParameter("uname");

            String pass = request.getParameter("upass");

            try {

                Class.forName("oracle.jdbc.driver.OracleDriver");

                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:XE", "sandeep", "welcome");

                Statement st = con.createStatement();

                ResultSet rs = st.executeQuery("select * from reg");

                int x = 0;

                while (rs.next()) {

                    if ((rs.getString("uname").equals(name)) && rs.getString("upass").equals(pass)) {

                        String stp = rs.getString("utype");

                        if (stp.equals("admin")) {

                            x = 1;

                            break;

                        } else {

                            x = 2;

                            break;

                        }

                    }

                }

                if (x == 2) {

                    response.sendRedirect("student.jsp");

                } else if (x == 1) {

                    response.sendRedirect("admin.jsp");

                } else {

                    out.println("Either you enter Invalid UserName or Password! Please Try Again");

        %>

        <jsp:include page="index.html" />

        <%}

 

            } catch (Exception e) {

                out.println(e);

            }

 

            session.setAttribute("username", name);

        %> 

Note:

The main work is done in this file. This file is responsible for checking which type of user is currently logged in. For validation we used a database table through which they check its type, in other words it is an Admin user or a normal user.

Step 9

Now create another jsp file named "admin.jsp" by which we can ensure that an Admin is logged successfully.

admin.jsp

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

        <title>Admin Page</title>

    </head>

    <body bgcolor="black"><font color="red">

        <h1>This is an Admin Page</h1>

        <%

            String str = (String) session.getAttribute("username");

            out.print("Welcome " + str);

        %>

        </font>       

    </body>   

</html>

Step 10

Create another jsp file named "student.jsp" through which a normal user logged in is checked out.

student.jsp

<%@ page language="java" import="java.sql.*"%>

<html>

    <head></head>

    <body bgcolor="black"><font color="red">

        <h1>This is a User Page</h1>

        <%

            String str = (String) session.getAttribute("username");

            out.print("Welcome " + str);

        %>

        </font>

 

    </body>

</html>

Step 11

Now our project is ready to run.

Right-click on the Project menu then select "Run". The following output will be generated.

Output

Step 12

Now, first login as a normal user, for example:

UserName-rahul
Password-pass

User Login

User Page

Step 13

Now return and re-login using an Admin UserName and Password, in other words:

UserName-sandeep
Password-welcome

Admin Login

Admin User Page

Step 14

Now enter an incorrect UserName and Password in the login page; the following error message is generated and the page is redirected to the login page as in the following.

Login Through Invalid User

Error Page

Up Next
    Ebook Download
    View all
    Learn
    View all