Design a user interface, for example, based on the B/S structure:
Put Lucene and so onProgramPackage and developed search programs (classes) are imported to the search page, and the results page is compiled.CodeFor example, searchresult. jsp:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb2312" %> <% @ page Import = "Java. Io. ioexception, java. Io. stringreader, java. util. arraylist, Org. apache. lucene. analysis. analyzer, org. apache. lucene. analysis. token, org. apache. lucene. analysis. tokenstream, org. apache. lucene. search. hits, Edu. guet. shilong. searcher. *, Javaserhighlight. webjavasehighlighter, org. Apache. Lucene. analysis. Standard. standardanalyzer "%> <% String path =Request. getcontextpath (); string basepath = Request. getscheme () + ": //" + request. getservername () + ":" + request. getserverport () + path + "/" ; %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"> <HTML> U {Font: ""; Color: # ff0066; font-weight: 100;} Em {color: #66 ffcc} </Style> // Receives the URL-based value and processes the parameter --------------- String key = request. getparameter ("key "); // Querystring received String keyval = New String (key. getbytes ("ISO8859-1"), "gb2312" ); Searcher SC = New Searcher (keyval ); // Build search instances Hits hits = SC. gethits (); // Retrieve hit records // Keyword Word Segmentation Analyzer analyzer = New Standardanalyzer (); stringreader in = New Stringreader (keyval); tokenstream = Analyzer. tokenstream ("" , In); arraylist al = New Arraylist (); For (Token = tokenstream. Next (); Token! =Null ; Token = Tokenstream. Next () {Al. Add (token. termtext ());} // End Webjavasehighlighter HL = New Webjavasehighlighter (Al ); // Build highlight instances // End Processing // ----------------------------------------- // Paging processing started ----------------- Int Intpagesize = 0; // Number of records displayed on one page Int Introwcount = 0; // Total number of records Int Intpagecount = 0; // Total page count Int Intpage = 0; // Page with display String strpage; // Parameter receiving variable in URL // Start setting Intpagesize = 10; // 10 records per page rated Strpage = request. getparameter ("page" ); If (Strpage = Null ){ // Indicates that the querystring in the URL does not have the page parameter. The first page is displayed. Intpage = 1 ;} Else { // Convert the string value in the URL to an integer Intpage =Integer. parseint (strpage );} If (Intpage <1 ) {Intpage = 1 ;} // Start computing records Introwcount = hits. Length (); // Retrieve hit records and set the total number of records // Total number of pages Intpagecount = (introwcount + intPageSize-1 )/ Intpagesize; // Adjust the page number with display If (Intpage> Intpagecount) {intpage = Intpagecount ;} // Data Pointer displayed Int Currentpoint = (intPage-1) * intpagesize; // Current pointer Int Roopcount; // Number of records displayed If (Introwcount-currentpoint> Intpagesize) {roopcount = Intpagesize ;} Else {Roopcount = Introwcount- Currentpoint ;} // Out. Print ("cycles:" + roopcount ); // Record Data Display processing started ------------ Out. println ( "<Table width = \" 800px \ ">" ); For ( Int I = currentpoint; I <currentpoint + roopcount; I ++) // Show all {Out. println ( "<Tr>" ); String title Export hl.highlight(hits.doc (I). Get ("title" ); String URL Using hits.doc (I). Get ("url" ); Out. println ( "<TD> <a href = '" + URL + "'>" + title + "</a> </TD>" ); Out. println ( "</Tr>" ); Out. println ( "<Tr>" ); String content Export hl.highlight(hits.doc (I). Get ("content"), 100 ); Out. println ( "<TD>" + content + "</TD>"); Out. println ( "</Tr>" ); Out. println ( "<Tr>" ); Out. println ( "<TD> <font-size: 9px> <a href = "+ URL +"> <em> "+ URL +" </em> </a> </font> </TD>" ); Out. println ( "</Tr>" );} Out. println ( "</Table>" ); // Display ended ----------- %> </Div> <form action = "show. jsp"> The <% = Intpage %> total pages <% = intpagecount %>Page <% If (Intpage> 1 ){ // If the current page is smaller than the total number of pages %> <A href = "show. jsp? Key = <% = keyval %> & page = <% = intPage-1 %> "> previous page </a> <% } If (Intpage <intpagecount ){ // If the displayed page is greater than 1%> <A href = "show. jsp? Key = <% = keyval %> & page = <% = intpage + 1%> "> next page </a> Go to section: <Input type = "text" name = "page" size = "4"> Page <Input type = "Submit" value = "go" name = "cndok"> <% }%> </form> <JSP: Include page = "Common. JSP "> </jsp: Include> </body>
Test results: