How to divide this DROPDOWN menu codes to servlet, dao and JSP?

This question already has an answer here:

  • Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern 5 answers
  • How to avoid Java code in JSP files? 28 answers

  • Create Servlet for the connection code and use JSTL to avoid Java code in the JSP file. Also use Request Dispatcher to set the values in an attribute in Servlet and then JSTL will allow you to get Attributes from the servlet with its own tag and that will solve your problem of avoiding Java code in JSPs.

    Servlet Sample Code :

    request.setAttribute("error_userID", "UserID is not Correct"); //setting error msg in an attribute request.getRequestDispatcher("/jsp/common/login.jsp").forward(request, response); //passing attribute to jsp

    Sample JSP Code

    < c:out value="${requestScope.error_userID}" >

    tag named will let you set your dropdown value too in jsp and value comes through Servlet through request Object.

    Enjoy Coding.. :) Thanks !!


    Write as mentioned by Babak Behzadi

  • Seperate DAO Class for DB connecivity
  • Servlet
  • JSP
  • In Your JSP file , Write JSTL Code... as

    <select name="name" id="id-select">
        <c:forEach items="<%=request.getAttribute("list")%>" var="i"> 
                    <option value="${i }">${i }</option>
        </c:forEach>
    </select>

    For example:

    Lecturer POJO class:

    class Lecturer {
    
        ...
        private String fullName;
        ...
    
       //getters and setters
    }
    

    DAO class:

    class MyDAO {
    
       public List<Lecturer> getLecturers() {
         try{
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url="jdbc:oracle:thin:@localhost:1521:xe";
            String username="sys as sysdba";
            String password="sys";
            String query="select lecturerFullname from lecturer";
            Connection con=DriverManager.getConnection(url,username,password);
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(query);
    
            List<Lecturer> list = new LinkedList<Lecturer>();
    
            while(rs.next()) {
                String fullname= rs.getString("lecturerFullname");
                Lecturer lec = new Lecturer();
                lec.setFullName(fullname);
                ...
                list.add(lec);
            }
         } catch(Exception e) {
         }
         return list;
       }    
    }
    

    In your servlet:

    class MyServlet {
    
       MyDAO myDao = ...;
    
       doGet() {
    
          List<Lecturer> list = myDoa.getLecturers();
          request.setAttribute("list",list); 
    
       }
    }
    

    and finally in the jsp file, you need to get list and iterate over. ;)

    EDITED

    JSP file:

    <select>
    <%
       List<Lecturer> list = (List<Lecturer>) request.getAttribute("list");
       for(Lecturer lec : list) {
    %>
       <option><%=lec.getFullName()%></option>
    <%
       }
    %>
    </select>
    
    链接地址: http://www.djcxy.com/p/76612.html

    上一篇: 来自JSP的DAO调用符合MVC模式?

    下一篇: 如何将DROPDOWN菜单代码划分为servlet,dao和JSP?