Skip to main content

Java servlet to check if the entered E-mail exists and the password of the E-mail is correct or not

Here is the Java Source Code:-

import java.awt.image.*;
import java.io.*;
import java.nio.file.*;
import java.util.*;
import javax.imageio.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class Login extends HttpServlet {

    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
        String email, password;
        email = req.getParameter("email");
        password = req.getParameter("password");
        if (!(email == null && password == null)) {
            login(email, password, req, res);
        }
    }

    private void login(String email, String password, HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        PrintWriter out = res.getWriter();
        boolean invalid = false;
        try {
            File acc = new File(System.getProperty("user.home"), "\\My Documents\\Accounts\\" + email.toLowerCase());
            File pas = new File(acc, "password.txt");
            BufferedReader buff = new BufferedReader(new FileReader(pas));
            String storedPassword = buff.readLine();
            if (storedPassword == null ? password == null : storedPassword.equals(password)) {
                logined(req, res, email);
            } else {
                invalid = true;
            }
        } catch (IOException e) {
            invalid = true;
        } finally {
            if (invalid == true) {
                Path uri = Paths.get("E:/NetBeans HTML/Servlets/ServletsExample/build/web/WEB-INF/Login Sign Up.html");
                Zucksnet ash = new Zucksnet("Zucksnet Style.css");
                out.println("<html>\n<head>");
                res.setContentType("text/css");
                out.println("<style>");
                String style[] = Zucksnet.getStyle();
                for (int i = 0; i < style.length; i++) {
                    out.println(style[i]);
                }
                out.println("</style>");
                out.println("<title>Login Failed!</title>\n</head>\n<body>");
                res.setContentType("text/html");
                out.println("Please Check Your Email or Password!");
                out.println("</body>\n</html>");
            }
        }
    }

    private void logined(HttpServletRequest req, HttpServletResponse res, String email) throws IOException {
        ImageServlet imageServlet = new ImageServlet();
        imageServlet.setName(email);
        PrintWriter out = res.getWriter();
        String pathToWeb = getServletContext().getRealPath(File.separator);
        Scanner scan;
        File file = new File(pathToWeb, "Zucksnet Style.css");
        scan = new Scanner(file);
        res.setContentType("text/html");
        out.println("<html>\n<head>\n<title>\nLogin\n</title>\n<style>");
        res.setContentType("text/css");
        while (scan.hasNextLine()) {
            out.println(scan.nextLine());
        }
        out.println("</style>");
        file = new File(pathToWeb, "home_script.txt");
        scan = new Scanner(file);
        while (scan.hasNextLine()) {
            out.println(scan.nextLine());
        }
        file = new File(pathToWeb, "head.txt");
        scan = new Scanner(file);
        res.setContentType("text/html");
        out.println("</head>\n<body id=\"homePage\" text=\"black\">");
        while (scan.hasNextLine()) {
            out.println(scan.nextLine());
        }
        out.println("<br><br>");
        file = new File(pathToWeb, "body.txt");
        scan = new Scanner(file);
        while (scan.hasNextLine()) {
            out.println(scan.nextLine());
        }
        out.println("</body>\n</html>");
    }
}



HTML Source Code:-


<!DOCTYPE html>
<html>
    <head>
        <title>Login</title>
    </head>
    <body id="mobile" style="text-align: left; background-color: #294a8f; font-family: sans-serif; color: white;" onload="links(); buttons(); mobiles(); dob();">
        <div align="left">
            <form method="post" action="http://localhost:8084/ServletsExample/build/web/WEB-INF/classes/Login">
                <table>
                    <tbody>
                        <tr>
                            <td><label for="email">E-mail</label></td><tr>
                            <td> <input name="email" type="email" id="email" autocomplete="off" placeholder="E-mail" autofocus style="font-kerning: auto; border-color: #294a8f; font-weight: bolder; background-color: #3B5998; color: white; text-spacing: none" size="32"/><tr>
                            <td><label for="password">Password</label></td><tr>
                            <td> <input name="password" type="password" id="password" autocomplete="off" placeholder="Password" style="font-kerning: auto; border-color: #294a8f; font-weight: bolder; background-color: #3B5998; color: white" size="32"/><tr>
                            <td><label for="remainLoggedIn" title="Remain Logged In on this Computer. Not recomonthended if using an shared Computer">
                                    <input type="checkbox" id="remainLoggedIn" title="Remain Logged In on this Computer. Not recomonthended if using an shared Computer" style="appearance: checkbox; background-color: #3B5998; color: white"/>Remember me?</label><tr>
                            <td><button title="Login to your account" type="submit" style="background-color: #294a8f; border-color: #2C4272; appearance: push-button; color: white;">Login</button>
                    </tbody>
                </table>
            </form>
    </div>
    </body>
</html>

Comments

Popular posts from this blog

Java Program to calculate the Run Rate per over in a cricket match

import java.io.*; import java.util.*; public class RunRate{     Scanner scan=new Scanner(System.in);     int runs, balls;     float runRate;     public void input(){         try{             System.out.println("Enter Runs Scored: ");             runs=scan.nextInt();             System.out.println("Enter Balls Delivered: ");             balls=scan.nextInt();         }         catch(NumberFormatException e){             System.out.println("Error Code: "+e);             System.exit(0);   ...

Vanilla Javascript each()

JQuery's each() is very useful when iterating through elements. But you don't want to use JQuery in your project you can simply add the following javascript code which works somewhat similar to the JQuery's each function. Here the fnc parameter is the function string which is converted to a valid function call replacing all the $(this) with this /**  * This function binds a particular function to every element with the specified selector. It is somewhat same as JQuery's each() with less functionality  * @param {String|DOMElement} selector  * @param {Function} fnc  */ function each(selector, fnc) {     var elem;     if (typeof selector === "string") {         elem = $_(selector);     } else {         elem = selector;     }     fnc = (fnc.toString().replace("$(this)", "elem") + "();").replace("function () {", "").replac...

Java Program to display Welcome Message

import java.io.*;// I/O package imported. public class Welcome{        //class name is "Welcome"     public Welcome(){      //constructor declaired to print the message.         System.out.println("Welcome to Java Programming Language!");/* System.out.println is used for output. Welcome Message is written within " ".*/     }//display() closes here.     public static void main(String[] args){        //main() is declaired to declair an object in it.         Welcome obj=new Welcome();  //Object "Obj" is bean created.     }//main() closes. }//class "Welcome" ends here. Above program displays the message which is written by you in " ".  In programs "/*" and "*/" are use for multiple line comment(s) and "//" is use for single line comment. Code line "Welc...