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


 CREATE TABLE LOGIN(   
 ID NUMBER,  
 UNAME VARCHAR2(20),  
 UPASS VARCHAR2(20)  
 );  
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:
C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\

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

web.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
   <servlet>  
     <servlet-name>logServlet</servlet-name>  
     <servlet-class>logServlet</servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>logServlet</servlet-name>  
     <url-pattern>/logServlet</url-pattern>  
   </servlet-mapping>  
   <session-config>  
     <session-timeout>  
       30  
     </session-timeout>  
   </session-config>  
   <welcome-file-list>  
     <welcome-file>index.jsp</welcome-file>  
   </welcome-file-list>  
 </web-app>  

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

index.jsp
 <%@page contentType="text/html" pageEncoding="UTF-8"%>  
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
   "http://www.w3.org/TR/html4/loose.dtd">  
 <html>  
   <head>  
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
     <title>JSP Page</title>  
   </head>  
   <body>  
     <h1>Login</h1>  
     <form action="logServlet">  
      UserName:<input type="text" name="t1">  
      Password:<input type="password" name="t2">  
      <input type="submit" value="Login" >  
      </form>  
     </body>  
 </html>  

Step 6: Create logservlet.java 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.


logServlet.java
 import java.io.IOException;  
 import java.io.PrintWriter;  
 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 {  
     res.setContentType("text/html");  
     String s1=req.getParameter("t1");  
     String s2 =req.getParameter("t2");  
     PrintWriter out=res.getWriter();  
     try{  
       Class.forName("oracle.jdbc.driver.OracleDriver");  
       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+"'");  
       if(rs.next())  
         out.print("welcome!"+ rs.getString(2));  
       else  
       out.print("invalid User");  
       }  
     catch(Exception e){  
       out.print(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.
     
Connection
A connection with a specific database. SQL statements are executed and results are returned within the context of a connection.     

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

ResultSet
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

Output:

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 logServlet.java page.

2 comments:

Yoke said...

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

Blog Admin said...

you can show error msgs on same page using javascript or ajax, check out these links
http://webideaworld.blogspot.in/2012/12/validation-on-textfield-using.html

also checkout ajax validation with struts2
http://webideaworld.blogspot.in/2013/05/ajax-validation-using-struts2-framework.html

Popular Posts