• 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

How can I format the result of my search?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a search function that performs a query and returns the result in my jsp. Problem is that the results look like a bunch of text and is not easily readable. And when I try to put the results inside html and apply formatting, it only applies to the whole result but I cannot break it down to each iteration or field.
Here is my java code:



I have a search form in jsp that asks for the and passes it to my result.jsp where I:




using ResultSetFormatter I can get nice output look in console but it does not apply in jsp. I know that if I want formatting I have to write html/css code or apply a xslt stylesheet. But I can't do either because 1) I can't get the Length or Size of my result since it's string and 2) xslt is applied to xml file but my query results isn't written to a file.

I know I am wrong somewhere in my understanding or coding but I can't figure it out (and I'm a beginner too:( ). I really appreciate any piece of code or advice you might have.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1.Create the HTML you want.(this is the file which you need to create with the CSS styling you want)
2.Whereever you wanna display your iterated results/results keep some keywords in that HTML.
Ex:

3.In you Java class populate your results using StringBuffer/StringBuilder.
Ex :


4.Convert the buffer into String.
5.Read the HTML file using the FILE API
6.Replace all the KEYWORDS in HTML file using replace method with the new string of the sbuffer
7.Your HTML format is ready.
 
sarah motlagh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you es kranthi for answering. About the points:

1 and 2 - I think I have the html and the table code needed in my result.jsp (line15, 35). Unfortunately I'd missed the <html> part in my copying. sorry.

3- my results were in string format so I changed them to StringBuilder:



So I changed every instance of ret in my code from String to StringBuilder. However I don't know the number of times my query is iterated (in for loop) so for i<somevalue I had to put an arbitrary number.


4- But I just changed from String to StringBuilder (to use append). Isn't it what you meant by point 3 ?


So when I ran my WAR file, it gave error: Unable to compile class for JSP: An error occurred at line: 170 in the jsp file: /result.jsp Type mismatch: cannot convert from StringBuilder to String 167: try{ 168: ProjectMain pm=new ProjectMain(); 169: 170: result=pm.different(search); 171: } catch(Exception e){ 172: result="An Exception Caught!"; 173: }


Could you please tell me where I should have used String or StringBuilder/Buffer ?
 
es kranthi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This link might help to convert StringBuilder to String http://www.java2s.com/Book/Java/0060__Development/Convert_StringBuilder_to_String.htm

you can convert to String atlast i.e. after every value is appended to the Builder and return a String instead of StringBuilder in your method.

I see the problem is in Line 27.
So try appeding first in you loop and finally before returning rconvert to string and return.
 
sarah motlagh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok. So this time I'm using StringBuilder only inside loop and return type remains String. So:




Is it right now? I get error at line (20) and I don't know how to get the String i (charAt, get, getValue not working). Do I need to convert String to Array, get Array i, convert it to StringBuilder and then convert to String? Or there is another way to get value of String/Builder for row i?

 
es kranthi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give the stackTrace of the error?

I never used ResultSetFormatter anytime.

Here one possible solution can be try to print the Text Table that your ResultSetFormatter is returning in the form of String.

If it has any special characters or seperator String we can split the String convert that into an array and display the results.

If you dint get me just give me the stackTrace and the String whichi this line is returning

 
sarah motlagh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear es kranthi thanks for answering. But unfortunately I didn't understand. ResultSetFormatter returns the value of ResultSet in different formats. I've tried Text and XML so far but the output doesn't look nice. A sample output of the program (as Text run in console) looks like the image. The same program when run in jsp is unable to format new line and dashed line according to the lenght and number of returned result which makes it unreadable. The only indicator of a new line is ( | ) which also appears before every new column value too. So I'm not sure if using it to detect a new line is possible?



sample output from jsp (queries are different cause I can't run the program anymore but it looks sth like this):

-----------------------------------------------------------------------------------------------| title | subject | course ===============================================================================================| "alphabet" | " vowel, consonant" | "english" || "abc" | "alphabet" | "writing" |-----------------------------------------------------------------------------------------------


The error given is from line ( 20 ) :
HTTP Status 500 - javax.servlet.ServletException: java.lang.Error: Unresolved compilation problem: The method getValue(int) is undefined for the type String

type Exception report

message javax.servlet.ServletException: java.lang.Error: Unresolved compilation problem: The method getValue(int) is undefined for the type String

description The server encountered an internal error (javax.servlet.ServletException: java.lang.Error: Unresolved compilation problem: The method getValue(int) is undefined for the type String ) that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.Error: Unresolved compilation problem:
The method getValue(int) is undefined for the type String
 
es kranthi
Ranch Hand
Posts: 36
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that result can only fit into .txt file or meant for console not JSP page.
One thing is that you can use alternate API instead of ResultSetFormatter in this case.i.e. if you dont want to go more complex on this.

Else if this is the only way you want (ResultSetFormatter).i have a solution for you.
1. Getting rid of the error.
Here we will change the logic you used.

lets say contains the string you wanna display

String ret=

-----------------------------------------------------------------------------------------------| title | subject | course ===============================================================================================| "alphabet" | " vowel, consonant" | "english" || "abc" | "alphabet" | "writing" |-----------------------------------------------------------------------------------------------




now we split "ret" based upon the separated charaters i.e.

|

and

||




Here from the output we see that each new row is separated with "||" and each value in that row is seprated with "|"

First we will fetch the rows and from each row we will fetch the values like

now we have an array of rows.each row consisting of PIPE ~ "|" separated values.

Again to fetch individual values like

I hope now you got it
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is only prog. method.. so i have no idea.
 
sarah motlagh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

One thing is that you can use alternate API instead of ResultSetFormatter in this case.i.e. if you dont want to go more complex on this.



May I know what do you mean by alternate API? I can't seem to get ResultSetFormatter to work.
 
es kranthi
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sarah,
Sorry for late reply.I think we can convert the result to XML format using that method




Then we can use any parser to SAX/JAX/DOM anyone to parse the XML and obtain the results.


This is one of the alternate solutions but it takes a bit of your application-performance.

On the other hand,
Can you tell me the issue you are facing?
Also post the XML format you aer getting in your result .
 
reply
    Bookmark Topic Watch Topic
  • New Topic