No Widget Added

Please add some widget in Offcanvs Sidebar

Shopping cart

shape
shape

Login Page with Java Server Pages (JSP)

  • Home
  • Java
  • Login Page with Java Server Pages (JSP)
login page with jsp, login page with java server pages, java course online,java classs online,how to learn java, how to become java full stack developer, Skills101, Skills101.in

To create a simple JSP login page that validates the username and password, and redirects to a welcome page or shows an error message on the same login page if the credentials are wrong, you need to follow these steps:

  1. Create the login form (login.jsp)
  2. Create the login validation logic (validateLogin.jsp)
  3. Create the welcome page (welcome.jsp)

Step 1: Create the Login Form (login.jsp)

This file contains the login form where users enter their username and password.

<!DOCTYPE html>
<html>
<head>
    <title>Login Page</title>
</head>
<body>
    <h2>Login Page</h2>
    <form action="validateLogin.jsp" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required><br><br>
        
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required><br><br>
        
        <input type="submit" value="Login">
    </form>
    <%
        String errorMessage = (String) request.getAttribute("errorMessage");
        if (errorMessage != null) {
    %>
        <p style="color:red;"><%= errorMessage %></p>
    <%
        }
    %>
</body>
</html>

Step 2: Create the Login Validation Logic (validateLogin.jsp)

This file handles the login logic by checking if the username and password are correct.

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    if ("admin".equals(username) && "admin".equals(password)) {
        // Correct username and password
        response.sendRedirect("welcome.jsp");
    } else {
        // Incorrect username or password
        request.setAttribute("errorMessage", "Invalid username or password");
        request.getRequestDispatcher("login.jsp").forward(request, response);
    }
%>

Step 3: Create the Welcome Page (welcome.jsp)

This file is the welcome page that users see after logging in successfully.

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Page</title>
</head>
<body>
    <h2>Welcome, Admin!</h2>
    <p>You have successfully logged in.</p>
</body>
</html>

Explanation:

  • login.jsp: This page displays a login form where users can enter their username and password. If the login fails, an error message is shown.
  • validateLogin.jsp: This script retrieves the username and password from the form, checks if they match the correct credentials (admin for both username and password), and redirects the user to welcome.jsp if they match. If they do not match, it sets an error message and forwards the request back to login.jsp.
  • welcome.jsp: This is the page users see after a successful login.

Deployment:

Ensure that your JSP files are placed in the appropriate directory (typically the webapp directory) and your web server (such as Apache Tomcat) is properly configured to serve JSP pages. You can deploy these JSP files to your server and access the login page via your web browser.

Testing:

  1. Access login.jsp via your web browser.
  2. Enter admin as the username and admin as the password.
  3. If the credentials are correct, you should be redirected to welcome.jsp.
  4. If the credentials are incorrect, you should see an error message on the login.jsp page.

Leave A Comment

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