rajamit872

Servlet Problem

Apr 3rd, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. //AuthenticateDAO.java
  2.  
  3. package com.dao;
  4. import java.sql.*;
  5.  
  6. public class AuthenticateDAO{
  7. public static final String qry =
  8.     "select count(*) from Studetail where uname = ? pwd =?" ;
  9.    
  10.    
  11.     public static String validate(String un1 , String pass1){
  12.          Connection con =null;
  13.        PreparedStatement ps = null;
  14.        ResultSet rs = null;
  15.     try{
  16.         //Register Driver And Establish the Connection
  17.          Class.forName("oracle.jdbc.driver.OracleDriver");
  18.  
  19.         //Establish Connection
  20.         con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
  21.  
  22.             ps = con.prepareStatement(qry);
  23.             //set param
  24.             ps.setString(1,un1);
  25.             ps.setString(2,pass1);
  26. System.out.println(qry);
  27.  
  28.         //Execute query
  29.         rs = ps.executeQuery();
  30.  
  31.         //Process the resultset
  32.         int cnt = 0;
  33.  
  34.         if(rs.next())
  35.             cnt = rs.getInt(1);
  36.        
  37.         if(cnt==0)
  38.             return "Invalid Credentials";
  39.         else
  40.             return "Valid Credentials";
  41.    
  42.     }//try
  43.     catch(Exception e){
  44.         e.printStackTrace();
  45.         return "Internal Problem";
  46.     }
  47.     finally{
  48.         try{
  49.             if(rs!=null)
  50.                 rs.close();
  51.         }catch(Exception e){
  52.         e.printStackTrace();
  53.         }
  54.  
  55.         try{
  56.             if(ps!=null)
  57.                 ps.close();
  58.         }catch(Exception e){
  59.         e.printStackTrace();
  60.         }
  61.        
  62.         try{
  63.             if(con!=null)
  64.                 con.close();
  65.         }catch(Exception e){
  66.         e.printStackTrace();
  67.         }
  68.    
  69.     }
  70.    
  71.     }//method
  72. }//class
  73. //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  74. //LoginSrv.java
  75. package com.nt;
  76. import java . io.*;
  77. import javax.servlet.*;
  78. import javax . servlet.http.*;
  79. import com.dao.AuthenticateDAO;
  80.  
  81. public class LoginSrv extends HttpServlet{
  82.     public void doGet(HttpServletRequest req , HttpServletResponse res)
  83.                 throws IOException, ServletException{
  84.         //General Setting
  85.         PrintWriter pw = res.getWriter();
  86.         res.setContentType("text/html");
  87.  
  88.         //read data from form
  89.         String un =  req.getParameter("user");
  90.         String pass =req.getParameter("pwd");
  91.  
  92.         //Use DAO class persistence logic
  93.         String result = AuthenticateDAO.validate(un,pass);
  94.  
  95.         //Display Result
  96.         pw.println("Result is ::" + result);
  97.  
  98.     //  pw.println(AuthenticateDAO.qry);
  99.    
  100.         //close the stream
  101.         pw.close();
  102.  
  103.     }
  104. }
  105.  
  106. //login.html
  107. <form action = "loginurl">
  108. Username : <input type = "text" name= "user"><br>
  109. Password : <input type = "text" name = "pwd">
  110. <input type = "submit" value = "Login">
  111. </form>
  112.  
  113. //web.xml<web-app>
  114.   <servlet>
  115.     <servlet-name> abc </servlet-name>
  116.     <servlet-class> com.nt.LoginSrv </servlet-class>
  117.   </servlet>
  118.  
  119.    <servlet-mapping>
  120.       <servlet-name> abc </servlet-name>
  121.       <url-pattern> /loginurl </url-pattern>
  122.    </servlet-mapping>
  123. </web-app>
Advertisement
Add Comment
Please, Sign In to add comment