How to display data from a text file onto a webpage using servlets?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naha
    New Member
    • Feb 2007
    • 41

    #1

    How to display data from a text file onto a webpage using servlets?

    Hi Guys,

    I have managed to write a java class which opens and reads a text file and prints the data onto a webpage. however in order to do this I have to compile my java class each time. Does any one know how I could do the same thing using servlets and jsp, I have been trying to do it, but have not succeeded yet.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Naha
    Hi Guys,

    I have managed to write a java class which opens and reads a text file and prints the data onto a webpage. however in order to do this I have to compile my java class each time. Does any one know how I could do the same thing using servlets and jsp, I have been trying to do it, but have not succeeded yet.
    How did you try to set up your JSP and servlet? Which server are you using? Please post the codes you have tried with and the error that you are getting.

    Comment

    • Naha
      New Member
      • Feb 2007
      • 41

      #3
      Originally posted by r035198x
      How did you try to set up your JSP and servlet? Which server are you using? Please post the codes you have tried with and the error that you are getting.
      I used a servlet for now, im using tomcat to run it.

      This is the code below:

      There is an error on line 13:Can not find symbol variable text.

      import javax.servlet.* ;
      import javax.servlet.h ttp.*;
      import java.io.*;

      public class TestServlet extends HttpServlet {

      String valString;

      public void init() throws ServletExceptio n{

      try {

      BufferedReader dataFile = Text.open("Data File.txt");

      valString = dataFile.readLi ne();

      if(valString != null)

      System.out.prin tln(" Data is: " + valString);

      dataFile.close( );
      }


      catch (FileNotFoundEx ception e) {
      System.err.prin tln("FileStream sTest: " + e);


      }
      }
      public void doPost(HttpServ letRequest req, HttpServletResp onse res)
      throws ServletExceptio n, IOException {

      res.setContentT ype("text/html");
      PrintWriter out = new PrintWriter(res .getOutputStrea m());


      out.println("<h tml>");
      out.println("<h ead><title>Temp erature</title>");
      out.println("</head><body>");
      out.println("<b >The temperature today is: " + valString + "</b><br>");
      out.println("</body></html>");

      out.close();
      }



      }

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Naha
        I used a servlet for now, im using tomcat to run it.

        This is the code below:

        There is an error on line 13:Can not find symbol variable text.

        import javax.servlet.* ;
        import javax.servlet.h ttp.*;
        import java.io.*;

        public class TestServlet extends HttpServlet {

        String valString;

        public void init() throws ServletExceptio n{

        try {

        BufferedReader dataFile = Text.open("Data File.txt");

        valString = dataFile.readLi ne();

        if(valString != null)

        System.out.prin tln(" Data is: " + valString);

        dataFile.close( );
        }


        catch (FileNotFoundEx ception e) {
        System.err.prin tln("FileStream sTest: " + e);


        }
        }
        public void doPost(HttpServ letRequest req, HttpServletResp onse res)
        throws ServletExceptio n, IOException {

        res.setContentT ype("text/html");
        PrintWriter out = new PrintWriter(res .getOutputStrea m());


        out.println("<h tml>");
        out.println("<h ead><title>Temp erature</title>");
        out.println("</head><body>");
        out.println("<b >The temperature today is: " + valString + "</b><br>");
        out.println("</body></html>");

        out.close();
        }



        }
        1.) Use code tags when posting code. It's easier to read that way
        2.)The code for opening the file should be put in the doPost Method as well. Use the init for initializing servlet and environment parameters only.
        3.)What is Text in Text.open ...
        You should use
        Code:
         
        BufferedReader dataFile = BufferedReader(new FileReader("DataFile.txt"));

        Comment

        • Naha
          New Member
          • Feb 2007
          • 41

          #5
          Is this how its suppose to be?

          Its giving and error on line 19.

          Error line 19: cannot find symbol method BufferedReader (java.io.FileRe ader)

          Code:
          import javax.servlet.*;
          import javax.servlet.http.*;
          import java.io.*;
          
          public class TestServlet extends HttpServlet {
          	
          	String  valString;
          
          
          			public void doPost(HttpServletRequest req, HttpServletResponse res) 
          			throws ServletException, IOException { 
          
          			res.setContentType("text/html");
          			PrintWriter out = new PrintWriter(res.getOutputStream());
          			
          			
          			 try { 
          			 
          			 BufferedReader dataFile = BufferedReader(new FileReader("DataFile.txt"));
          			
          			valString = dataFile.readLine();
          			
          			if(valString != null)
          			
          			System.out.println(" Data is: " + valString);
          			
          			dataFile.close();
                      }
          
          
                  catch (FileNotFoundException e) {
                      System.err.println("FileStreamsTest: " + e);
                      
                 
                  }
          			
          			
          			
          			out.println("<html>");
          			out.println("<head><title>Temperature</title>");
          			out.println("</head><body>");
          			out.println("<b>The temperature today is: " +   valString + "</b><br>");
          			out.println("</body></html>");
          			
          			out.close();
          		}
                  
                  
           
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Naha
            Is this how its suppose to be?

            Its giving and error on line 19.

            Error line 19: cannot find symbol method BufferedReader (java.io.FileRe ader)

            Code:
             
            import javax.servlet.*;
            import javax.servlet.http.*;
            import java.io.*;
             
            public class TestServlet extends HttpServlet {
             
            	String valString;
             
             
            			public void doPost(HttpServletRequest req, HttpServletResponse res) 
            			throws ServletException, IOException { 
             
            			res.setContentType("text/html");
            			PrintWriter out = new PrintWriter(res.getOutputStream());
             
             
            			 try { 
             
            			 BufferedReader dataFile = BufferedReader(new FileReader("DataFile.txt"));
             
            			valString = dataFile.readLine();
             
            			if(valString != null)
             
            			System.out.println(" Data is: " + valString);
             
            			dataFile.close();
            }
             
             
            catch (FileNotFoundException e) {
            System.err.println("FileStreamsTest: " + e);
             
             
            }
             
             
             
            			out.println("<html>");
            			out.println("<head><title>Temperature</title>");
            			out.println("</head><body>");
            			out.println("<b>The temperature today is: " + valString + "</b><br>");
            			out.println("</body></html>");
             
            			out.close();
            		}
             
             
             
            }
            Well it does not give an error on my compiler. Try splitting it up to

            Code:
             
            FileReader fr = new FileReader("DataFile.txt");
            BufferedReader br = new BufferedReader(fr);
            If that still does not work you may want to try using Scanner if your version supports jdk 1.5. But the above should work anyway.

            Comment

            • Naha
              New Member
              • Feb 2007
              • 41

              #7
              Thank you for that, its not giving mean errors now. However its not displaying the data on to the webpage. Do you know what the problem is?



              Code:
              import javax.servlet.*;
              import javax.servlet.http.*;
              import java.io.*;
              
              public class TestServlet extends HttpServlet {
              	
              	String  valString;
              
              
              			public void doPost(HttpServletRequest req, HttpServletResponse res) 
              			throws ServletException, IOException { 
              
              			res.setContentType("text/html");
              			PrintWriter out = new PrintWriter(res.getOutputStream());
              			
              			
              			 try { 
              			 
              			 FileReader dFile = new FileReader("DataFile.txt");
              			 
              			 BufferedReader dataFile = new BufferedReader(dFile);
              			
              		
              			
              			valString = dataFile.readLine();
              			
              			if(valString != null)
              			
              			System.out.println(" Data is: " + valString);
              			
              			dataFile.close();
                          }
              
              
                      catch (FileNotFoundException e) {
                          System.err.println("FileStreamsTest: " + e);
                          
                     
                      }
              			
              			
              			
              			out.println("<html>");
              			out.println("<head><title>Temperature</title>");
              			out.println("</head><body>");
              			out.println("<b>The temperature today is: " +   valString + "</b><br>");
              			out.println("</body></html>");
              			
              			out.close();
              		}
                      
                      
               
              }

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Naha
                Thank you for that, its not giving mean errors now. However its not displaying the data on to the webpage. Do you know what the problem is?



                Code:
                 
                import javax.servlet.*;
                import javax.servlet.http.*;
                import java.io.*;
                 
                public class TestServlet extends HttpServlet {
                 
                	String valString;
                 
                 
                			public void doPost(HttpServletRequest req, HttpServletResponse res) 
                			throws ServletException, IOException { 
                 
                			res.setContentType("text/html");
                			PrintWriter out = new PrintWriter(res.getOutputStream());
                 
                 
                			 try { 
                 
                			 FileReader dFile = new FileReader("DataFile.txt");
                 
                			 BufferedReader dataFile = new BufferedReader(dFile);
                 
                 
                 
                			valString = dataFile.readLine();
                 
                			if(valString != null)
                 
                			System.out.println(" Data is: " + valString);
                 
                			dataFile.close();
                }
                 
                 
                catch (FileNotFoundException e) {
                System.err.println("FileStreamsTest: " + e);
                 
                 
                }
                 
                 
                 
                			out.println("<html>");
                			out.println("<head><title>Temperature</title>");
                			out.println("</head><body>");
                			out.println("<b>The temperature today is: " + valString + "</b><br>");
                			out.println("</body></html>");
                 
                			out.close();
                		}
                 
                 
                 
                }
                Is it displaying anything at all in the JSP page?

                Comment

                • Naha
                  New Member
                  • Feb 2007
                  • 41

                  #9
                  Basically,

                  I have declared the servlet in the deployment descriptor,

                  Do I have to specify which html file i want the data to be printed out on in the servlet code?

                  Right now I just have a html file, called weather.html.


                  It gives me the error below:

                  HTTP Status 404 - /ee05u026/TestServlet

                  --------------------------------------------------------------------------------

                  type Status report

                  message /ee05u026/TestServlet

                  description The requested resource (/ee05u026/TestServlet) is not available.


                  --------------------------------------------------------------------------------

                  Apache Tomcat/5.5.17

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Naha
                    Basically,

                    I have declared the servlet in the deployment descriptor,

                    Do I have to specify which html file i want the data to be printed out on in the servlet code?

                    Right now I just have a html file, called weather.html.


                    It gives me the error below:

                    HTTP Status 404 - /ee05u026/TestServlet

                    --------------------------------------------------------------------------------

                    type Status report

                    message /ee05u026/TestServlet

                    description The requested resource (/ee05u026/TestServlet) is not available.


                    --------------------------------------------------------------------------------

                    Apache Tomcat/5.5.17
                    As the last line of the doPost method put
                    Code:
                     
                    req.getRequestDispatcher("outputJSP.jsp").include(req, resp);
                    To show the page that you want to display

                    Comment

                    • Naha
                      New Member
                      • Feb 2007
                      • 41

                      #11
                      I added the code in bold - is it meant to render the jsp page automatically.

                      should I rename my weather.html page to weather.jsp?






                      Code:
                      import javax.servlet.*;
                      import javax.servlet.http.*;
                      import java.io.*;
                      
                      public class TestServlet extends HttpServlet {
                      	
                      	String  valString;
                      
                      
                      			public void doPost(HttpServletRequest request, HttpServletResponse response) 
                      			throws ServletException, IOException { 
                      
                      			response.setContentType("text/html");
                      			PrintWriter out = new PrintWriter(response.getOutputStream());
                      			
                      			
                      			 try { 
                      			 
                      			 FileReader dFile = new FileReader("DataFile.txt");
                      			 
                      			 BufferedReader dataFile = new BufferedReader(dFile);
                      			
                      			 //File outputFile = new File("weather.html");
                      		
                      			
                      			valString = dataFile.readLine();
                      			
                      			if(valString != null)
                      			
                      			System.out.println(" Data is: " + valString);
                      			
                      			dataFile.close();
                      			
                      		[B]RequestDispatcher view = 	request.getRequestDispatcher("weather.jsp");[/B]
                      		
                      		view.forward(request, response);
                      		
                      	
                      			
                      			
                      			
                                  }
                      
                      
                              catch (FileNotFoundException e) {
                                  System.err.println("FileStreamsTest: " + e);
                                  
                             
                              }
                      			
                      			
                      			
                      			out.println("<html>");
                      			out.println("<head><title>Temperature</title>");
                      			out.println("</head><body>");
                      			out.println("<b>The temperature today is: " +   valString + "</b><br>");
                      			out.println("</body></html>");
                      			
                      			out.close();
                      		}
                              
                              
                       
                      }

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by Naha
                        I added the code in bold - is it meant to render the jsp page automatically.

                        should I rename my weather.html page to weather.jsp?






                        Code:
                         
                        import javax.servlet.*;
                        import javax.servlet.http.*;
                        import java.io.*;
                         
                        public class TestServlet extends HttpServlet {
                         
                        	String valString;
                         
                         
                        			public void doPost(HttpServletRequest request, HttpServletResponse response) 
                        			throws ServletException, IOException { 
                         
                        			response.setContentType("text/html");
                        			PrintWriter out = new PrintWriter(response.getOutputStream());
                         
                         
                        			 try { 
                         
                        			 FileReader dFile = new FileReader("DataFile.txt");
                         
                        			 BufferedReader dataFile = new BufferedReader(dFile);
                         
                        			 //File outputFile = new File("weather.html");
                         
                         
                        			valString = dataFile.readLine();
                         
                        			if(valString != null)
                         
                        			System.out.println(" Data is: " + valString);
                         
                        			dataFile.close();
                         
                        		[b]RequestDispatcher view = 	request.getRequestDispatcher("weather.jsp");[/b]
                         
                        		view.forward(request, response);
                         
                         
                         
                         
                         
                        }
                         
                         
                        catch (FileNotFoundException e) {
                        System.err.println("FileStreamsTest: " + e);
                         
                         
                        }
                         
                         
                         
                        			out.println("<html>");
                        			out.println("<head><title>Temperature</title>");
                        			out.println("</head><body>");
                        			out.println("<b>The temperature today is: " + valString + "</b><br>");
                        			out.println("</body></html>");
                         
                        			out.close();
                        		}
                         
                         
                         
                        }
                        You should forward after you write with the out.println statements not before otherwise the println statements will not show. Also out.println statements must be encosed in a try whose catch catches an IOException

                        Comment

                        • Naha
                          New Member
                          • Feb 2007
                          • 41

                          #13
                          Hi ,

                          I am trying to link a jsp and servlets using beans, but how do I declare the id in the servlet. I can't seem to link them both. Help!!!!!


                          Thanks in advance.

                          Below is the jsp and servlet code:

                          [Code]

                          <%@ page import='java.ut il.*‘ %>

                          <html>
                          <head>

                          <LINK REL="stylesheet " TYPE="text/css" HREF="styleshee t1.css">

                          <head>

                          <body>

                          <hr></hr>

                          <center>

                          <h1>Weather Forecast</h1>


                          <jsp:useBean id="today" scope="request" class="TestServ let" />


                          <%= request.getOutp utStream() %>

                          [Code]

                          [Code]

                          import javax.servlet.* ;
                          import javax.servlet.h ttp.*;
                          import java.io.*;

                          public class TestServlet extends HttpServlet {

                          String valString;


                          public void doPost(HttpServ letRequest request, HttpServletResp onse response)
                          throws ServletExceptio n, IOException {

                          response.setCon tentType("text/html");
                          PrintWriter out = new PrintWriter(res ponse.getOutput Stream());


                          try {

                          FileReader dFile = new FileReader("Dat aFile.txt");

                          BufferedReader dataFile = new BufferedReader( dFile);


                          valString = dataFile.readLi ne();

                          if(valString != null)

                          System.out.prin tln(" Data is: " + valString);



                          RequestDispatch er view = request.getRequ estDispatcher(" weather.jsp");

                          view.forward(re quest, response);

                          dataFile.close( );



                          }


                          catch (FileNotFoundEx ception e) {
                          System.err.prin tln("FileStream sTest: " + e);


                          }



                          out.println("<h tml>");
                          out.println("<h ead><title>Temp erature</title>");
                          out.println("</head><body>");
                          out.println("<b >The temperature today is: " + valString + "</b><br>");
                          out.println("</body></html>");

                          out.close();
                          }



                          }

                          [Code]

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by Naha
                            Hi ,

                            I am trying to link a jsp and servlets using beans, but how do I declare the id in the servlet. I can't seem to link them both. Help!!!!!


                            Thanks in advance.

                            Below is the jsp and servlet code:

                            [Code]

                            <%@ page import='java.ut il.*‘ %>

                            <html>
                            <head>

                            <LINK REL="stylesheet " TYPE="text/css" HREF="styleshee t1.css">

                            <head>

                            <body>

                            <hr></hr>

                            <center>

                            <h1>Weather Forecast</h1>


                            <jsp:useBean id="today" scope="request" class="TestServ let" />


                            <%= request.getOutp utStream() %>

                            [Code]

                            [Code]

                            import javax.servlet.* ;
                            import javax.servlet.h ttp.*;
                            import java.io.*;

                            public class TestServlet extends HttpServlet {

                            String valString;


                            public void doPost(HttpServ letRequest request, HttpServletResp onse response)
                            throws ServletExceptio n, IOException {

                            response.setCon tentType("text/html");
                            PrintWriter out = new PrintWriter(res ponse.getOutput Stream());


                            try {

                            FileReader dFile = new FileReader("Dat aFile.txt");

                            BufferedReader dataFile = new BufferedReader( dFile);


                            valString = dataFile.readLi ne();

                            if(valString != null)

                            System.out.prin tln(" Data is: " + valString);



                            RequestDispatch er view = request.getRequ estDispatcher(" weather.jsp");

                            view.forward(re quest, response);

                            dataFile.close( );



                            }


                            catch (FileNotFoundEx ception e) {
                            System.err.prin tln("FileStream sTest: " + e);


                            }



                            out.println("<h tml>");
                            out.println("<h ead><title>Temp erature</title>");
                            out.println("</head><body>");
                            out.println("<b >The temperature today is: " + valString + "</b><br>");
                            out.println("</body></html>");

                            out.close();
                            }



                            }

                            [Code]
                            What do you want to do in the servlet? Please explain your intended flow for this program from start to finish.

                            Comment

                            Working...