Friday, September 20, 2013

Creating a Simple Login Form using JSPs and Servlets in Eclipse - Part 3

Hi Guys,

    In Part 2, we came to know that how to create LoginServlet.java to let the LoginForm.jsp works which we created in Part 1, but left with the database setup process that we are going to see now.
    As we said, we use MySQL database for this login application development. MySQL is the most used open source RDBMS for its speed, ease of use, portability, small size, performance and rich features.


Step 1: Download and Configure MySQL

    Download the MySQL database from here. You can download either "Windows (x86, 32-bit), ZIP Archive" or "Windows (x86, 64-bit), ZIP Archive " depending on your machine configuration whether its 32 or 64 bit processor. Unzip the zip file (mysql-5.6.13-winx64.zip) into your machine at some location(ex: D:\GAP\GaneshTechBlog), then MySQL will be unzipped as "D:\GAP\GaneshTechBlog\mysql-5.6.13-winx64".

     Open the file 'my-default.ini' (ex: D:\GAP\GaneshTechBlog\mysql-5.6.13-winx64\my-default.ini) with some text editor like notepad and search for the entries basedir and datadir. Set the path of "MySQL installed directory" as "basedir" and set the path of "data of MySQL installed directory" as "datadir" and remove the '#' symbol before them.
ex.,
basedir = D:/GAP/GaneshTechBlog/mysql-5.6.13-winx64
datadir = D:/GAP/GaneshTechBlog/mysql-5.6.13-winx64

Step 2: Start the MySQL server

    MySQL is a client-server application and database runs as a server application. Users can access the database server through client application. The server application is known as "mysqld" and the client application is known as "mysql".

To start the MySQL server,
1. open the command prompt by selecting "windows start --> run" and give cmd.
2. go to the bin directory of "basedir" path (ex. cd /D D:\GAP\GaneshTechBlog\mysql-5.6.13-winx64\bin)
3. run the command "mysqld --console" on command prompt.

MySQL database server is started now and you will see the server output on the console like below:




Note: You can run the command "mysqld" with out "--console" option then the server output doesn't get printed on console. Blank screen will appear.

To stop the MySQL server,

1. open a new command prompt by selecting "windows start --> run" and give cmd.
2. go to the bin directory of "basedir" path (ex. cd /D D:\GAP\GaneshTechBlog\mysql-5.6.13-winx64\bin)
3. run the command "mysqladmin.exe -u root shutdown" (MySQL creates an user "root" with no password during installation).

Note: But don't stop the database server now. We are going to test our application soon with this. If you have already stopped the server then start it again by following the steps from "To start the MySQL server".

Now, we are ready with a database server. All that we need to do with this is to create a database and tables here.

Step 3: Start a MySQL client

    Once the MySQL server started, one or more clients can be started and connected to the database server as MySQL is a client-server application.

To start a MySQL client,1. open a new command prompt by selecting "windows start --> run" and give cmd.
2. go to the bin directory of "basedir" path (ex. cd /D D:\GAP\GaneshTechBlog\mysql-5.6.13-winx64\bin)
3. run the command "mysql -u root" on command prompt.

Now, client will be started with the super user "root" and you will see the below messages on command prompt.



To create a database "myDb",
Run the command "create database myDb;" in mysql prompt, here 'myDb' is database name. To see the created databases in the server, you can use the command "show databases;". It will show all the databases including MySQL system generated like below.


To create a table with the name "users", run the below commands with out quotes,
1. "use myDb;"
2. "create table users(username varchar(50), password varchar(50), firstname
varchar(50), lastname varchar(50));"
You can see the "users" table description with the command "desc users;" like below.


Now, lets create data in the table "users". To create data, run the following commands with out quotes,
1. "insert into users values('akshay','virat123','vikram','rathod');"
2. "insert into users values('salman','chande123','chulbul','pandey');"

You can view the data in "users" table by running the command "select * from users;"


Now, we can run the "LoginForm.jsp" by right clicking on the file and selecting "Run As" --> "Run on Server". Click on "Next --> Finish". Server will be started now and login page will be opened at the url "http://localhost:8080/Login/LoginForm.jsp" in the eclipse window. You can copy this url and paste into any web browser's address bar then you can see the login page.

Enter "akshay" in the UserId field and "virat123" in the  Password field as we know this data is already stored in the database and click on "Submit", then we will get "HTTP Status 404 - /Login/LoginSuccess.jsp" error page as the requested resource (LoginSuccess.jsp) is not available. This is the same case even we give wrong data in the login form. Since here too, we do not have the "LoginFailure.jsp" file, so we will end up with the 404 error page again. Lets create those two pages in next section.


7 comments:

  1. Great work Ganesh, really you have given a good example with screen shot which even a lay man will understand

    ReplyDelete
  2. Still am Getting HTTP Status 404 - /Login/LoginServlet
    Error...
    Please help me what is the problem???

    ReplyDelete
  3. so how would u create the servelet if u are connected to oracle database?

    ReplyDelete
  4. And Finally i got :----

    Here are the solutions:
    changing "bind-address" attribute
    Uncomment "bind-address" attribute or change it to one of the following IPs:

    bind-address="127.0.0.1"

    or

    bind-address="0.0.0.0"

    ReplyDelete
  5. where this bind-address attribute is available and how to set that?

    ReplyDelete
  6. Hey. I am getting an error "No suitable driver found for jdbc:mysql://localhost:3306/myDb". Can someone help me to rectify that. I followed all the steps like u said but got this error.

    ReplyDelete
  7. Thank You Soo Munch!
    Valuable Info.

    ReplyDelete