0

I'm having a difficulty in getting the value on my drop down list from jsp. I'm getting a Null value of String which is "" only. But if I use only textbox for the department It will work. I'm having a hard time of finding where is the error in my code.

This is my JSP file:

 <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
        <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>

        </head>
        <body>

            <form method="POST" action='PersonnelController' name="frmAddData" class="form">

                <fieldset>
                    <legend id="myLegend">User</legend>

                    <label for="firstname">First Name :</label>
                            <input type="text" name="firstname"
                                value="<c:out value="${data.firstname}" />" /><br />

                        <label>Last Name :</label>
                            <input type="text" name="lastname"
                                value="<c:out value="${data.lastname}" />" /><br />

                        <!--<label>Department Name :</label>
                            <input type="text" name="department_id" 
                            value="<c:out value="${data.department_id}" />" /><br />-->

                        <label>Department Name :</label>
                <select name="personnel">
                <c:forEach items="${personnels}" var="personnel">
                <option value="${personnel.department_id}"><c:out value="${personnel.department_name}" /></option>
                </c:forEach>
</select>
                </fieldset>
                <input type="submit" value="Submit" class="submit"/>
            </form>

        </body>
        </html>

This is for Servlet:

private DepartmentDao dd;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<DepartmentBean> personnels = dd.getAllDepartmentName();
    request.setAttribute("personnels", personnels);
    request.getRequestDispatcher("/WEB-INF/personnel.jsp").forward(request, response);
}

}

and this is my DAO:

   public List<DepartmentBean> getAllDepartmentName() {
                List<DepartmentBean> departments = new ArrayList<DepartmentBean>();
                try {
                    Statement statement = connection.createStatement();
                    ResultSet rs = statement.executeQuery("select department_name,id from department ORDER BY department_name");
                    while (rs.next()) {
                        DepartmentBean department = new DepartmentBean();
                        department.setPeople_manager_name(rs.getString("department_name"));
                        departments.add(department);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }

                return departments;
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user3496683
  • 25
  • 1
  • 6
  • 2
    I could search online and provide an answer, but I recommend you **to not use `` at all**. Move that query to your Servlet and provide the data from there rather than directly from your JSP. – Luiggi Mendoza Jan 07 '15 at 16:33
  • But for this scenario do you have an idea on how can I fix it? – user3496683 Jan 07 '15 at 16:47
  • 1
    Yes, but won't provide such answer. I won't support bad design. – Luiggi Mendoza Jan 07 '15 at 16:48
  • Can you show me some example on which – user3496683 Jan 07 '15 at 16:57
  • Here's an example: http://stackoverflow.com/questions/5003142/jsp-using-mvc-and-jdbc Your mistake is by the way a simple typo. Look closer at the base reference in `${}` of option value. – BalusC Jan 07 '15 at 18:55
  • I have a problem I changed the design but the values/items inside the drop down are not showing. I edited my question above already – user3496683 Jan 08 '15 at 10:40
  • Great, now you're following a better approach. Now the problem is that you're not fulfilling the data in your `DepartmentBean` object reference in the dao class. You're only filling `people_manager_name` field, and in your JSP you're requesting the data for `department_id` and `department_name`, which will call `DepartmentBean#getDepartment_id()` and `DepartmentBean#getDepartment_name()` methods. Please review how [expression language works](http://stackoverflow.com/tags/el/info). – Luiggi Mendoza Jan 08 '15 at 14:49
  • It's already working Thanks! – user3496683 Jan 08 '15 at 16:57

1 Answers1

0

The problem is that you're not fulfilling the necessary fields in your DepartmentBean object reference in the dao class. You're only filling people_manager_name field, and in your JSP you're requesting the data for department_id and department_name, which will call DepartmentBean#getDepartment_id() and DepartmentBean#getDepartment_name() methods.

Change this in getAllDepartmentName method:

while (rs.next()) {
    DepartmentBean department = new DepartmentBean();
    department.setDepartment_name(rs.getString("department_name"));
    department.setDepartment_id(rs.getInt("department_id"));
    departments.add(department);
}

Here's a useful link about how expression language works.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332