Program to validate a user using JSP

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<jsp:name_of_tag>

chevron_right


and end with

filter_none

edit
close

play_arrow

link
brightness_4
code

</jsp:name_of_tag>

chevron_right



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:

  1. We click the link on index.html page to deploy the application.
  2. We are then presented with a form, where we enter username and password and click submit.
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<%@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>
     

chevron_right


JSP to accept form data and verify a user : acceptuser.jsp

filter_none

edit
close

play_arrow

link
brightness_4
code

<%@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>

chevron_right


The ValidateUser.java class

filter_none

edit
close

play_arrow

link
brightness_4
code

package saagnik;
import java.io.Serializable;
  
// To persist the data for future use,
// implement serializable
public 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;
    }
}

chevron_right


Outputs:
login.jsp
User Form Page

Next on clicking ‘SUBMIT’ button following page is generated.
acceptuser.jsp
Validation Result



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.