• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing my object in JSP

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>");
}
%>
...
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
What's brown and sticky? ... a stick. Or a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic