Program to validate a user using JSP - GeeksforGeeks

12 Jun, 2018
Introduction to JSP : JSP(Java Server Page) is a server-side technology, used for developing webpages that support dynamic content. It enables the separation of dynamic and static content, thereby reducing development complexity. Developers are thus, armed with the power to insert java code in HTML pages by employing special JSP tags most of which start with <% and end with %>. It may further be noted here that JSP’s are built on top of Java Servlet API and also allow tags that begin with 
<jsp:name_of_tag>
and end with 
</jsp:name_of_tag>
Tags thus determine how the code within them will behave.
Here the power of JSP will be harnessed in order to Validate a user from his username and password. The user will initially be entering his username and password in a JSP form provided. The data will then be passed to another JSP to get the java bean object from the given scope or create a new object of java bean. The bean properties will then be set using the form data and verified using another java class. Finally, the result of verification will be displayed.
In this, the JSP Action tags are used to achieve the above purpose. JSP tags are specifically used during request processing. The tags used here will be as follows :jsp:useBean: It will be used to create the java bean and instantiate it.jsp:setProperty: It will be used to set the property of the created bean, using the form data.jsp:getProperty: It will be used to display the details entered.
It may be noted here that, all actions tags use the id attribute to uniquely identify an action element and refer to it inside the JSP page.The program has been tested on Netbeans IDE 8.1 using Apache Tomcat as the application server.
Steps to Validate a User:
We click the link on index.html page to deploy the application.We are then presented with a form, where we enter username and password and click submit.The JSP gets automatically called and it returns the data entered in the form and the result of Validation.
We click the link on index.html page to deploy the application.
We are then presented with a form, where we enter username and password and click submit.
The JSP gets automatically called and it returns the data entered in the form and the result of Validation.
Form to accept username and password : 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>Login Page</title>    </head>    <body>        <h1>User Details</h1>        <%-- The form data will be passed to acceptuser.jsp              for validation on clicking submit        --%>         <form method ="get" action="acceptuser.jsp">            Enter Username : <input type="text" name="user"><br/><br/>            Enter Password : <input type="password" name ="pass"><br/>                <input type ="submit" value="SUBMIT">            </form>    </body></html>     
JSP to accept form data and verify a user : acceptuser.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html>    <head>       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">       <title>Accept User Page</title>    </head>    <body>        <h1>Verifying Details</h1>        <%-- Include the ValidateUser.java class whose method              boolean validate(String, String) we will be using        --%>        <%-- Create and instantiate a bean and assign an id to              uniquely identify the action element throughout the jsp        --%>        <jsp:useBean id="snr" class="saagnik.ValidateUser"/>                  <%-- Set the value of the created bean using form data --%>        <jsp:setProperty name="snr" property="user"/>        <jsp:setProperty name="snr" property="pass"/>                  <%-- Display the form data --%>        The Details Entered Are as Under<br/>        <p>Username : <jsp:getProperty name="snr" property="user"/></p>        <p>Password : <jsp:getProperty name="snr" property="pass"/></p>                  <%-- Validate the user using the validate() of              ValidateUser.java class        --%>        <%if(snr.validate("GeeksforGeeks", "GfG")){%>            Welcome! You are a VALID USER<br/>        <%}else{%>            Error! You are an INVALID USER<br/>        <%}%>      </body></html>
The ValidateUser.java class
package saagnik;import java.io.Serializable;  // To persist the data for future use,// implement serializablepublic class ValidateUser implements Serializable {    private String user, pass;      // Methods to set username and password     // according to form data    public void setUser(String u1) { this.user = u1; }    public void setPass(String p1) { this.pass = p1; }      // Methods to obtain back the values set     // by setter methods    public String getUser() { return user; }    public String getPass() { return pass; }      // Method to validate a user    public boolean validate(String u1, String p1)    {        if (u1.equals(user) && p1.equals(pass))            return true;        else            return false;    }}
Outputs:login.jsp
Next on clicking ‘SUBMIT’ button following page is generated.acceptuser.jsp
Java-JSP
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
Initialize an ArrayList in Java
ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Convert a String to Character array in Java
Initializing a List in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class