java tutorials and useful tips for developer.

Friday, 10 April 2015

login form creation code using database and java.

you must created table with name admin with attribute name and pass,then copy this code and enjoy the login form using jsp.

code for form in html and css
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package StudentSystem;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Faraz
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     
         Connection conn=null;
          String image=null;
                             String name=null;
                              String occup=null;
                               String batch=null;
                             ResultSet rs;
        String uname=request.getParameter("name");
        String pass=request.getParameter("pass");
       
       
           try {
         
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SaveStudentServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student_data","root","badmash");


         String sql="select * from admin where name='"+uname+"' and pass='"+pass+"' ";
         //String sql2="insert into student (id,fname) values (5,'zeeshan')";
        rs= conn.createStatement().executeQuery(sql);
        boolean status=rs.next();
        if(status==true){
                               
                                   image=rs.getString("image");
                                    name=rs.getString("name");
                                     occup =rs.getString("occupation");
                                      batch =rs.getString("batch");
                                     
                                 
                             
             Admin ad=new Admin(name,occup,batch,image);  
           
                            AdminCopy.setAbo(ad);
                            request.setAttribute("admin", ad);
                           
                            request.getRequestDispatcher("dashboard.jsp").forward(request, response);
           
        }
        else{
           
           request.setAttribute("error","Username/Password Incorrect");
           request.getRequestDispatcher("login.jsp").forward(request, response);

        }
         
        } catch (SQLException ex) {
            Logger.getLogger(SaveStudentServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            try {
             
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(SaveStudentServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}


code for handling request coming from this form after clicking the submut button.




/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package StudentSystem;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Faraz
 */
@WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     
         Connection conn=null;
          String image=null;
                             String name=null;
                              String occup=null;
                               String batch=null;
                             ResultSet rs;
        String uname=request.getParameter("name");
        String pass=request.getParameter("pass");
       
       
           try {
         
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(SaveStudentServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student_data","root","badmash");


         String sql="select * from admin where name='"+uname+"' and pass='"+pass+"' ";
        rs= conn.createStatement().executeQuery(sql);
        boolean status=rs.next();
        if(status==true){
                               
                                   image=rs.getString("image");
                                    name=rs.getString("name");
                                     occup =rs.getString("occupation");
                                      batch =rs.getString("batch");
                                     
                                 
                             
             Admin ad=new Admin(name,occup,batch,image);  
           
                            AdminCopy.setAbo(ad);
                            request.setAttribute("admin", ad);
                           
                            request.getRequestDispatcher("dashboard.jsp").forward(request, response);
           
        }
        else{
           
           request.setAttribute("error","Username/Password Incorrect");
           request.getRequestDispatcher("login.jsp").forward(request, response);

        }
         
        } catch (SQLException ex) {
            Logger.getLogger(SaveStudentServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            try {
             
                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(SaveStudentServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
            }
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}



0 comments:

Post a Comment

Categories

Popular Posts

Live Stats

Powered by Blogger.