Jump to content

Try to make it more efficiency....

- - - - -

  • Please log in to reply
147 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello guyz,

Im tring to update my shopping webpage. I have 3 JSPs (main page - list of goods - good page). The main idea, click a category to main page, transfer you to list of correspoding goods and then click on good's name and transfer you to good page. Until now is working but i wanna make it more efficiency because in good page JSP, compile for each item name, image, price from XML file again and again and again.

Example:

if(request.getParameter("good").equals("1")){

		

		groName = xpath.compile("/madShop/category[@id='Grosery']/goodsItem[2]/*[3]");

		grosNmNode = groName.evaluate(dom, XPathConstants.NODESET);

		nodeGrosNm = (NodeList) grosNmNode;%>

			<img src="<%=nodeGrosNm.item(0).getTextContent()%>" height="148" width="198" ondblclick="height='148'; width='198'; border='0';" onclick="height='248'; width='298'; border='2';" style="position:fixed; top:210; left:30;"/>

			

		<%groName = xpath.compile("/madShop/category[@id='Grosery']/goodsItem[2]/*[1]");

		grosNmNode = groName.evaluate(dom, XPathConstants.NODESET);

		nodeGrosNm = (NodeList) grosNmNode;%>

			<h2 style=" color:#006600; position:fixed; left:400; top:200;"><%=nodeGrosNm.item(0).getTextContent()%></h2>

		

		

		<%groName = xpath.compile("/madShop/category[@id='Grosery']/goodsItem[2]/*[2]");

		grosNmNode = groName.evaluate(dom, XPathConstants.NODESET);

		nodeGrosNm = (NodeList) grosNmNode;%>

			<h6>Price: £<%=nodeGrosNm.item(0).getTextContent()%>/kg</h6>

<%}%>
I have 15 items, so im doing this 15 times. Bad coding i know :S
As you can see from if statement, i gave to each item unique id number, so i thought use it inside compiler


int goodId = Integer.parseInt(request.getParameter("good"));

<%groName = xpath.compile("/madShop/category[@id='Grosery']/goodsItem[goodId]/*[2]");

		grosNmNode = groName.evaluate(dom, XPathConstants.NODESET);

		nodeGrosNm = (NodeList) grosNmNode;%>

...but always show me the first item's details
HELP PLEASE

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I hope it's a typo that you open '<%' at line 2 instead of 1 there...

int goodId = Integer.parseInt(request.getParameter("good"));

[B][COLOR="#2e8b57"]<%[/COLOR][/B]groName = xpath.compile("/madShop/category[@id='Grosery']/goodsItem[goodId]/*[2]");

		grosNmNode = groName.evaluate(dom, XPathConstants.NODESET);

		nodeGrosNm = (NodeList) grosNmNode;%>

?
Also show the goodId you get and verify it are indeed the correct values:

<%int goodId = Integer.parseInt(request.getParameter("good"));%>

<p>goodId: <%goodId%></p>

[B][COLOR="#2e8b57"]<%[/COLOR][/B]groName = xpath.compile("/madShop/category[@id='Grosery']/goodsItem[goodId]/*[2]");

		grosNmNode = groName.evaluate(dom, XPathConstants.NODESET);

		nodeGrosNm = (NodeList) grosNmNode;%>


edit: Try this first
I also noticed you didn't close your string to input the variable, therefore goodId is handled as String and not as variable.
Try

xpath.compile("/madShop/category[@id='Grosery']/goodsItem[[B][COLOR="lime"]" + goodId + "[/COLOR][/B]]/*[2]");



#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Thanks mate,
yes was a typo, sorry about that. I changed
xpath.compile("/madShop/category[@id='Grosery']/goodsItem[goodId]/*[2]");

