Ken,
As I said in the last email, you don't need './webapps/example' in your
parameter to getRealPath,
theTextArea and theReadButton are the variables that 1) are dangerous
because they have class scope, and 2) are useless in a servlet b/c they are
part of the awt..
-Regards
Richard
import java.io.*;
import java.util.Enumeration;
import java.awt.*;
import javax.servlet.*;
import javax.servlet.http.*;
// import java.awt.event.*;
// import javax.swing.*;
/**
* This is a simple example of an HTTP Servlet that
* uses the HttpSession class
*
* Note that in order to guarantee that session
* response headers are set correctly, the session must be retrieved before
* any output is sent to the client.
*/
public class MyServlet extends HttpServlet {
JTextArea theTextArea = new JTextArea(20, 64);
JButton theReadButton = new JButton("Read");
String filename;
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
String path =
getServletConfig().getServletContext().getRealPath("/Note.txt");
System.out.println("Path = " + path);
StringBuffer sbuf = new StringBuffer();
try
{
BufferedReader br = new BufferedReader (new FileReader(path));
while(true)
{
String s = br.readLine();
if (s == null)
{
break;
}
sbuf.append(s);
sbuf.append("\n");
}
}
catch (Exception e)
{
}
res.setContentType("text/html");
// then write the data of the response
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head>");
out.println(path);
out.println("<title>the title of my document</title>");
out.println("</head>");
out.println("<body>");
out.println("HelloYou, ");
out.println(sbuf);
out.println("</body>");
out.println("</html>");
out.close();
}
}
At 06:07 PM 10/8/01 -0400, you wrote:
>Hi Richard,
>
>
>
>Thanks for your response.
>
>I tried your code exactly, but it still does not read the file Note.txt.
>It just ignores it as before. I get the response "Hello World null Hello
>You,". I then deleted the dot before webapps/examples/Note.txt, so I had
>---.getRealPath("/webapps/examples/Note.txt. In this case I got "Hello
>Worldc:/tomcat/jakarta-tomcat-3.2.3/webapps/examples/Note.txt Hello You,".
>Again. it ignored the statement out.print.ln(----+ sbuf + ------);
>
>Which are the variables in the swing class you refer to?
>
>Regards
>
>
>
>Ken
>
>
>
>
> >From: Richard Yee
> >Reply-To: "A mailing list for discussion about Sun Microsystem's Java
> Servlet API Technology."
> >To: [EMAIL PROTECTED]
> >Subject: Re: servlet to read a file
> >Date: Sun, 7 Oct 2001 14:56:55 -0700
> >
> >Ken,
> >I was able to get the servlet code you sent to read a file. I had
> >to make
> >a few changes to your code b/c the parameter to getRealPath is
> >wrong. The
> >path should be specified relative to the root directory of the web
> >app that
> >your servlet is running in. In your case, it seems that you are
> >running it
> >in the 'examples' web app'. Therefore, the path is "/Note.txt".
> >
> >There are a few other things I noticed.
> >1) You declare some variables from the Swing classes in your
> >servlet. You
> >can't use these in a servlet. You could use them in an applet that
> >is
> >displayed from a servlet, but then the Swing elements would be part
> >of the
> >applet code and not the servlet code.
> >2) Some of your variables are class variables and not method
> >variables. You need to be very careful about using class variables
> >in a
> >servlet. Most of the time you should use variables that are local
> >to your
> >methods. You are running the risk of running into thread safety
> >problems
> >when you use class variables. Basically, you might have multiple
> >requests
> >all accessing the same variables which can give you unexpected
> >results.
> >3) The html that your servlet outputs is incorrect. There are
> >multiple
> > tags and multiple tags.
> >
> >At 11:59 AM 10/7/01 -0400, you wrote:
> >>import java.io.*;
> >>import java.util.Enumeration;
> >>import java.awt.*;
> >>import javax.servlet.*;
> >>import javax.servlet.http.*;
> >>import java.awt.event.*;
> >>import javax.swing.*;
> >>/**
> >>* This is a simple example of an HTTP Servlet that
> >>* uses the HttpSession
> >>* class
> >>*
> >>* Note that in order to gaurentee that session
> >> * response headers are
> >>* set correctly, the session must be retrieved before
> >> * any output is
> >>* sent to the client.
> >>*/
> >>public class MyServlet extends HttpServlet {
> >>
> >> JTextArea theTextArea = new JTextArea(20, 64);
> >> JButton theReadButton = new JButton("Read");
> >> String filename;
> >>
> >> public void doGet (HttpServletRequest req,
> >> HttpServletResponse res)
> >> throws ServletException, IOException
> >> {
> >> String path =
> >>getServletConfig().getServletContext().getRealPath("./webapps/examples/N
> ote.txt");
> >>
> >> System.out.println(path);
> >> StringBuffer sbuf = new StringBuffer();
> >> try {
> >> BufferedReader br = new BufferedReader (new FileReader(path));
> >> while(true)
> >> {
> >> String s = br.readLine();
> >> if (s == null)
> >> {
> >> break;
> >> }
> >> sbuf.append(s);
> >> sbuf.append("\n");
> >> }
> >> }
> >> catch (Exception e) {
> >> }
> >> res.setContentType("text/html");
> >> // then write the data of the response
> >> PrintWriter out = res.getWriter();
> >> out.println("");
> >> out.println("");
> >> out.println("");
> >> out.println("Hello World");
> >> out.println("");
> >> out.println(path);
> >> out.println("");
> >> out.println("");
> >> out.println("HelloYou, ");
> >> out.println("" + sbuf + "");
> >> out.close();
> >>
> >> }
> >>}
> >
> >___________________________________________________________________________
> >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
> >
>
>
>----------
>Get your FREE download of MSN Explorer at http://explorer.msn.com
>___________________________________________________________________________
>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
___________________________________________________________________________
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