-
August 31st, 2009, 10:39 AM
#1
Passing more than one vector to a JSP
I can pass one vector to a JSP t be displayed (The clientDetails) and I also want to pass the houseDetails to the same JSP to be displayed. The servlette is
Code:
private void goSearchById(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException,
SQLException, NamingException, InstantiationException,
IllegalAccessException, ClassNotFoundException {
int id = Integer.parseInt(request.getParameter("id"));
Client client = new Client();
House house = new House();
client = client.getSingleClient(id);
request.setAttribute("clientDetails", client);
String stringURL1 = getStringURL(client);
house = house.getHouseById(id);
request.setAttribute("houseDetail", house);
RequestDispatcher rd = request.getRequestDispatcher(stringURL1);
rd.forward(request, response);
}
which uses the method
Code:
public House getHouseById(int id) throws SQLException, NamingException, InstantiationException, IllegalAccessException, ClassNotFoundException{
DbBean db = new DbBean();
db.connect();
StringBuffer qry = new StringBuffer(1024);
ResultSet rs = null;
qry.append("select * from house where clientId="+id+";");
House house = new House();
rs = db.doQuery(qry);
while (rs.next()) {
// first retrieve items from db
clientId = rs.getInt("clientId");
houseId = rs.getInt("houseId");
NumberOfFloors = rs.getInt("NumberOfFloors");
NumberOfBathrooms = rs.getInt("NumberOfBathrooms");
NumberOfKitchens = rs.getInt("NumberOfKitchens");
NumberOfLivingRooms = rs.getInt("NumberOfLivingRooms");
HeatingType = rs.getString("HeatingType");
IndividuallyHeatedRooms = rs.getString("IndividuallyHeatedRooms");
house.setClientId(clientId);
house.setHouseId(houseId);
house.setNumberOfFloors(clientId);
house.setNumberOfBathrooms(NumberOfBathrooms);
house.setNumberOfKitchens(NumberOfKitchens);
house.setNumberOfLivingRooms(NumberOfLivingRooms);
house.setHeatingType(HeatingType);
house.setIndividuallyHeatedRooms(IndividuallyHeatedRooms);
}
db.close();
return house;
}
and I am trying to display it on JSP
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" href="mystyles.css" type="text/css" />
<title>Client Details</title>
</head>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="client" class="com.technabling.models.Client" scope="session"/>
<%@ page language="java" import="java.util.Iterator, java.util.Vector, com.technabling.models.Client"%>
<body>
<h1> Full Client Details</h1>
<h3>${clientDetails.firstName} ${clientDetails.lastName}</h3>
<strong>Age:</strong> ${clientDetails.age} <strong>Sex:</strong> ${clientDetails.sex}
${houseDetails.houseId}
${houseDetails.NumberOfFloors}
${houseDetails.NumberOfBathrooms}
${houseDetails.NumberOfKitchens}
${houseDetails.NumberOfLivingRooms}
${houseDetails.HeatingType}
${houseDetails.IndividuallyHeatedRooms}
<a class="button" href="Technabling?action=deleteClient&id=${ViewClient.clientId}"><span>Delete Client</span></a>
</body>
</html>
DOn't worry about the layout I iwll play witht hat later.
Thanks anyone who can help.
-
August 31st, 2009, 11:10 AM
#2
Re: Passing more than one vector to a JSP
Again, is there a specific question about this?
Just a comment about your code (in both this and you previous post): you use StringBuffer/StringBuilder to avoid concatenating Strings directly using the + operator because of the unnecessary String instances that are created. This piece of code:
Code:
StringBuffer qry = new StringBuffer(1024);
qry.append("select * from house where clientId="+id+";");
does not make a lot of sense, because you are actually doing what you wanted to avoid by using StringBuffer. You should use:
Code:
StringBuffer qry = new StringBuffer(1024);
qry.append("select * from house where clientId=").append(id);
or even better:
Code:
StringBuffer qry = new StringBuffer("select * from house where clientId=");
qry.append(id);
-
August 31st, 2009, 01:16 PM
#3
Re: Passing more than one vector to a JSP
What's the problem? You can pass any quantity of objects with request or better session just as you do: request.setAttribute("clientDetails", client);
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|