【问题标题】:Validation of JSPJSP 的验证
【发布时间】:2014-10-02 16:19:43
【问题描述】:

我想在 JSP 本身中验证 JSP,但它给了我一个空指针异常......我的代码如下......

<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en"> <!--<![endif]-->

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Login Form</title>
  <link rel="stylesheet" href="admincss/style.css">
  <!--[if lt IE 9]><script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
<body>
<%
String username=request.getParameter("txt_username");
String password=request.getParameter("txt_password");
if(username.equals("k") && password.equals("k"))
{
    response.sendRedirect("Admin");
}
%>
  <section class="container">
    <div class="login">
      <h1>Login to Web App</h1>
      <form method="post" action="admin.jsp">
        <p><input type="text" name="txt_username" value="" placeholder="Username or Email"></p>
        <p><input type="password" name="txt_password" value="" placeholder="Password"></p>
        <p class="remember_me">
          <label>
            <input type="checkbox" name="remember_me" id="remember_me">
            Remember me on this computer
          </label>
        </p>
        <p class="submit"><input type="submit" name="btn_commit" value="Login"></p>
      </form>
    </div>

    <div class="login-help">
      <p>Forgot your password? <a href="index.html">Click here to reset it</a>.</p>
    </div>
  </section>

  <section class="about">
    <p class="about-links">
      <a href="http://www.cssflow.com/snippets/login-form" target="_parent">View Article</a>
      <a href="http://www.cssflow.com/snippets/login-form.zip" target="_parent">Download</a>
    </p>
    <p class="about-author">
      &copy; 2012&ndash;2013 <a href="http://thibaut.me" target="_blank">Thibaut Courouble</a> -
      <a href="http://www.cssflow.com/mit-license" target="_blank">MIT License</a><br>
      Original PSD by <a href="http://www.premiumpixels.com/freebies/clean-simple-login-form-psd/" target="_blank">Orman Clark</a>
  </section>
</body>
</html>

我不明白这个错误.....如果这不是正确的方法,请给我推荐正确的方法

【问题讨论】:

    标签: java jsp


    【解决方案1】:

    NullPointerException 通常意味着您尝试调用方法或访问变量上的字段不引用任何对象。除了预定义的requestresponse 之外,您在此文件中使用的唯一变量是usernamepassword

    如果请求没有指定名称的参数,request.getParameter 方法将返回 null。因此,如果您的请求不包含txt_username 和/或txt_password 参数,则username 和/或password 变量将为空。

    在下面的行中,当您调用username.equalspassword.equals 时,如果这些变量为空,您将收到异常。如果没有可以调用的对象,则无法调用该方法。

    您需要添加一些代码来处理usernamepassword 为空的情况。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2014-10-18
      • 1970-01-01
      • 2011-11-20
      相关资源
      最近更新 更多