Search Servlet ver. 2.4


    Servlet, allows you to search files on your web server.

How to use it:

1) download searchPackage.jar and save it in WEB-INF/lib

2) describe Search servlet in web.xml file
 


    <servlet>
     <servlet-name>Search</servlet-name>
     <servlet-class>com.cj.search.SearchServlet</servlet-class>
    </servlet>

3) define a mapping:
 


    <servlet-mapping>
     <servlet-name>Search</servlet-name>
     <url-pattern>/servlet/Search</url-pattern>
   </servlet-mapping>

Now you can set this servlet (in our example it is /servlet/Search) as an action parameter for your html form. Also you have to provide a configuration file (as an initial parameter or as a GET parameter). This file describes various environment settings for your search. You can pass parameter in query string or define it as an initial parameter for this servlet.

Direct setting of configuration file

The usage is:
 


    /servlet/Search?config_file

    E.g. some like that:
 


or in html-tags:
 


<html>
  <form action="http://your_host/servlet/Search?c:\files\my_config.txt" method="post">
  <input type="Text" name="search">    <input type="Submit" value="Search">
  </form>
</html>

Servlet supports WAP/WML, so you may use the same tool for your WAP sites:
 


<wml>
<card id="searchcard" title="Search">
<do type="accept" label="Search">
<go href="http://your_host/servlet/Search?your_config_file" method="post">
<postfield name="search" value="$(sSearch)"/>
</go>
</do>
<p>Query: <input name="sSearch" value="" emptyok="false"/></p>
</card>
</wml>

Configuration file as an initial parameter

You may set configuration file as a parameter for this servlet. Parameter name is conf. E.g. you may have some like this in your web.xml file
 


<servlet>
    <servlet-name>Search</servlet-name>
    <servlet-class>com.cj.search.SearchServlet</servlet-class>
    <init-param>
     <param-name>conf</param-name>
     <param-value>path_to_your_configuration_file</param-value>
    </init-param>
</servlet>

and now you do not need to pass config file in your query:
 


<html>
  <form action="http://your_host/servlet/Search" method="post">
  <input type="Text" name="search">    <input type="Submit" value="Search">
  </form>
</html>

So servlet will obtain a query string from HTML (WML) form and proceed a search. Files for search will be described in the configuration file. Output could be generated through this servlet itself (you will be able to configure look and feel) or through your own JSP page.

Configuration file

Configuration file is a text file describes servlet's settings. You can save this file anywhere on your server. This is a text file, each line describes one parameter in the form of

    parameter=value

Empty lines and any line starts with # or // are ignored.

Current version supports the following list of parameters:

# root directory for the search. By default it is a . (root directory of your servletrunner)
root=your_directory

# excluded directories. You may exclude some subdirectories from search
# Here you may provide a commas separated list of excluded directories
# By default this list is empty.
exclude=your_list_of_excluded_subdirectories

# web. URL for your root directory
web=your_url

# forward. Optional parameter. Describes your own JSP page for reports
forward=your_url

# ext. Comma separated list of file extensions for files being searched. By default it is
# htm,html,jsp,shtml,HTM,HTML,JSP,SHTML
# you may use . for files without extensions
ext=java,txt

# HTML/XML tags policy. You may skip tags during your search
# or include the whole file into your search (default behavior).
# Possible values are 1 (skip tags) or 0 (do not skip)
skiptags=0

# Field name for the search query. By default it is search
field=search

# Background color for the result page. By default it is #FFFFFF (white).
bgcolor=your_color

# Foreground color for the result page. By default it is #000000 (black).
fgcolor=your_color

# font face. By default it is a current browser's font.
face=Arial,Verdana

# font size. By default it is a current browser's font.
size=3

# page size (documents per page). By default it is 30
page=25

# how many pages are visible on the bottom. By default
# it is 10
pages=10

# title. Default value is Coldbeans search
title=Acme search

# header. You can set any HTML code for output on the top for each page.
# You can describe some banners for example. Content of this file will
# be outputted "as is".
header=path_to_your_header_file

# footer. You can set any HTML code for output on the bottom for each page.
footer=path_to_your_footer_file

# temp directory. This directory will be used for
# temp files only. By default it will be a temp directory
# provided by your application server.
# Note: if you set your own directory here than all files from this directory will be deleted!
tmp=your_tmp_directory

# open. How to open founded documents.
# Documents may be opened in the same window (open=0)
# or in the separate window (open=1)
# Default value is 0
open=1

# javascript settings for new window (in case of open=1). E.g.:
window=width=400,height=220,location=no,toolbar=no,menubar=no,scrollbars=yes,resize=yes

# You can set also your own page for "found no documents" output.
notfound=your_url

# localization support. You can set character encoding for input parameters
# here. Default value is ISO-8859-1
encoding=cp1251

# charset for output. Default value is ISO-8859-1
charset=your_charset

# customization. You may define here your own templates
# for elements in output. See below about templates

# template for counter
template1=your_file

# template for founded elements
template2=your_file

# template for pages string
template3=your_file
 

Examples

In general you have to provide a directory for searched files and a URI, describes a web access to this directory. For example (it is for Orion server in the default configuration):

root=c:\orion\default-web-app
web=http://your_host:8080

here servlet will search files in c:\orion\default-web-app and discovered items will be visible as http://your_host:8080/file_name. In other words parameter root describes a real location for searched files and parameter web describes an URI for this directory.

As soon as you set a parameter forward the output will passed to your own JSP page. You can use Search taglib for creating your own reports. Otherwise the same servlet will create a report with your results. And that report could be configured through templates.

Search tips:

Search servlet defines a word as any combination of letters and  numbers that is separated by any of the following:
   - white space, such as spaces, tabs, line ends, or the start or end  of a document.
   - special characters and punctuation, such as %, $, /, #, and _.

A search  phrase is a group of words or numbers linked together.

Use lowercase text in your searches. When you use lowercase  text, the search service finds both uppercase and lowercase results. When you use uppercase text, the search service only finds uppercase results.

To make sure that a word is always included in your search, place a plus sign (+) immediately before the keyword (no spaces) in the search box. To make sure that a word is always excluded from your search, place a minus sign (-) immediately before the keyword (no spaces) in the search box.

Templates

You may describe your patterns for elements in output. There are 3 templates. First one describes what will be outputted as a common search result. By default servlet will print some like this 10 document found. You may set in config your own content for this output. E.g.:
 


template1=c:\search\myheader.txt
where myheader.txt contains some like this for example:
 

Found $N documents

Meta-symbol $N will be replaced with the actual number before output

Next template defines pattern for founded elements. You may use meta-symbols:

$N - sequential number
$U - URL
$T - title for document
$D - last modified date
$S - size

For example, default template could be described so:
 


<br>$N. <a href="$U">$T</a>
<br>URL:$U
<br>Last modified on: $T Size: $S

And the latest template (template3) defines pattern for list of pages (by default servlet prints Pages:).

So templates let you to completely redesign output or localize it for example.

Notes:

1. Configuration file can be saved anywhere on your server. E.g. if you are using /servlet/Search?config call we assume this file is saved under the root directory of servletrunner. But you can of course always use the full name for setting config file location: /servlet/Search?/home/users/my_file (or /servlet/Search?c:\users\my_file) or a full path relatively to the web root: /servlet/Search?/data/conf.txt

2. Evaluation version displays the first 3 pages only.
 

for downloading:

Search package: searchPackage.jar

sample of configuration file:  findconf

 © Coldbeans     Comments?

See also JSOS - the largest collection of servlets and filters.

Also in JSOS: