1

I am attempting to use a servlet to retrieve data returned from a servlet to display on the page. My issue is my snippet of JSP code works but before it receives the response from the server it displays null on the screen from the request.getAttribute code.

here is my code

JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>

    <title>GameAlytics Login</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel ="stylesheet" href="styles.css">

</head>
<body>


 <div id="navbar">
                <div id="logo">
                <img SRC="logo.png" ALT="Unable to load image" WIDTH=150 HEIGHT=90>
            </div>
                <div id="navbar-right">
                    <a class="active" href="index.html">Home</a>
                    <a href="Login.html">Login</a> 
                    <a href="ViewData.html">View Data</a>
                    <a href="Contact.html">Contact</a>
                    <a href="About.html">About</a>
                </div>
            </div>

    <form action="Login" method="GET">
          <div class="imgcontainer">
            <img src="images/avatar.jpg" alt="Avatar" class="avatar">
          </div>

          <div class="container">
            <label for="username"><b>Username</b></label>
            <input type="text" placeholder="Enter Username" name="username" id="username"  required>

            <label for="psw"><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="password" id="password" required>

            <button>Login as administrator</button>
          </div>
        </form>
        <p style="color:red; font-size: 20px;"><%=request.getAttribute("errorMessage")%></p>
        <div id="footer">
          <h2>Copyright of GameAlytics 2019</h2>
        </div>
        </body>
        </html>

Servlet:

 //if details entered are correct, take user to system info page
          if(enteredUsername.equals(dbUsername)&&enteredPassword.equals(dbPassword)){

             response.sendRedirect("ViewData.html");     
          }

          ///if details entered are incorrect, user remains on login page
          else{

               request.setAttribute("errorMessage", "Invalid user or password");
               request.getRequestDispatcher("test.jsp").forward(request, response); 
          }

        }//try end

Really I'm just looking for a way to hide the null and only display the result when it arrives

Charlie Ansell
  • 451
  • 1
  • 7
  • 22

1 Answers1

0

You can check if the value is null or not like below :

//here we check if  the errorMessage is not null
<% if(request.getAttribute("errorMessage") !=null){ %>
  <p style="color:red; font-size: 20px;">
<%=request.getAttribute("errorMessage"); %>
 </p>
<% } %>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • This jurassic way of authoring JSPs makes the solution unnecessarily overcomplicated. Just use `

    ${errorMessage}

    ` instead. The support for EL was introduced in 2001 already. Get out the cave and carefully read https://stackoverflow.com/q/3177733 and make absolutely sure that you're using the right sources for learning JSP (i.e. they are not dated in previous century).
    – BalusC Aug 09 '19 at 11:35