package ForecastingPkg; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.text.*; import java.sql.*; public class ValidateUser extends HttpServlet { /* * Initialize global variables */ String id="",name=""; Connection conn; Statement st; String username=""; /* Initialize the servlet. This is called once when the servlet is loaded * It is guaranted to complete before any requests are made to the servlet * param config Servlet configuration information */ public void init(ServletConfig config) throws ServletException { super.init(config); } /* Connects to the Oracle Database */ public Connection connectToOracle () { try { //Class.forName("oracle.jdbc.driver.OracleDriver"); //String url = "jdbc:oracle:thin:@208.220.122.19:1525:M3TEST"; //conn = DriverManager.getConnection(url,"forecast", "forecast"); String driver = "sun.jdbc.odbc.JdbcOdbcDriver"; // use Jdbc-Odbc-Bridge String url = "jdbc:odbc:ayedduladb"; // as defined in the ODBC Data Source Administrator Class.forName(driver); // load the driver class conn = java.sql.DriverManager.getConnection(url, null, null); // connect to database System.out.println("Connection Established" ); } catch(ClassNotFoundException ce) { System.out.println("Class Not Found Exception" +ce.getMessage()); } catch (SQLException anSQLException) { anSQLException.printStackTrace(); } return conn; } /* Process the HTTP Get request */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ username = request.getParameter("employeeName"); response.setContentType("text/html; charset=WINDOWS-1252"); PrintWriter out = response.getWriter(); if (validUserName(username)){ out.println(""); out.println(""); out.println("CustomerFetch"); out.println(""); out.println(""); out.println(""); out.println("

The Current User is "+ name +" user's id is "+id ); out.println("



"); out.println("

"); out.println(""); out.println(""); } else { out.println("
Not a Valid User"); } out.close(); } catch(Exception e){} } private boolean validUserName(String username) { boolean valid = false; //HttpServletRequest request = null ; //HttpServletResponse response = null; String login = "",query = ""; System.out.println("username = " + username); if(username != null) { try { conn = connectToOracle (); //username = request.getParameter("employeeName"); st=conn.createStatement(); query = "select employeeName, phoneNumber From employee Where employeeName='"+username+"'"; System.out.println("Query = " + query); ResultSet rs = st.executeQuery(query); while(rs.next()) { id = rs.getString(1); name = rs.getString(2); valid = username.equalsIgnoreCase(id); } System.out.println("valid = " + valid + "id = " + id + "name = " + name); //System.out.println("valid = " + valid ); rs.close(); st.close(); conn.close(); } catch(Exception e){} } return valid; } // Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } /* Destroy the servlet. * This is called once when the servlet is unloaded. */ public void destroy() { try { conn.close(); System.out.println("Connection Closed" ); } catch (SQLException ex) { System.out.println ("\n*** SQLException caught ***\n"); } } /* Get Servlet information * @return java.lang.String */ public String getServletInfo() { return "ForecastingPkg.ValidateUser Information"; } }