dcsimg
Difficulty in reading text file in JSP
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Satish_Chandra
Posted On:   Monday, April 4, 2005 06:29 AM

Hi, I am unable to read the contents of the text file, whose name I retrieve from the database using the below code, as I get the exception stating that the system cannot locate the file specified. Can you please help me out? <% //File to retreieve and display contents of text files associated with one person %> <%@ page import="java.io.*, java.sql.*" %> <%response.addHeader("Cache-Control","no-cache");%> <%response.addHeader("Pragma","no-cache");%> <%response.addHeader("Expires","-1");%> <% String record = null; String webid = null; int recCou   More>>


Hi,

I am unable to read the contents of the text file, whose name I retrieve from the database using the below code, as I get the exception stating that the system cannot locate the file specified. Can you please help me out?



			
<% //File to retreieve and display contents of text files associated with one person %>

<%@ page import="java.io.*, java.sql.*" %>
<%response.addHeader("Cache-Control","no-cache");%>
<%response.addHeader("Pragma","no-cache");%>
<%response.addHeader("Expires","-1");%>

<%
String record = null;
String webid = null;

int recCount = 0;
int count;
count=0;
int i = 0;

Connection CN = null;
Statement ST = null;
ResultSet RS = null;
ResultSet RS2 = null;

String files[] = new String[5];
for(i=0;i <5;i++) files[i] = null;

String id = request.getParameter("id").toString().trim();

if((id.length() > 0) && (id!=null))
{
if(session.getAttribute("welcome")!=null)
{
if(session.getAttribute("welcome").toString().trim().length() > 0)
{
if (!(id.equals(session.getAttribute("welcome").toString().trim()))) response.sendRedirect("redirPage.jsp");

/*out.print("id="+id+" ");
out.print("session="+session.getAttribute("welcome")+" ");
out.print(id.equals(session.getAttribute("welcome").toString().trim())+" ");
out.print(session.getAttribute("welcome")==null);*/

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //instantiate JDBC ODBC Driver

CN=DriverManager.getConnection("jdbc:odbc:satishDSN","","");
ST=CN.createStatement();
RS=ST.executeQuery("Select Website_Id from satish.fac_has where Fac_Id='"+id+"'");
i = 0;
//out.println("Select Website_Id from satish.fac_has where Fac_Id='"+id+"'");


while(RS.next())
{
//out.print(RS.getString(1).toString().trim());
webid = RS.getString(1).toString().trim();
//out.println("webid = "+webid);
}


}

catch(SQLException e){
out.println("SQL Exception (I) caught " + e.getMessage());
}

//out.println("Select Web_File_Name from satish.web_file where Website_Id ='"+webid+"'");
RS.close();

try{
RS2 = ST.executeQuery("Select Web_File_Name from satish.web_file where Website_Id ='"+webid+"'");
while(RS2.next())
{
//out.println(RS2.getString(1).toString().trim());
files[i++] = RS2.getString(1).toString().trim();
count++;
}
}

catch(SQLException e){
out.println("SQL Exception (II) caught " + e.getMessage());
}

RS2.close();
CN.close();
i = 0;
//for(i=0;files[i]!=null;i++) out.println("files["+i+"]: "+files[i]);

%>
<%

try {

for(i=0;files[i]!=null;i++)
{
FileReader fr = new FileReader(files[i].toString().trim());
BufferedReader br = new BufferedReader(fr);

record = new String();
while((record = br.readLine()) != null) {
recCount++;
out.println(record+"
");
}
br.close();
fr.close();
}
}

catch(IOException e){
// catch possible io errors from readLine()
out.println("Exception during file read: "+files[i]+" "+e.getMessage());
}
%>
<%
out.println("Matches found for the string "");
out.print( "" are : " );
%>
<%
out.print(count);
%>
<%
}
else response.sendRedirect("redirPage.jsp");
}
else response.sendRedirect("redirPage.jsp");
}
else response.sendRedirect("redirPage.jsp");
%>


Satish    <<Less
About | Sitemap | Contact