0

I am trying to avoid java codes in jsp for drop down menu. i dont know if my codes are correct in the first place (i bet there are other errors) BUT my real problem is, i dont know how to allow drop down menu in JSP to receive data from servlet. Basically I want the user to select any lecturer name from drop down menu, then it will display the chosen lecturer's information to update (well, thats another story). I hope someone can help. Thank you in advance!

this is my jsp code:

<form action="updateLecturer" class="sky-form">
<header>Update Lecturer Information</header>
<center>
<fieldset>

<section>
<label class="select">
<select name="selectLecturer" id="lecturerFullname">
<option value="0" selected disabled>Lecturers Name</option>        
**<option name="lecturerFullname">I HAVE PROBLEM IN HERE!!!</option>** 
</select>

</label>
</section>

</center>
<footer>
<center><button type="submit" class="button">Update</button></center>
</footer>
</form>

This is my servlet (not sure if its right anyway):

public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {


        try {

            LecturerBean lecturer = new LecturerBean();
   lecturer.setLecturerFullname(request.getParameter("lecturerFullname"));

                        response.sendRedirect("updateLecturer.jsp");


        }

        catch (Throwable theException) {
            System.out.println(theException);
        }
    }

This is my web.xml file :

<servlet>
        <servlet-name>UpdateLecturerServlet</servlet-name>
        <servlet-class>CSP600.UpdateLecturerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UpdateLecturerServlet</servlet-name>
        <url-pattern>/updateLecturer</url-pattern>
    </servlet-mapping>

This is my DAO:

 static Connection currentCon = null;
    static ResultSet rs = null;

    public static LecturerBean lecturerlogin(LecturerBean Lbean) {

        // preparing some objects for connection
        System.out.println("JIJIJI");
        Statement stmt = null;

        String lecturerID = Lbean.getLecturerID();
        String lecturerFullname = Lbean.getLecturerFullname();
        String lecturerPassword = Lbean.getLecturerPassword();

        System.out.println("j444444");


        String searchQuery = "select lecturerfullname from lecturer";

        System.out.println("Your lecturer is " + lecturerFullname);

        System.out.println("Query: " + searchQuery);

        try {
            // connect to DB
            currentCon = JavaConnectionDB.getConnection();
            stmt = currentCon.createStatement();
            rs = stmt.executeQuery(searchQuery);
           // boolean more = rs.next();

            while(rs.next())
            {
            rs.getString(lecturerFullname);
            }
        }

        catch (Exception ex) {
            System.out.println("Log In failed: An Exception has occurred! " + ex);
        }

        // some exception handling
        finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception e) {
                }
                rs = null;
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                }
                stmt = null;
            }

            if (currentCon != null) {
                try {
                    currentCon.close();
                } catch (Exception e) {
                }

                currentCon = null;
            }
        }

        return Lbean;

    }
Mong2203
  • 71
  • 1
  • 12

1 Answers1

0

What you pass into the DAO and what returns from it - are one and the same. This code also doesn't make any sense:

while(rs.next()) {
    rs.getString(lecturerFullname);
}

You need to create an instance of the DAO object in the doGet() method, then populate some list and set the query attribute:

req.setAttribute("lecturerList", lecturerList);
RequestDispatcher rd = req.getRequestDispatcher("updateLecturer.jsp");
rd.forward(req, res);

Then populate the menu in the JSP page by using JSTL:

<select>
     <c:forEach var="lecturer" items="${lecturerList}">
          <option value="${lecturer.id}">${lecturer.fullName}</option>
     </c:forEach>
</select>