如何读取数据并将数据从servlet传递到JSP

时间:2014-03-21 23:48:49

标签: java jsp servlets

关于从servlet到JSP的读取和传递数据的问题

我想创建一个Java EE项目。总的来说,它在这个项目中有5个文件。

  • JSP文件:EmployeeInfo.jsp,EmployeeDetails.jsp
  • Servlets:EmployeeRegistrationServlet,EmployeeSearchServlet
  • Bean:员工
获取员工ID或员工信息的

EmployeeInfo.jsp 文件。它有两个提交按钮。如果用户想要搜索Employee,则会输入一个ID并调用EmployeeSearchServlet,如果用户想要添加新的Employee信息,则会调用EmployeeRegistrationServlet并输入客户信息。这是代码:

EmployeeSearchServlet 应该搜索具有最终用户输入的员工ID的员工。 servlet的工作是在集合中查找特定的员工。找到员工后,流程将转发到EmployeeDetails.jsp。

EmployeeRegistrationServlet 应该将员工添加到内存中存储员工的集合中。

如果找到员工,员工的详细信息将显示在EmployeeDetails.jsp上。 如果添加了新员工,员工的详细信息将显示在EmployeeDetails.jsp。

这是我的EmployeeInfo.jsp

<body>
<h1 align="center">Employee Information</h1>
<!--Form of find employee by employeeID -->
<form action="EmployeeSearchServlet" method="POST">
  <fieldset>
    <legend>Find Employee</legend>
    Employee ID: <input type="text" name="findEmployeeID"><br>
    <input type="submit" name="findEmployeeSubmit" value="Find Employee">
  </fieldset>
</form>
<p></p>
<!--Form of add employee-->
<form action="EmployeeRegistrationServlet" method="POST">
  <fieldset>
    <legend>Employee Information</legend>
        <table>
            <tr>
                <td>Employee ID (key)*: </td>
                <td><input type="text" name="employeeID"></td>
            </tr>
            <tr>
                <td>First Name:* </td>
                <td><input type="text" name="firstName"></td>
            </tr>
            <tr>
                <td>Last Name:* </td>
                <td><input type="text" name="lastName"></td>
            </tr>
            <tr>
                <td>Email: </td>
                <td><input type="text" name="email"></td>
            </tr>
            <tr>
                <td>Phone: </td>
                <td><input type="text" name="phone"></td>
            </tr>
            <tr>
                <td>Hire Date: </td>
                <td><input type="text" name="hireDate"></td>
            </tr>
            <tr>
                <td>Manager ID: </td>
                <td><input type="text" name="managerID"></td>
            </tr>
            <tr>
                <td>Department ID: </td>
                <td><input type="text" name="departmentID"></td>
            </tr>
        </table>
    <input type="submit" name="addEmployeeSubmit" value="Add Employee">
  </fieldset>
</form>

</body>

这是我的员工bean。

public class Employee {
    private String employeeID;
    private String firstName;
    private String lastName;
    private String email;
    private String phoneNumber;
    private String hireDate;
    private String managerID;
    private String departmentID;

    public Employee(String newEmployeeID, 
            String newFirstName, 
            String newLastName, 
            String newEmail,
            String newPhoneNumber,
            String newHireDate,
            String newManagerID,
            String newDepartmentID){
        this.employeeID = newEmployeeID;
        this.firstName = newFirstName;
        this.lastName = newLastName;
        this.email = newEmail;
        this.phoneNumber = newPhoneNumber;
        this.hireDate = newHireDate;
        this.managerID = newManagerID;
        this.departmentID = newDepartmentID;
    }

    public void setEmployeeID(String newEmployeeID){
        this.employeeID = newEmployeeID;
    }

    public void setFirstName(String newFirstName){
        this.firstName = newFirstName;
    }

    public void setLastName(String newLastName){
        this.lastName = newLastName;
    }

    public void setEmail(String newEmail){
        this.email = newEmail;
    }

    public void setPhoneNumber(String newPhoneNumber){
        this.phoneNumber = newPhoneNumber;
    }

    public void setHireDate(String newHireDate){
        this.hireDate = newHireDate;
    }

    public void setManagerID(String newManagerID){
        this.managerID = newManagerID;
    }

    public void setDepartmentID(String newDepartmentID){
        this.departmentID = newDepartmentID;
    }

public String getEmployeeID() {
    return employeeID;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

public String getEmail() {
    return email;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public String getHireDate(){
    return hireDate;
}

public String getManagerID(){
    return managerID;
}

public String getDepartmentID(){
    return departmentID;
}

}

这是我的EmployeeRegistrationServlet

@WebServlet("/EmployeeRegistrationServlet")
public class EmployeeRegistrationServlet extends HttpServlet {
    private Map<String,Employee> employees;
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String employeeID = request.getParameter("employeeID").trim();
        String firstName = request.getParameter("firstName").trim();
        String lastName = request.getParameter("lastName").trim();
        String email = request.getParameter("email").trim();
        String phone = request.getParameter("phone").trim();
        String hireDate = request.getParameter("hireDate").trim();
        String managerID = request.getParameter("managerID").trim();
        String departmentID = request.getParameter("departmentID").trim();
        employees = new HashMap<String,Employee>();
        Employee newAdd = new Employee(employeeID, firstName, lastName, email, phone, hireDate, managerID, departmentID);

        request.getRequestDispatcher("../../../../WebContent/EmployeeDetail.jsp").forward(request,response);
    }
}

我的问题是如何使用从EmployeeRegistrationServlet传递的EmployeeDetail.jsp读取bean对象?以及如何通过搜索员工ID来查找员工? 请给我一些建议。非常感谢。

2 个答案:

答案 0 :(得分:1)

  

我的问题是如何使用从EmployeeRegistrationServlet传递的EmployeeDetail.jsp读取bean对象?

您需要将请求/会话属性中的对象设置为可用于转发jsp

  

如何通过搜索员工ID来查找员工?

取决于您的数据存储,例如,如果您使用的是mysql,则需要使用jdbc和query来按id获取记录


查看

答案 1 :(得分:0)

你有很多选择。我建议您查看本教程,并考虑使用<jsp:useBean id="myObject" class="myClass" scope="session"/>标记:

以下是Oracle的完整JSTL教程: