November 17, 2007

JSP for Absolute Dummies: JDBC connectivity in JSP

Using Java Server Pages, simulate a simple User Validation scenario in which the user name, password is stored in a database

Login Page



Successful Login


Design of the Application:

The whole application consists of 3-JSP pages and a database created in Java DB - 'user-validate' :
  1. index.jsp - login page
  2. validate.jsp - validating the credentials
  3. user.jsp - User's page after succesfull login
  4. user-validate
    • Contains the table USERDATA which consists of the fields:
      • name
      • pass

Flow of control / Application Flow:
  1. User enters credentials in the login page, hits the 'Submit' button
  2. 'validate.jsp' is invoked via FORM action, GET method
  3. The Username/Password is compared with values obtained from the database in 'validate.jsp'
  4. On success, the 'user.jsp' page is invoked using tag else control returns back to 'index.jsp'
Software Used:
  1. Sun JDK 1.6
  2. Apache Tomcat 6.0.13
  3. Java DB (shipped with NetBeans 6 Beta 1)
  4. IDE
    1. NetBeans 6 Beta 1
Source Code:
Running the code:
  1. Open the project in NetBeans 6 Beta 1 (or M10 onwards)
  2. Add the 'Java DB libraries' (By choosing 'Add Library')
  3. Start Java DB
  4. Create a Database 'user-validate'
  5. Connect to it (Right-click and..)
  6. Create a table 'USERDATA', in the schema 'APP' with 2-columns - NAME, PASS, both VARCHAR
  7. Insert into the table values - 'Amit', 'amit' using a simple SQL script
  8. Observe the Database specific address, port and adjust the code in 'validate.jsp'
  9. Build the project
  10. Run it
If you are not aquainted with Java DB, refer this link.



Major Shortcomings:
  • No Session management

4 comments:

Aishwarya said...

hello

im a novice at java. Im having great trouble with finding instruction for remote login using the whole jdbc odbc to connect to mysql.
the documentation available is boggling. If you could put up some step by step instructions on that and other similar stuff it would be wonderful.
Also do u mind mailing me the links and letting me know if you happen to put these up?

Amit Kumar Saha said...

@aishwarya,

for connection to MySql you won't need the JDBC-ODBC driver, you will need only the JDBC driver.

Besides, what exactly do you mean by "remote login"?

You may mail me.

प्रभात नायक said...

Sir,
I am an MCA final year student.I am very much interested to build a career with java.So I want to learn java throughly.The problem is no one is here to personnally guide me.So I think what I have to do is to learn by myself by keeping some good books with me.Can you please suggest me some good books of Servlet,Jsp,Strut and whatever technology you feel is necessary that'll be good enough for a beginner?

litmus said...

Hi amit,
Really god stuff u have here. thanks alot i find it very usefull.
i am currently running a project that i am supposed to build xml amd wml dynamically from a mysql database. i'll use data extracted from a mysql table, then detect a http header( wether it is a mobile browser or an internet browser) to build wml or xml and using predefined xsl to present the data to the right client.
I am having a had time with these especially using the java xml builder can you provide some help?
I'll really appreciate it
thanks.
my email is engrliti@yahoo.com

Post a Comment