The below is a servlet I downloaded from
http://www.servletsource.com/code/ReadFile.html.
The servlet that reads a text file (from directory
/temp) and send the content to a client.
My question is where the directory "/temp"
is?
Thanks.
Daniel
************* ReadFileServlet.java ***************
/*
* A simple servlet that reads a text file then sends the content
* to a client/browser. The possibilities are limitless!!!
*@date 6/2/98
*@author Jon M Strande - [EMAIL PROTECTED]
*/
//lets import the classes that we need.
import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;
public class ReadFileServlet extends HttpServlet
{
//this is the directory where the file resides
private String _baseDir;
//I am just going to store the lines of the file in a vector
//maybe not the best way to do this, but it works easily.
private Vector v = new Vector();
public void init (ServletConfig config) throws ServletException
{
super.init(config);
_baseDir = "/temp";
}
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
//getOutputStream is in the Interface
javax.servlet.Servlet.ServletResponse
//It provides an output stream for returning data to the user
ServletOutputStream out = res.getOutputStream();
// set content type and other response header fields first
//this is also in the interface javax.servlet.Servlet.ServletResponse
res.setContentType("text/html");
//set the title stuff of the page
//println is a Method in the Class
javax.servlet.Servlet.ServletOutputStream
//it is an abstract class
//println - Prints the string provided, followed by a CRLF.
out.println("<HTML><HEAD><TITLE> SimpleServlet Output
</TITLE></HEAD><BODY><FONT FACE=\"Verdana, Arial, Helvetica\">");
out.println("<h1> ReadFileServlet Output </h1>");
out.println("<br>Output:");
try
{
String fileLine;
//you could pass the filename to the servlet as a parameter.
String InputFile = "/temp/aaa.txt";
// Get the file specified by InputFile
BufferedReader br = new BufferedReader(new InputStreamReader(new
FileInputStream(InputFile)));
//while there are still lines in the file, get-em.
while((fileLine = br.readLine())!= null)
{
//add each line to the vector, each line will have a CRLF
v.addElement(fileLine);
}
//IMPORTANT!!!! - CLOSE THE STREAM!!!!!
br.close();
}
catch(IOException e)
{
out.println("An error occurred reading the file" + e);
}
//Here we will just loop through the vector and print the
//lines of the file out to the client.
int num = v.size();
for(int times = 0; times < num; times++)
{
//maybe you want to check the value or do some
//processing to the text before sending it out.
//if so, you could say;
//String s;
//s = v.elementAt(times).toString());
//s = doSomething(s);
//out.println(s);
out.println(v.elementAt(times).toString());
out.println("<BR>");
}
out.println("<BR><P>This is output from
<b>ReadFileServlet</b>.</font>");
out.println("</BODY></html>");
//close the Servlet outputstream
out.close();
}
public String getServletInfo()
{
return "A Servlet that prints the contents of a file to the user.";
}
}
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html