Registration and Login using servlet and JDBC
In this, we will be implementing the registration and login using JDBC and servlet.
Before diving deep, let us first give a look on what servlet is and what are its benefits?
“Servlets are the Java programs that runs on the Java-enabled web server or application server. They are used to handle the request obtained from the web server, process the request, produce the response, and then send response back to the web server.”
Pretty straight forward, now lets have a look on its benefits.
Benefits of Servlet:-
There are many advantages of Servlet over CGI. The web container creates threads for handling the multiple requests to the Servlet. Threads have many benefits over the Processes such as they share a common memory area, lightweight, cost of communication between the threads are low. The advantages of Servlet are as follows:
· Better performance: because it creates a thread for each request, not process.
· Portability: because it uses Java language.
· Robust: JVM manages Servlets, so we don’t need to worry about the memory leak, garbage collection, etc.
· Secure: because it uses java language.
Alright, lets have a look on HttpSession:-
“In web terminology, a session is simply the limited interval of time in which two systems communicate with each other. The session object is used to track a client session between client requests.”
An object of HttpSession can be used to perform two tasks:
1. bind objects
2. view and manipulate information about a session, such as the session identifier, creation time, and last accessed time.
As we also need to implement JDBC, lets checkout it once:-
Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is a Java-based data access technology used for Java database connectivity. It is part of the Java Standard Edition platform, from Oracle Corporation. It provides methods to query and update data in a database, and is oriented toward relational databases. A JDBC-to-ODBC bridge enables connections to any ODBC-accessible data source in the Java virtual machine (JVM) host environment.
Lastly, for database, we will use MySQL. So, lets have a look at it:-
MySQL is an open-source relational database management system (RDBMS).
A relational database organizes data into one or more data tables in which data types may be related to each other; these relations help structure the data. SQL is a language programmer use to create, modify and extract data from the relational database, as well as control user access to the database.
Setting up our project
What we need as our setup?
Nothing more, except these two :-
o Eclipse IDE (Eclipse IDE for Enterprise Java Developers)
o Apache Tomcat (for setting and starting up server)
o MySQL Server(For serving the MySQL database which will be used store user details)
Steps to create new project:-
1. Open Eclipse IDE.
2. Click on File -> New -> Dynamic Web Project. Following dialog box will appear.
3. We enter “login”(changeable) as our project name and select Apache Tomcat as our runtime.
4. Leaving all other settings to default and clicking on finish, we are ready with our project setup.
Folder Structure and files to edit:-
o For creating our project, our project needs some java files(for servlet), our webpages which will be served, and XML for servlet mapping and other informations.
o In eclipse, we will store our java files in a package in directory “Java Resources/src” , which here is named as com.divyalogin.
o Our web pages will be stored in a WebContent folder and web.xml in WebContent/WEB-INF folder.
o In addition to this, we also need to download and place sql-connector.jar in WebContent/WEB-INF/lib folder required to connect with mysql database
o After doing this, we will get our project directory something like this:-
Steps to create and setup MySQL database:-
1. If already not downloaded, we need to download and follow install instructions for MySQL for installing from https://dev.mysql.com.
2. After installing MySQL, we are all set to create our database.
3. First of all, open MySQL terminal and login using your username and password.
4. After that, use following command to create table:-
CREATE DATABASE register;
5. After this, we need to create table. To create table with name USERS and having columns name and password, use following commands:-
USE register;CREATE TABLE USERS(name VARCHAR(255),password VARCHAR(255));
6. Now, as we have created our database and tables, we are all set to start coding.
Coding Part
Done with the setup part, we will proceed to coding:-
Our files will be:-
1. index.html :- Homepage
2. register.html:- For registering the user
3. login.html:- Login page
4. lock.jsp:- Page which show current state of authentication
5. style.css:- For Styling
6. RegisterServlet.java:- For registering user and saving user details to database
7. LoginServlet.java:- Login validation and create session
8. LogoutServlet.java:- For logging out the user
9. web.xml:- For servlets mappings
Web Part (HTML and CSS)
1. index.html:- Homepage
2. register.html:- For registering the user
3. login.html:- Login Page
4. lock.jsp:- Page which show current state of authentication
5. style.css:- Styling of HTML pages
Java Servlets
Done with HTML and CSS, now we will create our servlets:-
6. RegisterServlet.java:-
7. LoginServlet.java:-
This checks the login details and make user authenticated corresponding to it.
8. LogoutServlet.java:-
This logouts the user given he was authenticated earlier.
9. web.xml
Running the Project
Done with all the coding, now its time to check our code running:-
To run the project in eclipse IDE we follow these steps:-
1. Select your project by clicked on its name directory in projects panel.
2. Click on Run -> Run as -> Run on Server.
3. Here, we will select Tomcat server, then click on next.
4. Here, our project will already be added in configured state, we can do this, if not done so by clicking on button.
5. Click on Finish.
Exploring our Project in web browser
- Open any web browser and hit to url http://localhost:8080/login.We can see our homepage (index.html) is served.
2. We can check the current status of the vault by clicking on “Check your vault” button. We can see it shows vault in locked state.
3. Click on return to home and then click on “Register” button. Here we will enter username and password to register. When Registration is successful, we get redirected to homepage with message that Registration is successful.
4. Click on “Log In” button. Here we will enter username and password to open our vault, which if we enter incorrect prompts us to enter correct one.
5. After providing right credentials, we are served page in which vault is open.
6. The vault will be open for the time until we close the vault or log out, which we can verify by returning to home and again checking the current vault state.
7. Now, if we log out or close our vault, our vault state changes to close state until we again log in.
8. As our all steps are complete, we can switch off our server by navigating to Window -> Show View -> Servers. Here right click on running server and click on stop.