Now lets convert our dumb login program to an intelligent login
program which connects to the data base and validates the user name and
password before spitting out the response. We will use open source database
My SQL. You can use any other database you are comfortable with.
This tutorial section uses the general Data Source and pooling provided
by Tomcat. Alternatively you can also use Struts datasources which is
not very popular and is not covered in this tutorial.
Create a database firing the following query in the My SQL client
create database DOC_MANAGER;
Create a table to store login data
CREATE TABLE CMS_USER(
ID INT AUTO_INCREMENT,
USER_NAME VARCHAR(80),
PASSWORD VARCHAR(80),
CONSTRAINT CMS_USER_PK PRIMARY KEY (ID)
);
Insert values in the table
insert into cms_user (user_name, password) values ('admin', 'admin123');
Configuring My SQL in Tomcat Server
Download official JDBC driver for My SQL at dev.mysql.com/downloads/
Copy downloaded My SQL driver to <Tomcat Home>\webapps\lib.
Change the UserLoginAction.java to accommodate the login checking code.
We will use the general database access methodology we used in Tomcat.
<html>
<head>
<title>
Login Status
</title>
</head>
<body>
Wrong User Name or Password
</body>
</html>
To check your login program fire URL http://localhost:8080/salsa-tutorial/loginForm.jsp
in your browser.
For simplicity this tutorial has database access code in the execute
method of the UserLoginAction.java. However in real time systems this
code goes into separate classes called DAO (Data Access Object).