Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Question: How do can I highlight search results in my JSP application?

JavaFAQ Home » JSP, Servlets Go to all tips in JSP, Servlets


Bookmark and Share

If you used to search with Google or reading modern forums, you can see that your search words are highlighted in the results page and even more, when you click a link in results page you see that words you are searching are highlighted directly in the body of html page.

Question: How do can I highlight search results in my JSP application?

Answer: If you used to search with Google or reading modern forums, you can see that your search words are highlighted in the results page and even more, when you click a link in results page you see that words you are searching are highlighted directly in the body of html page.

Looks like complex task to highlight search results in Java (no search and highlight Class in Java API!!!), but it is not. The knowledge is key! You can already assume that it must be done some kind of text processing Smile

Actually all you need to highlight search word(s) in text with JSP is:

1. Get a text which must be searched.
2. Get a search term(s) from user input: can be posted with POST or GET.
3. Locate searched word(s) in the text, highlight them.
4. If no word found, just return original text.

After 20 minutes on weekend evening I was ready with two files: one JSP and one Java class:

test.jsp

Code:

<%@ page contentType="text/html;charset=UTF-8" language="java"
     
%>
<html>
  <head><title>Simple jsp page</title></head>
  <body>



   <%
    Test test = new Test();
     String text = " What makes AJAX-based clients unique is that the client contains page-specific control logic embedded as JavaScript technology. In traditional server-clients architectures, the page interacts with the JavaScript technology based on events such as the document being loaded, a mouse click, focus changes, or even a timer. AJAX interactions allow for a clear separation of presentation logic from the data. An HTML page can pull in bite-size pieces of data as needed rather than reloading the whole page every time a change needs to be displayed. AJAX will require a different server-side architecture to support this interaction model. Traditionally, server-side web applications have focused on generating HTML documents for every client event resulting in a call to the server. The clients would then refresh and re-render the complete HTML page for each response. Rich web applications focus on a client fetching an HTML document that acts as a template or container into which to inject content, based on client events using XML data retrieved from a server-side component. ";
<br />
// this is text we will search through

    String term = "AJAX"; // this is searched word, which will be higkighted in the text, all occured cases.

 %>
    The text we search trough: <%=text %> <br />
    Here are highlighted searched word <br /> <h4>AJAX</h4> <br />by yellow background: <br /> <%=test.highlightTerm(text, term) %>





  </body>

</html>

test.java

Code:

public class Test {

    public String highlightTerm(String text, String term){
        String startTag = "<span style=\"background-color: #FFFF00\">";
        String endTag = "</span>";
        if (!term.equals("")){

            StringBuffer hResult = new StringBuffer (startTag);
            hResult.append(term);
            hResult.append(endTag);
            System.out.println("---->" + text + "  " + term);
            return text.replace(term, hResult.toString());
        } else {
            return text;
        }
    }
}

User interaction code I will not describe here. Instead I will illustrate my example wit some kind of simulation. You see two files below: one is JSP page and another is Test Class which contains just one method which do text highlighting.

I assume that we got text from a user and it is in "String text" variable in JSP page. Search term we also got and it is "AJAX".

So, just create an Test instance and call for the method, passing to it text and search term.

When you run example test.jsp you will get in your browser:

    What makes AJAX-based clients unique is that the client contains page-specific control logic embedded as JavaScript technology. In traditional server-clients architectures, the page interacts with the JavaScript technology based on events such as the document being loaded, a mouse click, focus changes, or even a timer. AJAX interactions allow for a clear separation of presentation logic from the data. An HTML page can pull in bite-size pieces of data as needed rather than reloading the whole page every time a change needs to be displayed. AJAX will require a different server-side architecture to support this interaction model. Traditionally, server-side web applications have focused on generating HTML documents for every client event resulting in a call to the server. The clients would then refresh and re-render the complete HTML page for each response. Rich web applications focus on a client fetching an HTML document that acts as a template or container into      which to inject content, based on client events using XML data retrieved from a server-side component.

All the found search terms, in our case it is "AJAX" were found in the text.

You could wonder why for such simple example I have two files? The question is simple: JSP is used mostly to create a layout - presentation so say and class is for the content of the page.

Having presentation and content in different places makes code maintenance much easier. It also decreases a risk for "spaghetti" (anti pattern many people used unfortunately) code creation - when all the program is one big file - a few thousands lines.


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy