Wednesday, November 21, 2012

Login using JDBC,Servlet with NetBeans and Oracle

Step 1:Create Login table in Oracle Database 
Goto Run Sql Command Line and write following commands
a) Connect to HR schema
b) Create Login Table with three columns Id,Username and Password

c) Insert one Row values as Shown in Picture.
d)It is Very Important That You must commit insert because insert is not AutoCommit(DML).
If You'll not commit it.Then username and password not be found and It'll show Invalid User as output.

Step 2:
File->New Project->Java Web->Web Application
Here we are using Apache Tomcat 6.0 as Server.

Project Final Look After going through All Steps
Step 3: Adding Oracle Jar's
To add JAR files, Right click on Libraries Folder and goto Add JAR/Folder
Add ojdbc14.jar and ojdbc14_g.jar, to add these jars goto following location:

Step 4: Web-Pages->WEB-INF->web.xml
to open web.xml,double click on it and select xml view

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app version="2.5" xmlns="" xmlns:xsi="" xsi:schemaLocation="">  

Step 5: In index.jsp page in form tag logServlet is url pattern of page which we are passing in Action property which redirects index.jsp to

 <%@page contentType="text/html" pageEncoding="UTF-8"%>  
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     <title>JSP Page</title>  
     <form action="logServlet">  
      UserName:<input type="text" name="t1">  
      Password:<input type="password" name="t2">  
      <input type="submit" value="Login" >  

Step 6: Create by right click on Source Packages->New->Servlet

Here We are using Thin Driver, 1521 is port no. and XE stand for Oracle Express edition
First HR is Username and Second HR is Password of HR Schema. Our Login Table Exists in HR Schema.
 import javax.servlet.ServletException;  
 import javax.servlet.http.HttpServlet;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import java.sql.*;  
 public class logServlet extends HttpServlet {  
   protected void doGet(HttpServletRequest req, HttpServletResponse res)  
   throws ServletException, IOException {  
     String s1=req.getParameter("t1");  
     String s2 =req.getParameter("t2");  
     PrintWriter out=res.getWriter();  
       Connection connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hr","hr");  
       Statement st=connection.createStatement();  
       ResultSet rs=st.executeQuery("select * from login where uname='"+s1+"' and upass='"+s2+"'");  
         out.print("welcome!"+ rs.getString(2));  
       out.print("invalid User");  
     catch(Exception e){  

getParameter(String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
A connection with a specific database. SQL statements are executed and results are returned within the context of a connection.     

The object used for executing a static SQL statement and returning the results it produces.

A table of data representing a database result set, which is usually generated by executing a statement that queries the database.
A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row. The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set.     
Download code


Login Screen
Welcome Screen
In Address bar query you can see login values. you can hide these values by using method property in form tag in index.jsp page.Just you need to write method="post" and need to change method named doGet to doPost in page.


Yoke said...

how to print error msg in the same page without redirection?

Himanshu Bhasin said...

you can show error msgs on same page using javascript or ajax, check out these links

also checkout ajax validation with struts2

Popular Posts