当我在Tomcat Web服务器上运行此程序,并尝试使用正确或错误的凭据过帐FORM时,将导致HTTP / 500错误。
servlet代码:
/*
* 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);
}
}
}
这是登录页面代码:
<!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>
我得到的错误:
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)
有人可以帮我吗。这是Servlet需要做的:
username
检查(验证)输入,然后
password
,看是否有匹配项。ok.html
或fail.html
答案 0 :(得分:0)
也许您的变量(formUsername或formPassword)为空值,所以您的程序给出了一个空指针。
您应该检查是否可以从请求中获取名称和密码。
答案 1 :(得分:0)
感谢prasad_的最后评论,将POST更改为GET,以检查给定的变量。它们似乎在HTML表单中不匹配。我更改了
<input type="text" name="username" placeholder="Gebruikersnaam" maxlength="20" required>
到
<input type="text" name="Username" placeholder="Gebruikersnaam" maxlength="20" required>
和
<input type="password" name="password" placeholder="Wachtwoord" maxlength="20" required>
到
<input type="password" name="Password" placeholder="Wachtwoord" maxlength="20" required>
所有其他更改均保持不变,并且再次起作用。输入“确定”后的凭据将重定向到ok.html
答案 2 :(得分:-1)
我意识到您的servlet代码没有覆盖默认的doPost方法。您通常应该有这样的东西
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
还有我可以说的另一件事,就是检查您是否要添加javax.servlet.api-3.0.1.jar(可能是最新版本)