Sunday, 8 February 2015

Login Application

Sample View

Login Page

 If Login Fail

If Login Success


 Development

1) index.html

 

<form action="/LoginApplication/login">
Name:<input type="text" name="user" value="Ram"/>
Password:<input type="password" name="pwd" value="Ram"/>
<input type="submit" value="Login">
</form>
 

 

2)success.html

 

Login Success<br>
<a href="./index.html">Login</a>

 

3)fail.html

Login Failure<br>
<a href="./index.html">Login</a>

 

4)LoginServlet.java

 

package com.ram;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
   
    RequestDispatcher rd = null;
    boolean status;
      
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        String user = request.getParameter("user");
        String pwd = request.getParameter("pwd");
        UserDTO userDTO = new UserDTO();
        userDTO.setName(user);
        userDTO.setPwd(pwd);
       
        UserDAO userDAO = new UserDAO();
        status = userDAO.checkLogin(userDTO);
        if(status) {
            rd = request.getRequestDispatcher("success.html");
            rd.forward(request, response);
        } else {
            rd = request.getRequestDispatcher("fail.html");
            rd.forward(request, response);
        }
       
       
    }

}
 

 

5)UserDAO.java

 

package com.ram;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UserDAO {
   
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    boolean status;
    String name = null;
    String pwd = null;
   
    public boolean checkLogin(UserDTO userDTO) {
       
        name = userDTO.getName();
        pwd = userDTO.getPwd();
       
        try {
           
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","ram","ram");
            stmt=con.createStatement();
            rs=stmt.executeQuery("select * from users where NAME='"+name+"' and PWD='"+pwd+"'");
           
            if(rs.next()) {
                status = true;
            }
           
           
        } catch (SQLException e) {
            e.printStackTrace();
        }
       
        return status;
       
        }
    }

 

 

6)UserDTO.java

 

package com.ram;

public class UserDTO {
   
    String name;
    String pwd;
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
   
   

}
 

7)web.xml


<web-app>
  <display-name>LoginApplication</display-name>


  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>


  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.ram.LoginServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>


</web-app>

  8)Use ojdbc14.jar

9)Deployment


1 comment: