繁体   English   中英

需要使用布尔值更改JSP页面中的消息

[英]Need Message in JSP Page to Change using Boolean

我是一名学生,正在研究一个项目,其中Servlet根据JSP页面中用户输入的信息检查雇员目录的ID或姓氏。 我需要布尔值“ employeeFound”来输出有关是否找到雇员的消息(“ project4SearchMessage”)。 到目前为止,它仅显示一条有关是否找到该雇员的消息。 需要一些帮助,谢谢!

    ServletContext context = getServletContext();

    HttpSession session = request.getSession();

    //Get the EmployeeDirectory instance from the ServletContext
    EmployeeDirectory employeeSearch = 
        (EmployeeDirectory)context.getAttribute("employeeDirectory");

    //Get the search type and search term from the HTML form
    String searchTerm = request.getParameter("searchTerm");
    String searchType = request.getParameter("searchType");

    //Search for employees by calling method in the EmployeeDirectory
    //instance and pass the search type and the search term
    //to the method
    if (searchType.equals("id")) {
        Search search = new Search();
        search.setSearchTerm(searchTerm);
        search.setSearchType(searchType);
        employeeSearch.searchByEmployeeID(search);
        //place the Search object into the session   
        session.setAttribute("search", search);
        if (search.isEmployeeFound() == false) {
            String project4SearchMessage = "No Employee found.";
            session.setAttribute("project4SearchMessage", project4SearchMessage);
        }

        if (search.isEmployeeFound() == true) {
            String project4SearchMessage = "Employee Found";
            session.setAttribute("project4SearchMessage", project4SearchMessage);
        }
    }

    if (searchType.equals("lastName")) {
        Search search = new Search();
        search.setSearchTerm(searchTerm);
        search.setSearchType(searchType);
        employeeSearch.searchByEmployeeLastName(search);
        //place the Search object into the session
        session.setAttribute("search", search);
        if (search.isEmployeeFound() == false) {
            String project4SearchMessage = "No Employee found.";
            session.setAttribute("project4SearchMessage", project4SearchMessage);
        }

        if (search.isEmployeeFound() == true) {
            String project4SearchMessage = "Employee Found";
            session.setAttribute("project4SearchMessage", project4SearchMessage);
        }
    }

    //Employee Search Results page url
    String url = "/jsp/searchResults.jsp";

    //instantiate a RequestDispatcher object and assign the url
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);

    //forward to jsp page
    dispatcher.forward(request, response);
}

这是JSP:

<h3>${project4SearchMessage}</h3>
<h2>Employee Search Results:</h2>
  <table>        
    <tr>
    <td> Search Type </td>
    <td> ${search.searchType} </td>
    </tr>   
    <tr>
    <td> Search Term </td>
    <td> ${search.searchTerm} </td>
    </tr>
    <c:forEach var="employee" items="${search.employeeList}">
      <tr>
      <td> Employee </td>
      <td> ${employee.toString()}</td>
      </tr> 
    </c:forEach>
  </table> 

这是布尔值:

private String searchType;
private String searchTerm;
private List employeeList = new ArrayList();
private boolean employeeFound;

/**
 * Empty constructor for the Search object
 */
public Search() {

}

/**
 * Returns the value of searchType.
 */
public String getSearchType() {
    return searchType;
}

/**
 * Sets the value of searchType.
 * @param searchType The value to assign searchType.
 */
public void setSearchType(String searchType) {
    this.searchType = searchType;
}

/**
 * Returns the value of searchTerm.
 */
public String getSearchTerm() {
    return searchTerm;
}

/**
 * Sets the value of searchTerm.
 * @param searchTerm The value to assign searchTerm.
 */
public void setSearchTerm(String searchTerm) {
    this.searchTerm = searchTerm;
}

/**
 * Returns the value of employeeList.
 */
public List getEmployeeList() {
    return employeeList;
}

/**
 * Sets the value of employeeList.
 * @param employeeList The value to assign employeeList.
 */
public void setEmployeeList(List employeeList) {
    this.employeeList = employeeList;
}

/**
 * Returns the value of foundEmployee.
 */
public boolean isEmployeeFound() {
    return employeeFound;
}

/**
 * Sets the value of foundEmployee.
 * @param foundEmployee The value to assign foundEmployee.
 */
public void setEmployeeFound(boolean employeeFound) {
    this.employeeFound = employeeFound;
}

/**
 * The addFoundEmployee method adds the employee object to the List of found
 * Employee objects.
 */
public void addFoundEmployee(Employee employee) {
    employeeList.add(employee);
}

这是EmployeeDirectory方法:

/**
 * The searchByEmployeeID method searches for an Employee in the database by 
 * employee id.
 *
 *@param search search object from the SearchResultsServlet class
 */
