Hello Friends,
This is Part III of this jsp and servlet login tutorial. Check Part I and Part II here.
Step 8 ) We are done with the DAOs and Beans. Now we need to create a database from where we will get the authentication details like Username and Password. So, create a database named code2java and therein add a new Table called users. Please take care of the names you provide to table columns.
Step 9) Once you are done with the Database creation, we are only left with the Connection to it. So, we wil create a new java class for the Connection Manager which will provide the connection object to the DAO.
Under the package code2java, create a java class and name it as ConnectionManager and add the below code to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package code2java; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionManager { static Connection conn; public static Connection getConnection() { try { String url = "jdbc:mysql://localhost:3306/"; String dbName ="code2java"; // here "code2java" is the name of Database. String uname = "root"; String pwd = "root"; Class.forName("com.mysql.jdbc.Driver"); try { conn = DriverManager.getConnection(url+dbName,uname,pwd); } catch (SQLException ex) { ex.printStackTrace(); } } catch(ClassNotFoundException e) { System.out.println(e); } return conn; } } |
It will connect to the requested database and return the connection object. Here the database we will use is code2java, which we have created in the above step. Let’s assume the username and password to be “root”.
NOTE: Don’t forget to add the mysql-connection.jar file in the build path of this project.
Step 10) The most important file to be added is the web.xml which defines the redirection of the servlets. Add an xml file under the lib folder in WEB-INF folder, name it as web.xml and add the below code.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?xml version="1.0" encoding="UTF-8"?> <display-name>LoginExample</display-name> <servlet> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>code2java.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> </web-app> |
Here, we have defined the url pattern for running the application. At this stage your application is ready to run. Right click on the LoginPage.jsp and click on Run As –> Run On Server. Eclipse will run the application with the defined server or will ask you to define the one. Once the application starts execution you will first see the LoginPage:
Now you can test your application. Once you enter the details you will be navigated to the LoginSuccess page if the credentials you entered matched with the one stored in DB or else you will be navigated to LoginFailed page. Try this example and let me know if you face any difficulties in this.
You can download complete code and folder structure here LoginExample
Thanks,
Nikhil Naoghare
Nikhil,
I’ve been struggling with this type of problem for days, trying to execute a simple login/user verification routine. Your example code was the simplest I’ve found that allowed me to understand and then troubleshoot the code until I could succeed.
I’m using Eclipse/WTP/Apache Derby Plugin and Tomcat6 on a Linux platform. Following your instructions exactly, I was continually getting connection failures – first ClassNotFoundExceptions and then SQLExceptions, because the database could not be found. These are the changes I had to make to your ConnectionManager to get the code to successfully connect to the database and verify a user’s login:
[code]
// changed from “jdbc:mysql://localhost:3306/”
// then moved derby.jar to the /usr/share/tomcat6 directory,
// my tomcat6 installation directory
String url = “jdbc:derby:”;
// had to include the full pathname to the database. i know this
// should be simpler, but i’m tired of playing with it tonight.
String dbName = “/home/gregb/Documents/Programming/Workspaces/javaeetutorial/LoginExample/data/code2java;”;
// changed from “com.mysql.jdbc.Driver”
Class.forName(“org.apache.derby.jdbc.EmbeddedDriver”);
try
{
// changed from “url+dbName,uname,pwd”
conn = DriverManager.getConnection( url + dbName );
} [/code]
I’m thrilled that it now works, and I have a template to follow for similar applications I plan to write. I hope this is helpful to someone else.
Thanks again for your time.
I just added in lib folder jstl-1.2.jar and it works perfectly :D….tnx for this tutorial
that was a nice tutorial for the beginners.. works like charm…..
tht was a nice post… charm to learn as a beginner.. works perfectly…
ya very nice …bt dont v require struts-config.xls file???
Hi Sir, First of all thank u for such tutorial , I am doing some task that is based on Encryption and Decryption for secure message in jsp or servlet .