0

I have DB structure like :

enter image description here

This is doGet() of Controller Servlet as :

protected void doGet(HttpServletRequest request,
                    HttpServletResponse response) throws ServletException, IOException {
    getSubjects(request, response);
}

and getSubjects(request, response) is :

private void getSubjects(HttpServletRequest request,
                    HttpServletResponse response) throws ServletException{
            try {
                    List<Subject> subjects = subjectDAO.subjectList();// I'm sending subjects List as Arttribute and, Please SEE *** below
                    ### HERE by passing subject ID Here.
                    List<Page> pages = pageDAO.topicList(subjectId); // Now I want to send pages List as Arttribute as well and, Please SEE### below
                    request.setAttribute("subjects", subjects);
                    request.setAttribute("pages", pages);
                    getServletContext().getRequestDispatcher("/view.jsp").forward(
                                    request, response);
            } catch (Exception e) {                        
                    throw new ServletException("Error ", e.getCause());
            }
    }

and this is my JSP

<ul class="subjects">    *** HERE below I'm getting that subjects List.
                        <c:forEach items="${subjects}" var="subject">
                          <li
                            <c:catch>
                              <c:choose>
                                <c:when test="${subject.subjectId == param.subj_id}">
                                <c:out value=" class=\"selected\""/>
                                </c:when>
                              </c:choose>    
                            </c:catch>><a href='/contexct/super-controller?action=view-content&subj_id=
                           <c:out value="${subject.subjectId}"/>'>
                           <c:out value="${subject.subjectName}" />
                           </a></li>
                           <ul class="pages">
                            ### HERE below I can get that pages List .But how can I get value of subjectId So that I can get pages from DB, Please SEE$$$ above.
                            <c:forEach items="${pages}" var="page">
                                <li
                                  <c:catch>
                                     <c:choose>
                                        <c:when test="${page.pageId == param.topic_id}">
                                        <c:out value=" class=\"selected\""/>
                                       </c:when>
                                     </c:choose>    
                                   </c:catch>><a href='/contexct/super-controller?action=view-content&amp;topic_id=
                             <c:out value="${page.pageId}"/>'>
                             <c:out value="${page.pageName}" />
                           </a></li>
                         </c:forEach>
                       </ul>
                     </c:forEach>
                    </ul>

As I'm getting Subject data in jsp through :

<c:forEach items="${subjects}" var="subject">

Now how can I pass subject ids to this line in getSubjects():

 List<Page> pages = pageDAO.topicList(subjectId);

So that I can get pages corresponding to subject and set that list as :

 request.setAttribute("pages", pages);

Hope now you can understand my problem

UPDATE

I debugged in this way :

private void getSubjects(HttpServletRequest request,
        HttpServletResponse response) throws ServletException{
    try {
        List<Subject> subjects = subjectDAO.subjectList();
        List<Page> pages = null;
        for (int i = 0; i < subjects.size(); i++) {
            pages = pageDAO.topicList(subjects.get(i).getSubjectId());
            System.out.println("List id is " + subjects.get(i).getSubjectId());
                            for (int i = 0; i < subjects.size(); i++) {
            pages = pageDAO.topicList(subjects.get(i).getSubjectId());
            System.out.println("List id is " + subjects.get(i).getSubjectId());
            for (int j = 0; j < pages.size(); j++) {                    
                System.out.println("Topics are " +pages.get(j).getTopicName());
            }
        }
        }
        request.setAttribute("subjects", subjects);
        request.setAttribute("pages", pages);
        getServletContext().getRequestDispatcher("/content.jsp").forward(
                request, response);
    } catch (Exception e) {             
        throw new ServletException("Cannot obtain subjects from DB", e.getCause());
    }
}

I got Console as :

List id is 1
Topics are App User Interface
List id is 2
Topics are Java Basics
Topics are OOP
List id is 3
List id is 4

which is correct, but could not be able to get this work

<c:forEach items="${pages}" var="page">
     <li
       <c:catch>
         <c:choose>
           <c:when test="${page.pageId == param.topic_id}">...

given above..

means nothing is returned there.

11
  • 2
    hello sorry what is your question?
    – John Hogan
    May 15, 2014 at 6:59
  • When you are getting subjects, pass the first subject_id to fetch its pages on first load of page and then when changing subject pass subject_id back to action and fetch pages accordingly. May 15, 2014 at 7:17
  • request.getParameter("subj_id") docs.oracle.com/javaee/6/api/javax/servlet/… May 15, 2014 at 7:42
  • As I earlier mentioned, send just first object's subject_id on initial,, as: List<Subject> subjects = subjectDAO.subjectList(); List<Page> pages = pageDAO.topicList(subjects.get(0).getSubjectId()); May 15, 2014 at 7:48
  • String[] array =request.getParameterValues('subj_id'); convert that to list May 15, 2014 at 7:48

2 Answers 2

0

Okay I had to perform a similar task. I used a controller servlet. Then a database object that used classes to handle the retrieving of data from the data base. It doesn't appear that you are checking the "action" parameter being sent to the servlet.

if (action.equalsIgnoreCase("getTopic")){
            int SubjectId = Integer.parseInt(request.getParameter("SubjectId"));
            dao.getTopic(SubjectId);
            forward = "myjsp.jsp";
            request.setAttribute("subjects", dao.getAllSubjects());    
        } else if (action.equalsIgnoreCase("listSubjects")){
            forward = "myjsp.jsp";
            request.setAttribute("categories", dao.getAllSubjects());

        } else {
            forward = "myjsp.jsp";
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }
0

If you want to get pages from Map try following approach (Not Tested):

private void getSubjects(HttpServletRequest request, HttpServletResponse response) 
throws ServletException{

    try {
        List<Subject> subjects = subjectDAO.subjectList();
        List<Page> pages = null;
        Map<String, ArrayList<Page>> subjectPageMap = null;

        if(subjects!= null && subjects.size() > 0){
            subjectPageMap = new HashMap<String, ArrayList<Page>>();
            for(Subject subject : subjects){
                pages = pageDAO.topicList(subject.getSubjectId());
                pages = pages != null && pages.size() > 0 ? pages : new ArrayList<Page>();
                subjectPageMap.put(subject.getSubjectId(), pages);
            }
        }

        request.setAttribute("subjects", subjects);
        request.setAttribute("subjectPageMap", subjectPageMap);
        getServletContext().getRequestDispatcher("/view.jsp").forward(
                                request, response);
    } catch (Exception e) {
        throw new ServletException("Error ", e.getCause());
    }
}
6
  • Map<int, ArrayList<Page>> subjectPageMap = null; gives error at Map<int..
    – user3310291
    May 15, 2014 at 8:35
  • what is datatype of your subjectId? May 15, 2014 at 8:37
  • I typed that as String
    – user3310291
    May 15, 2014 at 8:39
  • Alas! still I'm not getting pages Man!
    – user3310291
    May 15, 2014 at 8:47
  • But why, if your method (pageDAO.topicList) is working fine for given subjectId, then it should populate Map. Try debugging your code and find the root of the issue. Share exception, if you can't handle. May 15, 2014 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.