如何将变量从java传递给jsp?

时间:2016-10-06 12:33:44

标签: java jsp

EmployeeController.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            Employee employee = new Employee();
            employee.setFirstname(request.getParameter("firstName"));
            employee.setLastname(request.getParameter("lastName"));
            employee.setEmail(request.getParameter("email"));
            employee.setStatus(request.getParameter("status"));

            String employeeId = request.getParameter("email");

                employee.setEmail(employeeId);
                dao.addEmployee(employee);              

            RequestDispatcher view = request.getRequestDispatcher(employee_listing);
            request.setAttribute("employees", dao.getAllEmployees());
            view.forward(request, response);            
        }

EmployeeDao.java

HttpServletRequest request;   

   public void addEmployee(Employee employee) throws ServletException, IOException{
        try {
            PreparedStatement preparedStatement = conn.prepareStatement("insert into login(firstname, lastname, email, pass, role, status) values (?, ?, ?, ?, 'employee', ?)");                               
            preparedStatement.setString(1, employee.getFirstname());            
            preparedStatement.setString(2, employee.getLastname());
            preparedStatement.setString(3, employee.getEmail());
            //preparedStatement.setString(4, employee.getFirstname());
            preparedStatement.setInt(4, employee.getFirstname().hashCode());
            preparedStatement.setString(5, employee.getStatus());
            //preparedStatement.setBoolean(4, employee.getStatus());
            int i = preparedStatement.executeUpdate();
            if(i > 0){
                System.out.println("Employee added successfully.");
                HttpSession session = request.getSession();
                session.setAttribute("successMessage", "Employee added successfully.");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            System.out.println(e);
            //e.printStackTrace();
        }
    }

addEmployee.jsp

<form class="form-horizontal" method="post" action="EmployeeController" name="frmAddEmployee" id="frmAddEmployee">
                                <!-- <form class="form-horizontal" method="post" action="adduser.jsp" name="frmAddEmployee" id="frmAddEmployee"> -->
                                    <div class="form-group">
                                        <label for="" class="col-sm-4 control-label">First Name</label>
                                        <div class="col-sm-8">
                                          <input type="text" name="firstName" class="form-control" id="firstName">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="" class="col-sm-4 control-label">Last Name</label>
                                        <div class="col-sm-8">
                                          <input type="text" name="lastName" class="form-control" id="lastName" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="" class="col-sm-4 control-label">Email</label>
                                        <div class="col-sm-8">
                                          <input type="email" name="email" class="form-control" id="email" >
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-4 control-label">Employee Status</label>
                                        <div class="col-sm-8">
                                            <label class="switch">
                                                <input type="checkbox" name="status" value="1" checked>
                                                <div class="slider round"></div>
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <div class="col-sm-4"></div>
                                        <div class="col-sm-8">
                                            <input type="submit" name="submit" value="Submit" class="btn btn-primary"> &nbsp; <!-- <button name="cancel"  onclick='employee-listing.jsp' class="btn btn-primary" >Cancel</button> -->
                                        </div>
                                    </div>
                                </form> 

所以基本上提交表单时它重定向到EmployeeController.java文件并获取Employee中的所有值,然后重定向到EmployeeDao.java文件addEmployee(employee);

我会在jsp页面中得到这个,如::

session.getAttribute("successMessage");

我尝试了所有这些,但我没有从“successMessage”属性获得任何消息,只获得“null”值。

我也试过response.sendRedirect("employee-success.jsp"),但它还没有重定向到该页面。

所以任何人都可以帮我设置java代码中的值,并在jsp页面中获取该值以供进一步使用。

2 个答案:

答案 0 :(得分:1)

改变你的

public void addEmployee(Employee employee)

public void addEmployee(Employee employee, HttpServletRequest request) 

HttpServletRequest

中传递EmployeeController.java个对象
dao.addEmployee(employee,request); 

答案 1 :(得分:0)

从Controller传递请求对象,

dao.addEmployee(employee);     

并直接在jsp中访问sessionAttribute,如

${successMessage}

您可以从addEmployee方法将成功消息发送回Controller,然后在请求属性中设置该字符串消息,如

String successMessage = dao.addEmployee(employee);              

RequestDispatcher view = request.getRequestDispatcher(employee_listing);
request.setAttribute("employees", dao.getAllEmployees());
request.setAttribute("successMessage", successMessage);

在你的DAO方法中,

public String addEmployee(Employee employee) throws ServletException, IOException{
    String successMessage = "";
    try {
        PreparedStatement preparedStatement = conn.prepareStatement("insert into login(firstname, lastname, email, pass, role, status) values (?, ?, ?, ?, 'employee', ?)");                               
        preparedStatement.setString(1, employee.getFirstname());            
        preparedStatement.setString(2, employee.getLastname());
        preparedStatement.setString(3, employee.getEmail());
        //preparedStatement.setString(4, employee.getFirstname());
        preparedStatement.setInt(4, employee.getFirstname().hashCode());
        preparedStatement.setString(5, employee.getStatus());
        //preparedStatement.setBoolean(4, employee.getStatus());
        int i = preparedStatement.executeUpdate();
        if(i > 0){
            System.out.println("Employee added successfully.");
            HttpSession session = request.getSession();
            successMessage = "Employee added successfully.";
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.out.println(e);
        //e.printStackTrace();
        successMessage = "Error occured while adding employee."
    }
    return successMessage;
}