Stan Murdoch
Stan Murdoch

Reputation: 11

Java Servlet send data to JSP page

I just started programming with Java Servlet and JSP. How do i implement a Controller->View setup by using a Servlet->JSP approach. I basically want to separate logic from presentation by making the Servlet output its data to a JSP file which then displays the page.

Upvotes: 1

Views: 3229

Answers (2)

dkea
dkea

Reputation: 708

PART JSP PAGE using JSTL:

<c:forEach var="workload" items="${workList}">
            <tr class="font" style="height: 32px">
                <td scope="row" >${workload.details}</td>
                <td >${workload.datestart}</td>
                <td >${workload.status}</td>
                <td >${workload.membername}</td>
            </tr>
</c:forEach>

PART of the SERVLET which PASSES DATA TO JSP PAGE:

ArrayList<Workload> workList = new Leader_DAO().getProjectWorkload(request.getParameter("projectid"));
request.setAttribute("workList", workList);
RequestDispatcher rd = request.getRequestDispatcher("yourfolder/yourpage.jsp");
rd.forward(request, response);

The code flow: Declare your object. Call the DAO(DATA ACCESS OBJECT) to get the data you need -- with a parameter if needed. Set an attribute with the value of what you're getting to be passed to the JSP page. Request the page where data shall be passed. Forward the request.

Just comment here if you need more solid help and code :) @Stan Murdoch

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240946

Set the data in session/request or any appropriate scope from the Servlet (Controller) and use JSTL on view to render it.

See Also

Upvotes: 4

Related Questions