Jump to content

Search function in jsp

- - - - -

  • Please log in to reply
5 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello, i have to build a search fuction in my website. In main page(JSP) i have a text field and a submit button. When button clicked, user should transfer to goods list (JSP) where all my goods retrieved from store(XML). I have stuck at the point where only goods with name similar to the keyword should be displyed. (ex. If user insert "to" in main page, goods list page should present "Tomatoes" and "Toaster" or if insert "apples" only "Apples")

Any help???
Thank you

#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
Dunno what you use to parse your XML, but it should have some kind of Xpath option. If it can't handle xpath, take something else.
I made this xml:

<food>

	<item>

		<name>tomato</name>

		<color>red</color>

	</item>

	<item>

		<name>banana</name>

		<color>yellow</color>

	</item>

	<item>

		<name>toast</name>

		<color>yellow/brown</color>

	</item>

</food>

To search for 'to', all I need to do is use this xpath expression, and it returns the items
/food/item[contains(name,'to')]
To get only the names:
/food/item/name[contains(.,'to')]


#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello, yes im using XPath to compile the XML, i didn't really get the meaning of "name" before 'to' and fullstop?

#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
Well plainly

/food/item

Would result in a list of all 'item' nodes in 'food' right.
The brackets [ ] Can actually be read as a WHERE condition.
So
/food/item[contains(name,'to')]
Would be: Select all 'item' from 'food' where the 'name' element of 'item' contains 'to'.

/food/item/name[contains(.,'to')]
This only selects the names WHERE . contains 'to'.
In xpath a dot/point/'.' means current element. So because I select the name, i don't need to go deeper and check a child or attribute, but the name itself --> '.'


Quote

Yes im using XPath to compile my xml, im working on two jsp files now, the main page and the list of goods. Main page has 4 images and each one goes to list of goods page but print out different goods (Grosery, bakery...). This working fine. Now i have to create search engine. My text field and go button are on main page, so when i click on go button web site should go to goods list page and print out goods who contain letters from keyword. Until now when im clicking the button text inside the text field doesn't transfered to the other page, print out null always
Any idea?
So you mean that request.getParameter('textFieldId') returns null?
Please make sure the textField is actually inside <form> </form> tags, together with the button.

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Thank you, web site working fine, without exceptions. The problem now is the cointained keyword would vary. If i put ....name[contains(./'to')] print me out just tomatoes name and that's right...but what happens if i put a variable there? Prints me out all products whatever keyword be. Maybe because it wants the single quotes?

Thank you

#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

String xpath = "/food/item/name[contains(.,'" + variable + "')]";






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users