Please Help on ControllerServlet-DataAccessObject-JSP

[nobr]Hello,

First of all sorry if this seems an obvious simple issue. I am new to Servlets and JSP and am trying to develop an application using these technologies.

I once found a question on the forum but can't seem to locate the link for reference. Many thanx to those guys because the code is from their discussion and am just trying to understand it.

I will later get the records from the database using the same concept + some other design patterns I have read on(action classes etc).

I have a controller servletTestControllerServlet.java which forwards the request to a jsp page for display. The servlet gets ArrayList (from the data access object)of objects and stores it inside an HTTP Request then forwards the request to a JSP page. Each record is store a JavaBean instance.

When I access the controller servlet I don't get the info from the records(I just get a blank page).I have included the code below:

TestControllerServlet]

package test_javabean;

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.util.ArrayList;

publicclass TestControllerServletextends HttpServlet{

publicvoid processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

response.setContentType("text/html;charset=UTF-8");

//Getting the ArrayList of Objects from the database

DeclarationDataAccess declarationDataAccess =new DeclarationDataAccess();

ArrayList<DeclarationBean> dataRecords = declarationDataAccess.getDeclarationRecords();

//Store the ArrayList of objects into HTTP Request

request.setAttribute("dataRecords", dataRecords);

//Forward the HTTP Request to a JSP page

RequestDispatcher reqstDisp =

getServletContext().getRequestDispatcher("/TestDeclarationView.jsp");

reqstDisp.forward(request,response);

}

publicvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

processRequest(request, response);

}

publicvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

processRequest(request, response);

}

}

TestDeclarationView

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>

<head>

<title>JSP Page</title>

</head>

<body>

<jsp:useBean id="declarationRecords" class="java.util.ArrayList" scope="request"/>

<h1>Test Declaration</h1>

<c:forEach var="currentRecord" items="${declarationRecords}">

First Name: ${currentRecord.firstName}</br>

Surname: ${currentRecord.surName}

</c:forEach>

</body>

</html>

DeclarationDataAccess

package test_javabean;

import java.util.*;

publicclass DeclarationDataAccess{

public ArrayList<DeclarationBean> getDeclarationRecords(){

ArrayList<DeclarationBean> declarationRecords =new ArrayList<DeclarationBean>();

DeclarationBean declarationRecord;

declarationRecord =new DeclarationBean();

declarationRecord.setFirstName("AiEx");

declarationRecord.setSurName("Kuomboka");

declarationRecords.add(declarationRecord);

return declarationRecords;

}

}

DeclarationBean

package test_javabean;

import java.sql.*;

import java.io.*;

publicclass DeclarationBeanimplements Serializable{

private String firstName;

private String surName;

public DeclarationBean(){

}

public String getFirstName(){

return firstName;

}

publicvoid setFirstName(String firstName){

this.firstName = firstName;

}

public String getSurName(){

return surName;

}

publicvoid setSurName(String surName){

this.surName = surName;

}

}

problems:

1. I can't understand why am getting a blank page

2. in the TestDeclarationView.jsp I haven't understood well(from the example on the forum) why the class in <jsp:useBean id="declarationRecords" class="java.util.ArrayList">

I thought it should be <jsp:useBean id="declarationRecords" class="test_javabean.DeclarationDataAccess">

but when I do so I get the error: "javax.servlet.ServletException: Don't know how to iterate over supplied "items" in <forEach>;"

Thank you in advance for the help.[/nobr]

[7524 byte] By [aiExa] at [2007-11-27 5:48:39]
# 1

I always use scriplets instead of the JSP tag libs so lost touch on the JSP tag libs. Still , probably I can help you. The only problem I see in the code is regarding the id.

Please change the following line of code

<jsp:useBean id="declarationRecords" class="java.util.ArrayList" scope="request"/>

to following

<jsp:useBean id="dataRecords" class="java.util.ArrayList" scope="request"/>

probably its the name which is creating the problem.

Hope this helps.

Thanks

anurag_dasha at 2007-7-12 15:34:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thank a lot!! Your right and its working. I even managed to get records from the DataBase :-) ....promising begining
aiExa at 2007-7-12 15:34:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...