Jsp Application Examples: Registration & Login Shape

In this tutorial, we’re going develop pattern applications with JSP and the use of MVC structure.

Following Program Examples, will be advanced –

  • Registration form
  • Login and Logout shape

Using registration shape through JSP

In Registration shape, we will have a form to fill all the info on the way to incorporate call, username, password, cope with, touch range, and so on.

This shape will assist us to sign up with the utility. They take all our information and store it in a database or cache.

In this situation, we are going to take “Guru Registration shape” which has the subsequent fields:

  • First Name
  • Last Name
  • Username
  • Password
  • Address
  • Contact Number

  • After filling most of these information we have submit button, on click on of that button all of the details might be stored.

    Register_1.jsp

    

    <%@ web page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML four.01 Transitional//EN" "http://www.w3.org/TR/html4/unfastened.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content material="textual content/html; charset=ISO-8859-1">

    <title>Guru Registration Form</title>

    </head>

    <frame>

    <h1>Guru Register Form</h1>

    <form action="guru_register" method="submit"> <table style="with: 50%"> <tr> <td>First Name</td> <td><input type="text" call="first_name" /></td> </tr> <tr> <td>Last Name</td> <td><enter kind="text" name="last_name" /></td> </tr> <tr> <td>UserName</td> <td><input kind="textual content" name="username" /></td> </tr> <tr> <td>Password</td> <td><input kind="password" call="password" /></td> </tr> <tr> <td>Address</td> <td><input kind="text" name="deal with" /></td> </tr> <tr> <td>Contact No</td> <td><enter type="textual content" call="contact" /></td> </tr></desk> <input type="submit" cost="Submit" /></form>

    </frame>

    </html>

    Explanation of the code:

    Code Line 11: Here we are taking a form call which has movement i.e. the servlet to which the request might be processed and servlet call is guru_register.java. The request will be processed via POST method.

    Code Line 14-sixteen: Here we’re taking enter kind as textual content and name is first name

    Code Line 18-20: Here we are taking enter type as textual content and call is ultimate name

    Code Line 22-24: Here we are taking input kind as textual content and call is username

    Code Line 26-28: Here we are taking input kind as password(this will cover the password when typed) and name as password

    Code Line 30-32: Here we’re taking input type as textual content and name as cope with

    Code Line 34-36: Here we are taking input type as text and name as contact

    Code Line 37: Here we are taking a button of type put up and fee is likewise submit. On click of this button the action will go to corresponding guru_register servlet wherein all of the parameter values will be handed in the request.

    Guru_register.java

    

    package demotest;

    import java.io.IOException;

    import javax.servlet.RequestDispatcher;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    /** * Servlet implementation class guru_register */

    public class guru_register extends HttpServlet personal static very last long serialVersionUID = 1L; included void doPost(HttpServletRequest request, HttpServletResponse reaction) throws ServletException, IOException // TODO Auto-generated approach stub String first_name = request.getParameter("first_name"); String last_name = request.getParameter("last_name"); String username = request.getParameter("username"); String password = request.getParameter("password"); String address = request.getParameter("deal with"); String contact = request.getParameter("contact password.isEmpty() cope with.isEmpty() touch.isEmpty()) RequestDispatcher req = request.getRequestDispatcher("register_1.jsp"); req.encompass(request, reaction); else RequestDispatcher req = request.getRequestDispatcher("register_2.jsp"); req.forward(request, reaction);

    Explanation of the code:

    Code Line 14: Here we defining guru_servlet that is extending HttpServlet.

    Code Line 18: This motion doPost() technique in an effort to be called while we point out POST in motion characteristic in the above JSP shape.

    Code Line 20-25:Here we’re fetching the values from request i.efirst_name, last_name , username, password, cope with and call the use of request.getParameter.

    Code Line 27-32: Here we are taking if situation where we check any of the parameters that are fetched from request as whether they’re empty or not. If any of the parameter is empty then it will enter this condition deal with.isEmpty touch.isEmpty()) and we ought to fetch RequestDispatcher item using request item if you want to forward request to register_1.jsp. Here we also are including request and reaction objects.

    Code Line 33-37: This case will execute while any of the parameter isn’t empty .We will should fetch requestDispatcher object using request object a good way to ahead request to register_2.jsp.Here we’re forwarding request and reaction items.

    Register_2.jsp

    

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML four.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content material="text/html; charset=ISO-8859-1">

    <title>Guru Success Page</name>

    </head>

    <body> <a><b>Welcome User!!!!</b></a>

    </body>

    </html>

    Explanation of the code:

    Code Line 10: Here we are pronouncing welcome user. This JSP can be referred to as while all the parameters are stuffed.

    When you execute the above code , you get the following output:

    Output:

    When we click on on register_1.jsp, we are able to get a form so one can have details like first call, remaining name, username, password, cope with, touch. All the info had been filled. When we click on on submit button then we get the message as “Welcome User”

    Login and logout shape

    Like registration form we are able to have a login and logout shape.

    In this case, we’ve taken Login shape where we have two fields “username” and “password” with a put up button.

    When we click on on post button then we get welcome message with a logout button.

    When we click on on logout button then we get again to login shape.

    Register_3.jsp

    

    <%@ page language="java" contentType="textual content/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/unfastened.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

    <title>Guru Login Form</title>

    </head>

    <frame>

    <shape motion="guru_login" technique="submit"> <table style="with: 50%">

    <tr> <td>UserName</td> <td><input type="textual content" name="username" /></td> </tr> <tr> <td>Password</td> <td><enter kind="password" name="password" /></td> </tr> </table> <enter kind="post" fee="Login" /></shape>

    </frame>

    </html>

    Explanation of the code:

    Code Line 10:Here we’re taking a shape name which has motion i.e. servlet to which it has passed is guru_login.java. The technique thru which it’s going to bypass its POST.

    Code Line 13-16: Here we are taking an input subject “username” that is of the type text.

    Code Line 17-20: Here we’re taking an input field “password” which is of the kind password.

    Code Line 22: Here we are taking a “put up” button with the fee”Login” on which we click on then it goes to servlet guru_login where both the fields are taken the use of request item.

    Guru_login.java(servlet)

    

    package demotest;

    import java.io.IOException;

    import javax.servlet.RequestDispatcher;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    /** * Servlet implementation magnificence guru_login */

    public elegance guru_login extends HttpServlet

    public guru_login() terrific(); // TODO Auto-generated constructor stub

    blanketed void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException // TODO Auto-generated method password.isEmpty() ) RequestDispatcher req = request.getRequestDispatcher("register_3.jsp"); req.encompass(request, response); else RequestDispatcher req = request.getRequestDispatcher("register_4.jsp"); req.forward(request, reaction);

    Explanation of the code:

    Code Line 5-9: Here we are uploading vital imports inside the code.

    Code Line 14: Here we are taking guru_login servlet which extends HttpServlet.

    Code Line 21: Here we are using doPost() technique as within the shape we are the usage of POST method.

    Code Line 23-24: Here we taking parameters using request object i.e. username and password.

    Code Line 25-29: In this way, we are taking “if” situation in which we’re checking username and password whether they’re empty or not.In this example if it’s miles empty then we are getting requestdispatcher object which forwards to register_3.jsp with request and response items.

    Code Line 30-34: This could be executed if each aren’t empty then it forwards the request to register_4.jsp with request and reaction gadgets.

    Register_4.jsp

    

    <%@ web page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/free.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content material="textual content/html; charset=ISO-8859-1">

    <title>Guru Logged In</name>

    </head>

    <body> <table fashion="with: 50%"> <tr><td> <% String username = request.getParameter("username"); %>

    <a>Welcome<% out.println(username); %> User!!!! You have logged in.</a></td></tr>

    <tr></tr><tr><td></td><td></td><td><a href="register_3.jsp"><b>Logout</b></a></td></tr>

    </desk>

    </frame>

    </html>

    Explanation of the code:

    Code Line 12: Here we are getting parameter “username” from the request object within the string item username.

    Code Line thirteen: Here we have a welcome message with the username.

    Code Line 14: Here we link to logout the form which redirects to register_3.jsp.

    When you execute the above code then you definately get the following output:

    Output:

    Here whilst we click on register_3.jsp we get two fields”username” and “password” with a login button.

    After clicking on the Login button you get the beneath message with a button of Logout.

    When you click on on logout button you move back to login page

    Related Posts

    Leave a Reply

    Your email address will not be published. Required fields are marked *