<!--  Author : Jagbir Singh -->
<!--  Debug : Amanbir Singh -->
<!--  Tested by : Ajinkya -->

 <%
    response.setHeader("Cache-Control","no-store");
    response.setHeader("Pragma","no-cache");
    response.setDateHeader ("Expires", 0);
    %>
    
  <%@ page language="java" import="java.sql.*, javax.sql.DataSource, javax.naming.*" errorPage="" %>
<%
DataSource pool;
int flag =0;

  Connection conn = null;

  PreparedStatement pstmt = null;
 
  String usrId = request.getParameter("sUserName");
  String pswd1 = request.getParameter("sPwd");
  String radioInJSP = request.getParameter("active");
  String loginQuery = null;
  try{
	// Create a JNDI Initial context to be able to lookup the DataSource
      InitialContext ctx = new InitialContext();
      // Lookup the DataSource, which will be backed by a pool
      //   that the application server provides.
      pool = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
      if (pool == null)
         throw new SQLException("Unknown DataSource 'jdbc/TestDB'");
	   
   	  // Get a connection from the pool
      conn = pool.getConnection();

      
      if(radioInJSP.equals("actpatient"))
       loginQuery = "SELECT * from userInfo where user_Id = ? and password = ?";
      else
    	 loginQuery = "SELECT * from drInfo where doc_Id = ? and password = ?";
      
      pstmt = conn.prepareStatement(loginQuery);

      pstmt.setString(1, usrId);
      pstmt.setString(2, pswd1);
      ResultSet rs = pstmt.executeQuery();
      if(rs.next())
      {										// 			for maintaing session of user
    	  session.setAttribute("sUserName",null);
    	  session.setAttribute("sPwd",null);
    	  session.setAttribute("active", null);
    	  session.setAttribute("sUserName",usrId);
    	  session.setAttribute("sPwd",pswd1);
    	  session.setAttribute("active", radioInJSP);
    	  flag=1;
      }
      rs.close();
      pstmt.close();
      conn.close();
      
  }catch(SQLException se){
      //Handle errors for JDBC
      se.printStackTrace();
   }catch(Exception e){
      //Handle errors for Class.forName
      e.printStackTrace();
   }finally{
      //finally block used to close resources
       // nothing we can do
      try{
          if(pstmt!=null)
             pstmt.close();
       }catch(SQLException se2){
       }// nothing we can do
      try{
         if(conn!=null)
         conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }//end finally try
   } //end try
	if(flag==1 && radioInJSP.equals("actpatient"))
		   pageContext.forward("welcome.jsp");
   else if(flag==1 && radioInJSP.equals("actdoctor"))
	   pageContext.forward("welcome.jsp");
			else
			pageContext.forward("errLogin.jsp");
%>
	
}
