<html>
	<head>
		<title>Address Book JSP Application</title>
	</head>
	<body bgcolor="black">
    	<h3 style='color:white'>Address Book JSP Application</h3>

		<%@ page import="java.io.*" %>					<!-- JSP import tag -->          
		
        <%!												//JSP field/method declaration tag
            static String method;
            static String lastname = "";
            static String result   = "";
        	static String message  = "";
		%>
		<%												//JSP java code tag 
			method   = request.getMethod();
            lastname = request.getParameter("lname");
            if (lastname == null) lastname = "";
														
        	if (method.equals("POST"))					//if POST --> client submitted form, validate
			{														
                message = validate();					//validate 												//JSP java code tag 
                if (message.equals(""))                 //if no errors
                    result = getData(lastname);         //retrieve data from DB
			}
         %>

		<fieldset style='width:450px; height:300px; border-color:gold'>
		<legend style='color:gold'>Address Book</legend>
        <form name='form' method='POST' action='example5.jsp'>
        <table style='background-color:lightyellow'>
        <tr><td><b>Enter (partial) last name:
        <td><input type='text' name='lname' value='<%=lastname%>'>
        <td><input type='submit' value='  Search  '>
        <tr><td colspan=3><textarea cols=60 rows=15> <%=result%> </textarea> 
        </table>
        </form>
        </fieldset>
        <p style='color:red'> <%=message%> </p>
        </body>
        </html>
<%!												  //JSP field/method declaration tag
//-----------------------------------------------------------------------------------
// Validate data entered
//-----------------------------------------------------------------------------------
    static String validate()
    {
        String error = "";

        if (lastname.equals(""))
            error = "Please enter last name";

        return(error);
    }
//-----------------------------------------------------------------------------------
// getdata from file
//-----------------------------------------------------------------------------------
    static String getData(String searchName)
    {        
        String found = "";
        
        try 
        {
            FileReader     addrbook = new FileReader("/home/sultans/web/java/demo/8inpout/zAddrBook.txt");  
            BufferedReader fileBuf  = new BufferedReader(addrbook);     //buffer the input

            int i = 0;        

            while(true)                                                 //keeing looping
            {
                String rec = fileBuf.readLine();                        //read from the file

                if (rec == null)                                        //if end-of-file
                    break;                                              //exit loop

                String[] tokens = rec.split(",");                       //split on ,

                String fileLastname = tokens[0];                        //get first token

                fileLastname   = fileLastname.toLowerCase();            //convert to lowercase
                searchName     = searchName.toLowerCase();
                        
                if (fileLastname.indexOf(searchName) == 0)              //if match
                {
                    String lname  = tokens[0];                          //obtain the data elements
                    String fname  = tokens[1];
                    String street = tokens[2];
                    String city   = tokens[3];
                    String state  = tokens[4];
                    String zip    = tokens[5];
                    String phone  = tokens[6];

                    found += fname+" "+lname+"\n"+street+"\n"+city+", "+state+"  "+zip+"\n"+phone+"\n"
                          +  "--------------------------  \n";
                }
            }
            addrbook.close();                               //close the file
        }       
        catch (IOException e)
        {
            return(e.toString());
        }       

        return(found);
    }
//-----------------------------------------------------------------------------------
%>