File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Accessing my object in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Accessing my object in JSP" Watch "Accessing my object in JSP" New topic
Author

Accessing my object in JSP

Gajan Raj
Greenhorn

Joined: Sep 19, 2003
Posts: 12
Hi, I am a newbie to JSP and am having difficulty acessing an object in JSP. What I am trying to do is this - I have a JSP page with a form and on submitting it a servlet is called. The servlet using the parameters passed creates a SQL query and calls java method which connects to the db and retrieves the required employee records into a ResultSet. I create an employee object and populate the attributes with the row values. and put each employee object into a linked list and return it to the calling servlet.
I then pass this back to a a JSP page for display. When i try to retrieve the values from the employee object i am getting a message - Symbol nor resolved - Basically the JSP does not recognise my employee object. I would appreciate some help with this. cheers.
Also would appreciate if you could let me know if i am doing anything wrong with the following bits of code:
I am using the code shown below in the java class to popluate the linkedlist with employee object -
...
public static List list = new LinkedList();
...
results = stmt.executeQuery();
if (dataType == "Employee")
{
while(results.next())
{
Employee employee = new Employee(results.getString("EmployeeTitle"),
results.getString("EmployeeSurname"));
list.add(employee);
}
}
I am using the following bit of code to send the linkedlist to JSP page:
request.setAttribute("employees", list);
I am using the following bit of code in JSP page where i am trying to display the records in linkedlist
<%@ page import= "HousingManger.Employee" %>
...
<%
List employees = (List) request.getAttribute("employees");
ListIterator listX = employees.listIterator();
while(listX.hasNext())
{
Employee emp =new Employee();
emp = listX.next();

String varEmployeeSurname = emp.getEmployeeTitle();
String varEmployeeTitle = emp.EmployeeSurname();

out.println("<table><tr><td>" + varEmployeeSurname + "</td><td>" + varEmployeeTitle + "</td></tr></table>");
}
%>
...
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
Try using this, rather than getting from the request object

Now you can use "employees" as the object of the desired class.
Hope this helps.


Sanjeev Kaushik
Anand Loganathan
Greenhorn

Joined: Sep 13, 2003
Posts: 12
Actually the problem in your code while extracting the value from List,
you are not typecasting the extracted value with bean Object.
Employee emp =new Employee();// Initialization
emp = listX.next(); // Your code. You have to do
// typecasting to bean object.
emp = (Employee)listX.next(); //Correct

you just try the above the code, and it will work fine.
Gajan Raj
Greenhorn

Joined: Sep 19, 2003
Posts: 12
Thnx Sanjeev and Anad. I used the type casting and usebean tag in the code and it complied without errors. However I am still having trouble getting it to work.
When I try to do a search the showemployee page does not load instead i am getting :
The server encountered an internal error () that prevented it from fulfilling this request
....
....
root cause
java.lang.ClassCastException
....
I think this is because what is being passed from servlet to JSP is a linkedList containing Employee objects -
request.setAttribute("employees", list);
but i am defining it as Employee class in the JSP page
<jsp:useBean id="employees" class="HousingManager.Employee" scope="request"/>
How do i get around this problem...any help would be appreciated very much...
cheers.
Gajan
Gajan Raj
Greenhorn

Joined: Sep 19, 2003
Posts: 12
Hi
Got it fully working now. My second JSP now includes the following code which makes sure that the JSP recognises both the Linked List passed through and the Employee object stored in it.
<%@ page import= "HousingManager.Employee" %>
<jsp:useBean id="employees" class="java.util.LinkedList" scope="request"/>

Cheers for your help...
Gajan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing my object in JSP
 
Similar Threads
Pagination in JSP
A small web application
html Checkbox value not getting
database with jsp
In the same jsp page I want to display data fetched from database