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


3) define a 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:


    E.g. some like that:

or in html-tags:

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

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

<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)"/>
<p>Query: <input name="sSearch" value="" emptyok="false"/></p>

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


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

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

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


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)

# 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.

# web. URL for your root directory

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

# 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

# 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)

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

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

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

# font face. By default it is a current browser's font.

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

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

# how many pages are visible on the bottom. By default
# it is 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".

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

# 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!

# 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

# javascript settings for new window (in case of open=1). E.g.:

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

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

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

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

# template for counter

# template for founded elements

# template for pages string


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):


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.


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.:

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>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.


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: