Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /home/purvgwtw/public_html/tutorials/common.php on line 7
JSP-Servlet Login Action Script
Sponsered Links
Categories
Sponsered Links

JSP-Servlet Login Action Script

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import java.io.IOException;
import java.io.PrintWriter;

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

import util.DBConnection;


import java.sql.*;

/**
 *
 * @author Administrator
 */
public class LoginAction 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 {       
        try {
            boolean loginstatus = false;
            if (request.getParameter("username") != null
                    && request.getParameter("password") != null) {
                // Variables to collect user data from login form
                String usernameParam = request.getParameter("username").trim();
                String passwordParam = request.getParameter("password").trim();

                // Variables to collect user data from database
                String username = "";
                String password = "";
                if (!usernameParam.equals("")
                        && !passwordParam.equals("")) {
                                       
                    Connection con = null;                   
                    try{                     
                      con = new DBConnection().getConnection();
                      try{
                        Statement st = con.createStatement();
                        ResultSet res = st.executeQuery("select * from user where username='"+usernameParam+ "' and password='"+passwordParam+"'");
                       
                        while (res.next()) {                           
                            username = res.getString("username");
                            password = res.getString("password");                       
                        }
                        con.close();
                      }
                      catch (SQLException s){
                          if(con !=null)con.close();
                          System.out.println("SQL code does not execute.");
                      }   
                    }
                    catch (Exception e){
                        if(con !=null)con.close();
                      e.printStackTrace();
                    }                   
                    if (username.equals(usernameParam)
                            && password.equals(passwordParam)) {
                        loginstatus = true;
                        HttpSession httpSession = request.getSession(true);
                        httpSession.setAttribute("username", username);                       
                        response.sendRedirect("index.jsp");
                       
                    }
                }
            }
            if (loginstatus == false) {               
                response.sendRedirect("login.jsp?loginstatus=fail");
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    } // <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>
}

Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2014 PURVACOMPUTERS.COM All Right Reserved