Example of Check User Avalibility in JSP

Create Login table in Oracle 10g
When we create jsp form for Login user through data base, we require a table for checking user avalibity to stored data from data base. Here we create a 'Login ' Table in oracle 10g.
CREATE TABLE Login ( username varchar2(20) primary key, password varchar2(20), );
Example of User Avalibity from DataBase
  • DataBaseAccess.java
  • index.jsp
  • userChecking.jsp
package db; import java.sql.Connection; import java.sql.DriverManager; public class DataBaseAccess { static Connection conn=null; static{ String DRIVER="oracle.jdbc.driver.OracleDriver"; String CONNECTION_URL="jdbc:oracle:thin:@localhost:1521:xe"; String USERNAME="sunil"; String PASSWORD="sunil"; try { Class.forName(DRIVER); conn=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD); } catch(Exception e) { System.out.println(e); } } public static Connection getCon(){ return conn; } }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> <script language="javascript"> reqObj=null; function varify() { document.getElementById("res").innerHTML="Checking"; if(window.XMLHttpRequest) { reqObj=new XMLHttpRequest(); } else { reqObj=new ActiveXObject("Microsoft.XMLHTTP"); } reqObj.onreadystatechange=process; reqObj.open("POST", "userChecking.jsp? uname="+document.getElementById("uname").value,true); reqObj.send(null); } function process() { if(reqObj.readyState==4) { document.getElementById("res").innerHTML=reqObj.responseText; } } </script> </head> <body> <form> <table> <tr> <td> User Name : </td> <td> <input type="text" name="uname" id="uname" onkeyup="varify()"> </td> <td> <span id="res"> </span> </td> </tr> <tr> <td> Password : </td> <td> <input type="text" name="password" id="password" > </td> <td> </td> </tr> <tr> <td> <input type="submit" value="Submit" > </td> <td> <input type="reset" value="Reset" > </td> <td> </td> </tr> </table> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*,db.*;" %> <% String username=request.getParameter("uname"); int flag=0; try { Connection conn= DataBaseAccess.getCon(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("Select * from Login where username= '"+username+"'" ); while(rs.next()) { rs.getString(1); flag=1; } } catch(Exception e) { out.println(e); } if(flag==1) { response.getWriter().print("<font color='red'> Not Available </font>"); } else { response.getWriter().print("<font color='green'> Available </font>"); } %>

Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE


Latest Tutorial

Most Visited Tutorial