PreparedStation不使用Java类的方法参数从MySql获取数据

PreparedStation不使用Java类的方法参数从MySql获取数据,java,jsp,jdbc,Java,Jsp,Jdbc,我试图从jsp页面传递用户名和密码,以验证Java类LogMeIn.Java中的方法,但结果是“false”。然而,当我硬编码值abc/abc时,我确实得到了“通过”的结果。这是成功的 请告诉我为什么它不接受通过jsp传递的参数 -LogMeIn.java- package org.cms.model; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLExce

我试图从jsp页面传递用户名和密码,以验证Java类LogMeIn.Java中的方法,但结果是“false”。然而,当我硬编码值abc/abc时,我确实得到了“通过”的结果。这是成功的

请告诉我为什么它不接受通过jsp传递的参数

-LogMeIn.java-

package org.cms.model;

      import java.sql.Connection;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.sql.Statement;
      import java.sql.PreparedStatement;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.sql.DataSource;


      public class LogMeIn {


        public boolean Authenticate( String username, String password ) {

          Connection connection = null;
           // Statement statement = null;
            ResultSet resultSet = null;
            PreparedStatement pst = null;
            String query;
            boolean result = false;

            try {
              Context initCtx = new InitialContext();
              Context envCtx = (Context) initCtx.lookup("java:comp/env");
              DataSource ds = (DataSource) envCtx.lookup("jdbc/TestDB");
              connection = ds.getConnection();
                //statement = connection.createStatement();

                query = "select name from account where name= ? and password = ?";

                pst = connection.prepareStatement(query);

                pst.setString(1, username);
                pst.setString(2, password);

                resultSet = pst.executeQuery();

                int count=0;

                if(resultSet.next())
                {
               count++;
                }
                if(count>0)
                {
                  result = true;
                }
                else
                {
                  result = false;
                }
            }

            catch (SQLException e){
              e.printStackTrace();
            }
            catch (Exception e){
              e.printStackTrace();
            }
            finally {
                if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
                if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
            }

          return result;
        }

      }
--罗金瑟夫莱特酒店--

    package org.cms.controller;

    import java.io.IOException;

    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 org.cms.model.LogMeIn;

    /**
     * Servlet implementation class LoginServlet
     */
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;

      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username;
        String password;

        username = request.getParameter("user");
        password = request.getParameter("pass");

        LogMeIn a = new LogMeIn();

        boolean result = a.Authenticate(username, password);
            System.out.println(result);
        if (result) {
          request.setAttribute("loginresult","Pass");
          RequestDispatcher dispatcher = request.getRequestDispatcher("Login.jsp");
          dispatcher.forward(request, response);
          }
        else {
          request.setAttribute("loginresult","Failed");
          RequestDispatcher dispatcher = request.getRequestDispatcher("Login.jsp");
          dispatcher.forward(request, response);
          }

      }

    }
--Login.jsp--

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>  
        <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Login</title>

    <link rel="stylesheet" href="css/style.css" type="text/css"></link>

    <script language="JavaScript" type="text/JavaScript" src="validate.js"> </script>

    </head>
    <body>

    <form enctype="multipart/form-data" onSubmit="return validate(this)" method="post" action="LoginServlet" class="form">
      <table border = "0">
      <tr align="left" valign="top">
      <td>User Name:</td>
      <td><input type="text" name ="user" class="inputbox"/></td>
      </tr>
      <tr align="left" valign="top">
      <td>Password:</td>
        <td><input type="password" name ="pass" class="inputbox" /></td>
      </tr>
      <tr align="left" valign="top">
      <td></td>
      <td><input type="submit" name="submit" 
      value="submit" class="fb8"/></td>
      </tr>
      </table>
      <c:out  value="${loginresult}" > </c:out>

    </form>
    </body>
    </html>

非常感谢使用setAttribute方法。此方法设置请求的属性值,稍后在servlet中通过dispatcher方法传递请求对象来检索该属性。属性的设置值由请求对象的getAttribute方法检索

   http://www.roseindia.net/servlets/request-parameter.shtml

 http://www.roseindia.net/tutorial/servlet/passParameters.html

JSP传递给LogMeIn的参数是什么?您已经将它们打印到控制台了吗?validate做什么?旁注:LogMeIn中catch块之前的最后12行可以减少为result=resultSet.next;很可能你们需要用引号或者类似的东西来包装它们。谢谢你们!Validate正在查看一个不存在的javascript文件。问题是由于Validate方法的原因,将null参数传递给servlet。谢谢你的建议,我已经在我的代码中做了更改。你能回答这个问题吗?