Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //AuthenticateDAO.java
- package com.dao;
- import java.sql.*;
- public class AuthenticateDAO{
- public static final String qry =
- "select count(*) from Studetail where uname = ? pwd =?" ;
- public static String validate(String un1 , String pass1){
- Connection con =null;
- PreparedStatement ps = null;
- ResultSet rs = null;
- try{
- //Register Driver And Establish the Connection
- Class.forName("oracle.jdbc.driver.OracleDriver");
- //Establish Connection
- con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
- ps = con.prepareStatement(qry);
- //set param
- ps.setString(1,un1);
- ps.setString(2,pass1);
- System.out.println(qry);
- //Execute query
- rs = ps.executeQuery();
- //Process the resultset
- int cnt = 0;
- if(rs.next())
- cnt = rs.getInt(1);
- if(cnt==0)
- return "Invalid Credentials";
- else
- return "Valid Credentials";
- }//try
- catch(Exception e){
- e.printStackTrace();
- return "Internal Problem";
- }
- finally{
- try{
- if(rs!=null)
- rs.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- try{
- if(ps!=null)
- ps.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- try{
- if(con!=null)
- con.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- }//method
- }//class
- //;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- //LoginSrv.java
- package com.nt;
- import java . io.*;
- import javax.servlet.*;
- import javax . servlet.http.*;
- import com.dao.AuthenticateDAO;
- public class LoginSrv extends HttpServlet{
- public void doGet(HttpServletRequest req , HttpServletResponse res)
- throws IOException, ServletException{
- //General Setting
- PrintWriter pw = res.getWriter();
- res.setContentType("text/html");
- //read data from form
- String un = req.getParameter("user");
- String pass =req.getParameter("pwd");
- //Use DAO class persistence logic
- String result = AuthenticateDAO.validate(un,pass);
- //Display Result
- pw.println("Result is ::" + result);
- // pw.println(AuthenticateDAO.qry);
- //close the stream
- pw.close();
- }
- }
- //login.html
- <form action = "loginurl">
- Username : <input type = "text" name= "user"><br>
- Password : <input type = "text" name = "pwd">
- <input type = "submit" value = "Login">
- </form>
- //web.xml<web-app>
- <servlet>
- <servlet-name> abc </servlet-name>
- <servlet-class> com.nt.LoginSrv </servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name> abc </servlet-name>
- <url-pattern> /loginurl </url-pattern>
- </servlet-mapping>
- </web-app>
Advertisement
Add Comment
Please, Sign In to add comment