with
xpath.compile("/madShop/category[@id='Grosery']/goodsItem[" + goodId + "]/*[2]");
and working fine.
Now problem is the rest of categories. Because as you can see, i have id for each category also. Problem is that, from Jsp of goods list to good page jsp, category parameter is null. How i can transfer it or save it, so i can use it later on?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
How are you currently handling the transfers from goods list to goods page?
Is it a form where you select something, and press a button?
Or is it a bunch of links like:
<a href="goodPage.jsp?goodId=2" />
?

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
My mistake, yes im using a link <a href="goodPage.jsp?good=2" /> from goods list to good page and from main page to goods list again <a href="goodsList.jsp?category=(something)" />

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think you can make up the links on goodsList page like so:

<a href="goodPage.jsp?good=2&category=<%request.getParameter("category")%>" />

And then you can access the category on the goodPage.

request.getParameter("category")



#7
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
In good page im doing:

<%int goodId = Integer.parseInt(request.getParameter("good"));

String catName = request.getParameter("category");%>


<%groName = xpath.compile("/madShop/category[@id='"+catName+"']/goodsItem[" + goodId + "]/*[2]");

		grosNmNode = groName.evaluate(dom, XPathConstants.NODESET);

		nodeGrosNm = (NodeList) grosNmNode;%>

Working perfectly for first category of XML but for the rest i have exception here:
<img src="<%=nodeGrosNm.item(0).getTextContent()%>" height="148" width="198" ondblclick="height='148'; width='198'; border='0';" onclick="height='248'; width='298'; border='2';" style="position:fixed; top:210; left:30;"/>


strange :S

#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
And the category you get in is correct? You can propably verify this by checking the address bar.

#9
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Found what going wrong, and for sure is not you :) ... you helped me a lot and appreciate it. Webpage working fine only for first four goods of XML file, because theis ids are less than 4, so system can find the specific product. Rest of products are wrong, because are greater than 4. For example, toaster has id="5", so if i select toaster, in goods list page, good page takes parameters goodId = 5 and category = Electrical. Finally when trying to compile this information is out of range
<%groName = xpath.compile("/madShop/category[@id='"+catName+"']/goodsItem["+goodId+"]/*[1]");%>

So... now program has catName = Electrical and goodId=5, but each category has just 4 products.

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Could you copy paste the xml here?

#11
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
<category id="Grosery">

		<goodsItem id="0">

			<name>APPLES</name>

			<price>1.56</price>

			<image>apples.jpg</image>

			<provenance>South Africa</provenance>

		</goodsItem>

		<goodsItem id="1">

			<name>TOMATOES</name>

			<price>0.64</price>

			<image>tomatoes.jpg</image>

			<provenance>Holland</provenance>

		</goodsItem>

		<goodsItem id="2">

			<name>MANGOS</name>

			<price>0.50</price>

			<image>mangos.jpg</image>

			<provenance>Brazil</provenance>

		</goodsItem>

		<goodsItem id="3">

			<name>CUCAMBER</name>

			<price>1.21</price>

			<image>cucambers.jpg</image>

			<provenance>Spain</provenance>

		</goodsItem>

	</category>

	

	<category id="Electrical">

		<goodsItem id="4">

			<name>TOASTER</name>

			<price>30.00</price>

			<image>toaster.jpg</image>

			<provenance>Japan</provenance>

		</goodsItem>

		<goodsItem id="5">

			<name>COFFEE MACHINE</name>

			<price>87.99</price>

			<image>coffee_machine.jpg</image>

			<provenance>Italy</provenance>

		</goodsItem>

		<goodsItem id="6">

			<name>KETTLE</name>

			<price>15.99</price>

			<image>kettle.jpg</image>

			<provenance>China</provenance>

		</goodsItem>

		<goodsItem id="7">

			<name>TV</name>

			<price>499.00</price>

			<image>tv.jpg</image>

			<provenance>Germany</provenance>

		</goodsItem>

	</category>
Did't paste all of my categories due to space :P

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Try using:

<%groName = xpath.compile("/madShop/category[@id='"+catName+"']/goodsItem[[B][COLOR="#ff8c00"]@id='[/COLOR][/B]"+goodId+"[B][COLOR="#ff8c00"]'[/COLOR][/B]]/*[1]");%>






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users