使用DAO类进行用户名密码验证WITHOUT SCRIPTLETS或.jsp页面中的java代码

时间:2012-12-02 15:30:26

标签: jsp jstl javabeans jsp-tags

我想在javaBean类中运行一个方法(它不是getter或setter方法),它需要2个参数(用户名,密码)并返回一个布尔值(在从数据库中变换它之后(单独的DAO类))。

然后我想根据该布尔值做出决定,以显示一些文本或重定向页面。我该怎么做。? 我需要JSTL或jsp:action(useBean和set / get属性)标签还是足够?

没有脚本或.jsp页面中的java代码。没有像struts或spring那样的任何框架 任何实用的代码?

的index.jsp

  <%@page contentType="text/html" pageEncoding="UTF-8"%>
  <!DOCTYPE html>
  <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Open-Pages</title>
      </head>
      <body>
      <form method="post" action="/open-pages/comments.jsp">
        User Name: <input type="text" name="userName">
        <br />
        Password: <input type="password" name="password">
        <input type="submit" value="submit">
        <input type="reset" value="reset">
      </form>
      </body>
  </html>

comments.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>
    <%
//         out.println(request.getParameter("userName"));
//         out.println(request.getParameter("userName"));
    %>

    <jsp:useBean id="verifyUser" class="entry.UserBean" scope="page" />
    <jsp:setProperty name="verifyUser" property="userName" param="userName" />
    <jsp:setProperty name="verifyUser" property="password" param="password" />

    <jsp:getProperty name="verifyUser" property="userName" />
    <jsp:getProperty name="verifyUser" property="password" />


    </body>
</html>

UserBean.java

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      package entry;

      import java.io.Serializable;


      /**
      *
      * @author user1
      */
      public class UserBean implements Serializable{
      private String userName;
      private String password;
      private boolean validUser;

      /**
      * @return the userName
      */
      public String getUserName() {
          return userName;
      }

      /**
      * @param userName the userName to set
      */
      public void setUserName(String userName) {
          this.userName = userName;
      }

      /**
      * @return the password
      */
      public String getPassword() {
          return password;
      }

      /**
      * @param password the password to set
      */
      public void setPassword(String password) {
          this.password = password;
      }




      public boolean getValidUser(String user, String password) {
          if(entry.userAuthDAO.verifyUserPass(user,password)){
        return true;
        }else {
        return false;
        }
      }


      public void setValidUser(boolean validUser) {
          this.validUser = validUser;
      }
      }

UserAuthDAO

      package entry;


      import entry.Constants;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.logging.Level;
      import java.util.logging.Logger;

      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      /**
      *
      * @author user1
      */
      public class userAuthDAO {

      static String authUser = "select userName, password "
              + "from users "
              + "where userName = ? and password = ?";
      public static boolean verifyUserPass(String userName, String password){


          try {
          Connection con = DriverManager.getConnection(Constants.DBHost, userName, password);
          PreparedStatement pStmt = con.prepareStatement(authUser);

          pStmt.setString(1,userName);
          pStmt.setString(2, password);

          ResultSet rs = pStmt.executeQuery();
          String rsUserName = null;
          String rsPassword = null;
          if(rs.next()){
              rsUserName = rs.getString("userName");
              rsPassword =  rs.getString("password");    
          }

          con.close();

          if (rsUserName.equals(userName) && rsPassword.equals(password)){
              return true;
          }



        } catch (SQLException ex) {
            Logger.getLogger(userAuthDAO.class.getName()).log(Level.SEVERE, null, ex);

        }
        return false; 
      }

      }

1 个答案:

答案 0 :(得分:1)

不要在JSP中执行此操作。这不是JSP的用途。它们是视图组件,应该用于生成HTML标记。

从servlet或首选MVC框架的操作执行此操作。一旦servlet或action有结果,然后调度到JSP或重定向。