Hirdesh,

 

I tried your code. It works fine except it does not read the file Note.txt - just ignores this file. Also the path returned is c:\tomcat\jakarta-tomcat-3.2.3\webapps\Root\Note.txt  (Root instead of examples).

I am just stuck now.

Thanks

Ken

>From: Hirdesh Mishra <[EMAIL PROTECTED]>
>Reply-To: "A mailing list for discussion about Sun Microsystem's Java Servlet API Technology." <[EMAIL PROTECTED]>
>To: [EMAIL PROTECTED]
>Subject: Re: servlet to read a file
>Date: Mon, 8 Oct 2001 10:04:11 +0530
>
>I am a greenhorn and so i tried my hands on this example. With your tips I
>was a ble to run it.
>I made a one change in code. And a few in HTML. I placed my Note.txt in
>examples folder. The code is :
>------------------------------------------------------------------
>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(System.getProperty("file.
>separator")+"Note.txt");
> System.out.println("path is ..... " + 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("path is ------> "+ path);
> out.println("
Hello You, ...... ");
> out.println( sbuf +"
" );
> out.println("");
> out.println("");
> out.close();
>
> }
>}
>
>
>
>
>------------------------------------------------------------------
>I got this output:
>
>Hello World
>path is ------> D:\Tomcat\jakarta-tomcat-3.2.3\webapps\examples\Note.txt
>HelloYou, ...... ---------------------- Hi This is Hirdesh Mishra
>Thanks ----------------------
>
>
>However my file contents were :
>
>----------------------
>Hi
>This is Hirdesh Mishra
>Thanks
>----------------------
>
>MY QUESTION IS : why did my file appeared as one line. Where did newline
>characters present in my NOte.txt file went?
>
>
>
>
>
>Thanks,
>Hirdesh Mishra
>
>
>
>-----Original Message-----
>From: A mailing list for discussion about Sun Microsystem's Java Servlet
>API Technology. [mailto:[EMAIL PROTECTED]]On Behalf Of
>Richard Yee
>Sent: Monday, October 08, 2001 3:27 AM
>To: [EMAIL PROTECTED]
>Subject: Re: servlet to read a file
>
>
>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/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
>
>
>_________________________________________________________
>Do You Yahoo!?
>Get your free @yahoo.com address at http://mail.yahoo.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
>


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

Reply via email to