public void searchByEmployeeID(Search search) {

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
        connection = getConnection();
        statement = connection.createStatement();

        String queryString = "SELECT emp_id, first_name, last_name, ssn, dept, room, phone "
                + " FROM employees " 
                + "WHERE emp_id = " + search.getSearchTerm() + ";";

        resultSet = statement.executeQuery(queryString);

        if (resultSet != null) {
            //if the query returns any rows set boolean to true
            search.setEmployeeFound(true);

            //finds and returns the next complete entry
            while (resultSet.next()) {
                //instantiate a new Employee object
                Employee employee = new Employee();

                //set instance variables from the row from the database
                employee.setId(resultSet.getString("emp_id"));
                employee.setFirstName(resultSet.getString("first_name"));
                employee.setLastName(resultSet.getString("last_name"));
                employee.setSsn(resultSet.getString("ssn"));
                employee.setDept(resultSet.getString("dept"));
                employee.setRoom(resultSet.getString("room"));
                employee.setPhone(resultSet.getString("phone"));

                //each new Employee object added to the Search object
                search.addFoundEmployee(employee);

            }

        } else {
            //if the query does not return any rows set boolean to false
            search.setEmployeeFound(false);
        }

    } catch (SQLException sqlException) {
        System.err.println("Error in connection.ecting to database "
                + sqlException);
        sqlException.printStackTrace();
    } catch (Exception exception) {
        System.err.println("General Error");
        exception.printStackTrace();
    //close the connection
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
            }

            if (statement != null) {
                statement.close();
            }

            if (connection != null) {
                connection.close();
            }
        } catch (SQLException sqlException) {
            System.err.println("Error in connection.ecting to database "
                    + sqlException);
            sqlException.printStackTrace();
        } catch (Exception exception) {
            System.err.println("General Error");
            exception.printStackTrace();
        }
    }            
}


/**
 * The searchByEmployeeLastName method searches for an Employee in the  
 * database by employee last name.
 *
 *@param search search object from the SearchResultsServlet class
 */
public void searchByEmployeeLastName(Search search) {

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    try {
        connection = getConnection();
        statement = connection.createStatement();

        String queryString = "SELECT emp_id, first_name, last_name, ssn, dept, room, phone "
                + " FROM employees " 
                + "WHERE last_name LIKE '" + search.getSearchTerm() + "%'";

        resultSet = statement.executeQuery(queryString);

        if (resultSet != null) {
            //if the query returns any rows set boolean to true
            search.setEmployeeFound(true);

            //finds and returns the next complete entry
            while (resultSet.next()) {
                //instantiate a new Employee object
                Employee employee = new Employee();

                //set instance variables from the row from the database
                employee.setId(resultSet.getString("emp_id"));
                employee.setFirstName(resultSet.getString("first_name"));
                employee.setLastName(resultSet.getString("last_name"));
                employee.setSsn(resultSet.getString("ssn"));
                employee.setDept(resultSet.getString("dept"));
                employee.setRoom(resultSet.getString("room"));
                employee.setPhone(resultSet.getString("phone"));

                //each new Employee object added to the Search object
                search.addFoundEmployee(employee);

            }

        } else {
            //if the query does not return any rows set boolean to false
            search.setEmployeeFound(false);
        }

    } catch (SQLException sqlException) {
        System.err.println("Error in connection.ecting to database "
                + sqlException);
        sqlException.printStackTrace();
    } catch (Exception exception) {
        System.err.println("General Error");
        exception.printStackTrace();
    //close the connection
    } finally {
        try {
            if (resultSet != null) {
                resultSet.close();
            }

            if (statement != null) {
                statement.close();
            }

            if (connection != null) {
                connection.close();
            }
        } catch (SQLException sqlException) {
            System.err.println("Error in connection.ecting to database "
                    + sqlException);
            sqlException.printStackTrace();
        } catch (Exception exception) {
            System.err.println("General Error");
            exception.printStackTrace();
        }
    }           
}     

在您的代码中,您将在遍历结果集的开始处设置标志。

但是,结果集永远不会为null(请参阅Java文档

        resultSet = statement.executeQuery(queryString);

        /**********************************/
        /** your result set is never null */
        /** this is incorrect             */
        /**********************************/
        if (resultSet != null) {
            //if the query returns any rows set boolean to true
            search.setEmployeeFound(true);
            //...
        }

相反,您需要在处理结果集的末尾设置标志。 如果将结果集添加到列表,则将标志设置为true。

while (resultSet.next()) {
    //instantiate a new Employee object
    Employee employee = new Employee();

    //set instance variables from the row from the database
    employee.setId(resultSet.getString("emp_id"));
    // ... etc.

    //each new Employee object added to the Search object
    search.addFoundEmployee(employee);    
}

// employeeList is never null so we don't check for it, just its size
if( search.getEmployeeList().size() > 0 ){
     search.setEmployeeFound(true);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM