Tuesday 12 May 2015

Simple Login

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>index page</title>
    </head>
    <body>
        <form action="Validate" method="post">
            <center>
                User Name : <input type="text" name="uname"><br><br>
                Password : <input type="password" name="pass"><br>
                <br>
                <input type="submit" value="Login">
            </center>
        </form>
    </body>
</html>


valid.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
       
        String name=request.getParameter("uname");
        String pass=request.getParameter("pass");
       
        if(pass.equals("admin") && name.equals("admin")){
            RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
            rd.forward(request, response);
        }
        else{
            out.println("<center><h2><font color='red'>Sorry! Wrong user name...</font></h2></center>");
            RequestDispatcher rd = request.getRequestDispatcher("index.html");
            rd.include(request, response);
        }
    }

welcome.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome</title>
    </head>
    <body>
        <% String name = request.getParameter("uname");
        out.println("Welcome "+name);%>
    </body>
</html>

No comments: