有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

forms Java Servlet以状态代码HTTP/500运行

当我在Tomcat web服务器上运行这个程序,并尝试使用正确或错误的凭据发布表单时,它会导致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.htmlfail.html

共 (1) 个答案

  1. # 1 楼答案

    我意识到您的servlet代码没有覆盖默认的doPost方法。你通常应该吃这样的东西

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

    我可以说的另一件事是检查你是否在添加javax。servlet。api-3.0.1。jar(可能有最新版本)