Introduction:
This application searches for the File name in a given Directory.
The File name is the Input .The user has to type in the file name or any parameter in the text box that appears on the HTML page .This HTML page calls on a servlet named as searchservlet which in turn basing on the input parameter displays the relevant file names along with the absolute file paths, record number in the directory. It also provides links which on clicking open up the respective files.
Components of this application:
a) This application consists of a HTML page named “search.html” which asks for the user’s input of a file name as an input parameter .If nothing is entered then a prompt message is displayed showing the message “Enter the file name to be searched”on pressing the “Find” button .The search is done based on any alphabet that is entered in the textbox.
b) A java file named as “ OnlyExt.java “ which actually implements the FileNameFilter interface .This is used to restrict the visibility of the file names returned by list() to files with names that end in the file extension specified when the object is constructed.
c) A java file named as “ searchservlet.java” . This servlet is called by the HTML page when the “Find” button is presses after some input in the textbox .This servlet in turn displays all the search details along with the relevant file names, their absolute paths, respective record number and provides a link which takes to the respective file.
Note: The class files after compilation of both the OnlyExt.java and searchservlet.java must be kept at the same place(i.e. in the same directory).
Code for search.html:
<html>
<head>
<title> Search Page</title>
<script language=”javascript“>
<!– This function does the validation so that the textbox is not blank– >
function fnSearch()
{
if(document.frm1.txtSearch.value.length==0)
{
alert(“Enter the file name to be searched”)
document.frm1.txtSearch.focus()
}
else
{
document.frm1.submit();
}
}
</script>
<body>
<center>
<form name=”frm1″ method=”POST” action=”/Zafir/searchservlet“>
<table>
<tr>
<td>Enter the file to be searched</td>
<td><input type=textbox name=”txtSearch” size=”25″ value=”"></td>
</tr>
</table>
<input type=button value=”Find” onClick=”fnSearch()”>
</form>
</center>
</body>
</html>
Code for OnlyExt.java :
import java.io.*;
public class OnlyExt implements FilenameFilter{
String ext;
public OnlyExt(String ext){
this.ext=”.”+ext;
}
public boolean accept(File dir,String name){
return name.endsWith(ext);
}
}
class DirListOnly{
public static void main(String args[]){
String dirname=”/java”;
File f1=new File(dirname);
FilenameFilter only=new OnlyExt(“html”);
String s[]=f1.list(only);
for(int i=0;i<s.length;i++){
System.out.println(s[i]);
}
}
}
Code for searchservlet.java :
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class searchservlet extends GenericServlet {
public void service(ServletRequest req,ServletResponse res) throws ServletException,IOException{
res.setContentType(“text/html”);
PrintWriter pw=res.getWriter();
int iRowCount=0;
String strSearchString=”";
//This stores the input parameter from the textbox into a string strSearchString
strSearchString=(String)req.getParameter(”txtSearch“);
String UpperStrSearchString = strSearchString.toUpperCase();
pw.println(“You searched for : “+strSearchString + “<br>”);
if (strSearchString.length()!=0)
{
//The directory name is stored in a string.
String dirname=”/share1/Comprehensive/html”;
File f1=new File(dirname);
//This lists all the Files and stores them in a string array.
String s1[]=f1.list();
pw.write(“Total Number Of Records Searched ” + s1.length);
for(int i = 0;i < s1.length;i++)
{
int k=0;
String result1=”";
String UpperRecord =s1[i].toUpperCase();
//This converts all the File names into UpperCase to avoid the case sensitiveness.
k=UpperRecord.indexOf(UpperStrSearchString);
if(k!=-1){
pw.println(“<br><h3>File Found!!<br>” + UpperRecord+ “</h3>”);
pw.println(“Record Length ” + UpperRecord.length());
pw.println(“<br>”);
pw.println(“Absolute Path ” + f1.getAbsolutePath());
pw.println(“<br>”);
pw.println(“<a href=\”" + f1.getAbsolutePath() + “/” + UpperRecord + “\”>Click here to view the file</a>”);
pw.println(“<br>”);
if(s1[i].length()!=0)
{
pw.println(“<br>”);
pw.write(” Found Record Number= ” + (i+1));
}
else
{
pw.println(“<br>”);
pw.println(“sorry”);
}
}
}
}
pw.close();
}
}
Tags: java servlets, searching files, To search for Files in a given Directory and to display the results using Java Servlets