Hi Ken,

    Your original servlet class must be residing in
webapps/root/web-inf/classes directory. Right? And you have put Note.txt
in webapps/examples directory. Then what exactly are you asking your
servlet to do? You are asking the servlet that is in the ServletContext
"root" to read a file from a directory called"/webapps/examples"
*starting from the current ServletContext ie. "root"*. So for your
original code you would get real path as
".../webapps/root/webapps/examples/Note.txt".

    That is why Richard told you to change the getRealPath("xxxx ") to
"/Note.txt". Then the servlet would look in webapps/root directory. Or
if you put your servlet in webapps/examples/web-inf/classes & Note.txt
in webapps/examples it would work the same way. I tried your original
code with the modification by Richard and it worked perfectly fine.

    MyServlet.class was put in webapps/examples/web-inf/classes and
Note.txt in webapps/examples. You try it that way and also read more
about ServletContext and webapps. Just do a search in the archives of
this list & you are sure to get more info than you bargained for. The
Servlet Specification also is a must-be-read.

    Oh, and my servlet URL was
http://localhost:8080/examples/servlet/MyServlet

Sebu

ken dias 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 =
> >>getServletConf
> g().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("");
> >> 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

Reply via email to