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
<head> tags and multiple <body> 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/Note.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("<html>");
>             out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
>     out.println("<BODY>");
>     out.println("<BIG>Hello World</BIG>");
>             out.println("<HEAD>");
>    out.println(path);
>               out.println("<title>the title of my document</title>");
>                             out.println("</head>");
>              out.println("HelloYou, ");
>               out.println("<body>" + sbuf + "</body>");
>                 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

Reply via email to