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
andpassword
and see if there is any match. - Redirect to the page
ok.html
or thefail.html