java - Retrieving data from JSP-Class (MVC with Servlet) -
im working on school project me , group supposed build mvc-project have classes building , room. relation onetomany, building-(onetomany)-room. problem occurs when want search building name , later show rooms in building. can extract relevant data when run our servlet without our jsp, when start project running our searchbuilding.jsp can type building name, , when expected see connected rooms, instead message "java.lang.classcastexception: java.util.arraylist cannot cast org.ics.ejb.room". might bit messy code appreciate if me spot problem since im supposed show professor tomorrow.
this our code: building:
@entity @table(name = "building") public class building { private string bname; private list<room> rooms; // building can have many rooms @id @column(name = "bname") public string getbname() { return bname; } public void setbname(string bname) { this.bname = bname; } @onetomany(mappedby = "building", fetch = fetchtype.eager) public list<room> getrooms() { return rooms; } public void setrooms(list<room> rooms) { this.rooms = rooms; }
}
room:
@namedqueries({ @namedquery(name="room.findbybname", query="select r room r r.bname :bname"), }) @entity @table(name = "room") public class room implements serializable { /** * */ private static final long serialversionuid = 1l; private roomid id; private string bname; private building building; public string getbname() { return bname; } public void setbname(string bname) { this.bname = bname; } @id public roomid getid() { return id; } public void setid(roomid id) { this.id = id; } @manytoone @joincolumn(name = "bname", insertable = false, updatable = false) public building getbuilding() { return this.building; } public void setbuilding(building building) { this.building = building; } }
roomid:
@embeddable public class roomid implements serializable { private string bname; private string rcode; public roomid() { } public roomid(string bname, string rcode) { this.bname = bname; this.rcode = rcode; } @column(name = "bname", nullable = false) public string getbname() { return bname; } public void setbname(string bname) { this.bname = bname; } @column(name = "rcode", nullable = false) public string getrcode() { return rcode; } public void setrcode(string rcode) { this.rcode = rcode; } public boolean equals(object other) { if ((this == other)) { return true; } if ((other == null)) { return false; } if (!(other instanceof roomid)) { return false; } roomid castother = (roomid) other; return ((this.getbname() == castother.getbname()) || (this.getbname() != null && castother.getbname() != null && this.getbname().equals(castother.getbname()))) && ((this.getrcode() == castother.getrcode()) || (this.getrcode() != null && castother.getrcode() != null && this.getrcode().equals(castother.getrcode()))); } public int hashcode() { return super.hashcode(); }
}
buildingeao:
@stateless public class buildingeaoimpl implements buildingeaoimpllocal { @persistencecontext(unitname = "labejbsql") private entitymanager em; public buildingeaoimpl() { // todo auto-generated constructor stub } public building findbybname(string bname) { return em.find(building.class, bname); } }
facade:
@stateless public class facade implements facaderemote, facadelocal { @ejb buildingeaoimpllocal buildingeao; @ejb roomeaoimpllocal roomeao; public facade() { // todo auto-generated constructor stub } public list<room> findroomsbybname(string bname) { return roomeao.findbybname(bname); } }
servlet:
@webservlet("/testclientservlet") public class testclientservlet extends httpservlet { private static final long serialversionuid = 1l; @ejb private facadelocal facade; /** * @see httpservlet#httpservlet() */ public testclientservlet() { super(); // todo auto-generated constructor stub } protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); out.println("<!doctype html><html><head>"); out.println("<title>lab1</title>"); out.println("<meta charset=\"iso-8859-1\">"); out.println("</head><body>"); /*//this works fine list<room> rooms = facade.findroomsbybname("ec2"); (room r : rooms) { out.println("<h4>hittade: " + r.getid().getbname() + " " + r.getid().getrcode() + "</h4>"); out.println("</body></html>"); } }*/ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { printwriter out = response.getwriter(); out.println("testclientservlet-doget"); out.close(); } protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string url = null; // hidden field string operation = request.getparameter("operation"); if (operation.equals("showbuilding")) { string bname = request.getparameter("txtbname"); list<room> r = facade.findroomsbybname(bname); request.setattribute("rooms", r); url = "/showbuilding.jsp"; } else if (operation.equals("searchbuilding")) { system.out.println("testclientservlet-searchbuilding"); url = "/searchbuilding.jsp"; } else { url = "/searchbuilding.jsp"; } system.out.println(url); requestdispatcher dispatcher = getservletcontext() .getrequestdispatcher(url); dispatcher.forward(request, response); } */ }
searchbuilding.jsp:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=iso- 8859-1"> <title>search building</title> </head> <body> <form action="/buildroomclientproject/testclientservlet" method="post"> <table cellspacing="0" cellpadding="0" border="0" align="left"> <tr> <td><h2>search building:</h2></td> </tr> <tr> <td> <input type= "text" name= "txtbname" size ="25" maxlength="25"> <input type="submit" name="submit" value="search" /> </td> <td></td> </tr> </table> <input name="operation" value="showbuilding" type="hidden"> </form> </body> </html>
showbuilding.jsp:
<%@ page contenttype="text/html;charset=windows-1252"%> <%@ page import = "org.ics.ejb.building" %> <%@ page import = "org.ics.ejb.room" %> <%@ page import = "org.ics.ejb.roomid" %> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title> show building </title> </head> <body> <% room r = (room)request.getattribute("rooms"); %> <% building b = (building)request.getattribute("building"); %> <% (int i=0; i<100; i++){ system.out.println(r.getbname() + " " + r.getid().getrcode()); }%> <h2> building: </h2> <p> <%= r.getid().getrcode()%> <%= b.getbname()%> </p> <form action="/buildroomclientproject/testclientservlet" method="post"> <input type="submit" name="submit" value="tillbaka"> <input name="operation" value="searchbuilding" type="hidden"> </form> </body> </html>
the "rooms" attribute being set arraylist of rooms in servlet. when trying in jsp, expecting single room. change
<% room r = (room)request.getattribute("rooms"); %>
to
<% list<room> rooms = (list<room>)request.getattribute("rooms"); %>
and iterate through each room.
Comments
Post a Comment