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);   ...

Java Program to calculate the Strike Rate of a Cricket Batsman

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

WP similar or related posts widget without using any plugin

Hi guys, Recently I was working on some WP website and my client told me that he required a widget for displaying related/similar posts on the single post page. But as his website was already using many plugins, even for some pretty small tasks like this one, I decided not to use another WP plugin (plugins are not good for your WP websites, we will discuss about that on some other post.) I am not explaining the code as it is pretty simple if you are familiar with WP classes. But please let me know if you have any questions related to the PHP code posted below in the comments section or even much better, on Gist. You can add the following code directly in your child theme's functions.php file or you can create a separate file and include this at the bottom of functions.php file. <?php class similar_posts_widget extends WP_Widget {     function __construct()     {         parent::__construct('similar_posts_...