-1

When I run this on Tomcat web server and try to POST the FORM with the right or wrong credentials it results to a HTTP/500 error.

The servlet code:

/*
 * Written by Leonardo van de Weteringh
 */
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/check")
public class check extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        final String Username = "Noord";
        final String Password = "Korea";

        String formUsername = request.getParameter("username");
        String formPassword = request.getParameter("password");

        if (formUsername.equals(Username) && formPassword.equals(Password)) {
            response.sendRedirect("/internet/ok.html");
        } else {
            RequestDispatcher view = request.getRequestDispatcher("/internet/fail.html");
            view.forward(request, response);
        }
    }
}

This is the Login Page code:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Koryo Airlines DPRK</title>
<link rel="stylesheet" type="text/css" href="../../scripts/css/login.css">
<link rel="shortcut icon" type="image/png" href="../../content/images/favicon.png"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class=navbar>
<ul class=horinav>
  <li class=horinav><a href="../../.">Home</a></li>
  <li class=horinav><a href="../../news/news.html">News</a></li>
  <li class=horinav><a href="../../contact/contact.html">Contact</a></li>
  <li class=horinav><a href="../../about/about.html">About</a></li>
  <li class=horinav>
  <div class=dropdown>
    <button class="dropbtn">More...</button>
  <div class="dropdown-content">
    <a href="../../about/flyingkoryo.html">Flying Koryo</a>
    <a href="../../about/aboutkoryo.html">About Koryo</a>
    <a href="../../about/bagage.html">Bagage</a>
    <a href="../../about/checkin.html">Check-In Procedure</a>
    <a href="../../websites/koreatoday.html">Korea Today</a>
    <a href="../../websites/friendsofkorea.html">More Websites...</a>
  </div>  
  <li class=horinav style="float:right"><a href="../register/register.html">Register</a></li>
  <li class=horinav style="float:right"><a class="active" href="#">Login</a></li>
</ul>
</div>
<div class="row">
<div class="column">
<center><img src="../content/images/banner.jpg" alt="" height="200" width="50%"></center>
</div>
</div>

<h1> Koryo Airlines Login Portal</h1>

<form action="/check" method="post">
  <div class="imgcontainer">
    <img src="../../content/images/login.jpg" alt="Avatar" class="avatar">
  </div>
<center>
  <div class="container">
    <label for="username"><b>Username</b></label>
    <input type="text" name="username" placeholder="Gebruikersnaam" maxlength="20" required>
<br>
    <label for="password"><b>Password</b></label>
    <input type="password" name="password" placeholder="Wachtwoord" maxlength="20" required>

    <button type="submit">Login</button>

    <label>
      <input type="checkbox" checked="checked" name="remember"> Remember me
    </label>
  </div>
</center>
  <div class="container" style="background-color:#f1f1f1">
    <button onclick="window.location.href='../../index.html'" type="button" class="cancelbtn">Cancel</button>
    <span class="psw">Forgot <a href="./forgotpsw.html">password?</a></span>
  </div>

</form>

</body>
</html>

The error I get:

 java.lang.NullPointerException check.doPost(check.java:21)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Can somebody help me. This is what the Servlet needs to do:

  • Accept input from the form and POST it.
  • To check (validate) the input with a hard-coded username and password and see if there is any match.
  • Redirect to the page ok.html or the fail.html
user207421
  • 305,947
  • 44
  • 307
  • 483
Blackd00r
  • 3
  • 7
  • Can you tell what is the code on line _21_ of the `check.java`? (`java.lang.NullPointerException check.doPost(check.java:21) `. – prasad_ Jan 15 '19 at 05:17
  • I think you are getting a `null` value for `formUsername` from this code: `String formUsername = request.getParameter("username");`. And it is resulting in the `NullPointerException` at: `if (formUsername.equals(Username)`. It is difficult to tell from the code you have shown what is wrong; the code looks alright at the first look. – prasad_ Jan 15 '19 at 05:50
  • Did you enter _user name_ and _password_ values in the HTML form? If you don't enter values and submit the form you will get that error. – prasad_ Jan 15 '19 at 05:56
  • One way to check if your form submit is working and the parameters are sent to the servlet is do these three steps: (1) remove the form method="post" and it will default to "get". (2) When you do the form submit you can see the request parameters and values in the browser address bar like this `http://example.com/path/to/page?username=John&password=Doe`; note the query string has the request parameter names and their values. What do you see there? (3) Add a `doGet` method to the servlet to verify the posting of the form from the changed HTML. – prasad_ Jan 15 '19 at 06:08

3 Answers3

0

Maybe your variable(formUsername or formPassword) is null value , So your program gives a null pointer。

You should check if you can get the name and password from the request.

lixinglin
  • 61
  • 1
  • 4
0

Thank prasad_ for his last comment to change the POST to GET to check the variables which are given. They did not seem to match in the HTML form. I changed the

<input type="text" name="username" placeholder="Gebruikersnaam" maxlength="20" required>

to

<input type="text" name="Username" placeholder="Gebruikersnaam" maxlength="20" required>

and

<input type="password" name="password" placeholder="Wachtwoord" maxlength="20" required>

to

<input type="password" name="Password" placeholder="Wachtwoord" maxlength="20" required>

All the other changes remain untouched and it worked again. The credentials when entered OK are redirecting to ok.html

Blackd00r
  • 3
  • 7
-1

One thing I realized your servlet code is not overriding the default doPost method. You should have something like this normally

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
                   throws ServletException, IOException 

and other thing I can say check if you are adding javax.servlet.api-3.0.1.jar (there might be latest version)