Servlet Show Error And Success Message



fi_login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Filter</title>
        <style type="text/css">
            *{
                font-family: arial;
                font-size: 15px;
            }
        </style>
    </head>
    <body>
        <h1>Login Page</h1>
        <form method="post" action="LogIn">
            Username : <input type="text" name="username"/><br/>
            <p>${msg.username}</p>
            Password : <input type="text" name="password"/><br/>
            <p>${msg.userpass}</p>
            <p>${msg.error}</p>
            <input type="submit" value="Login" name="submit"/>
        </form>
    </body>
</html>

-----------------------------------------------------------
LogIn.java

package com.developer.controller;

import java.io.IOException;
import java.util.*;
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 LogIn extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String getUser = request.getParameter("username");
        String getPass = request.getParameter("password");
        Map<String, String> message = new HashMap<>();
        if (getUser.isEmpty()) {
            message.put("username", "Please type username");
        }
        if (getPass.isEmpty()) {
            message.put("userpass", "Please type password");
        }
        if (message.isEmpty()) {
            if (getUser.equals("demo") && getPass.equals("demo")) {
                message.put("error", "Login Success...");
            } else {
                message.put("error", "Username and Password invalid");
            }

        }
        request.setAttribute("msg", message);
        RequestDispatcher rd = request.getRequestDispatcher("fi_login.jsp");
        rd.forward(request, response);
    }
}

-------------------------
Output


Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment