473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

While Loop

41
Hi Guys,

I am trying to print lines of data from a text file on to a webapge using jsps. but i can not get the while loop to work. Can Someone help me please???
code is below:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. package testBean;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class TestBean{
  8.  
  9.   private String  valString;
  10.  
  11.     //ServletContext context = pageContext.getServletContext(); 
  12.  
  13.             public String post() 
  14.  
  15.  
  16.  
  17.  
  18.             throws IOException { 
  19.  
  20.  
  21.  
  22.  
  23.         try { 
  24.  
  25.             int recCount = 0;
  26.  
  27.         //File dir = new File( context.getRealPath( "www" ) );
  28.  
  29.         //FileReader dFile = new FileReader("DataFile.txt");
  30.  
  31.              FileReader dFile = new FileReader("w:/www/DataFile.txt");
  32.  
  33.              BufferedReader dataFile = new BufferedReader(dFile);
  34.  
  35.  
  36.  
  37.  
  38.             valString = dataFile.readLine();
  39.  
  40.  //change to while
  41.  
  42.         while(valString != null){
  43.  
  44.             recCount++;
  45.  
  46.             valString = dataFile.readLine();
  47.  
  48.         System.out.println(valString);    
  49.  
  50.  
  51.      }
  52.  
  53.  
  54. dataFile.close();        
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
  65. catch (FileNotFoundException e) {
  66. System.err.println("FileStreamsTest: " + e);
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73.         return valString;    
  74.         }
  75.  
  76.  
  77.  
  78. }
  79.  
Feb 19 '07 #1
23 3527
r035198x
13,262 8TB
Hi Guys,

I am trying to print lines of data from a text file on to a webapge using jsps. but i can not get the while loop to work. Can Someone help me please???
code is below:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. package testBean;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. public class TestBean{
  8.  
  9. private String valString;
  10.  
  11.     //ServletContext context = pageContext.getServletContext(); 
  12.  
  13.             public String post() 
  14.  
  15.  
  16.  
  17.  
  18.             throws IOException { 
  19.  
  20.  
  21.  
  22.  
  23.         try { 
  24.  
  25.             int recCount = 0;
  26.  
  27.         //File dir = new File( context.getRealPath( "www" ) );
  28.  
  29.         //FileReader dFile = new FileReader("DataFile.txt");
  30.  
  31.              FileReader dFile = new FileReader("w:/www/DataFile.txt");
  32.  
  33.              BufferedReader dataFile = new BufferedReader(dFile);
  34.  
  35.  
  36.  
  37.  
  38.             valString = dataFile.readLine();
  39.  
  40. //change to while
  41.  
  42.         while(valString != null){
  43.  
  44.             recCount++;
  45.  
  46.             valString = dataFile.readLine();
  47.  
  48.         System.out.println(valString);    
  49.  
  50.  
  51.     }
  52.  
  53.  
  54. dataFile.close();        
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
  65. catch (FileNotFoundException e) {
  66. System.err.println("FileStreamsTest: " + e);
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73.         return valString;    
  74.         }
  75.  
  76.  
  77.  
  78. }
  79.  
You forgot to specify the problem. What is happening and what do you want to happen?
Feb 19 '07 #2
Naha
41
Using a if statement I was able to print out the first sentence using a jsp, but now i want to print out all the lines from a text file using a while loop but it returns null on the webpage.
Feb 19 '07 #3
r035198x
13,262 8TB
You forgot to specify the problem. What is happening and what do you want to happen?
You should have

Expand|Select|Wrap|Line Numbers
  1. System.out.println(valString);
  2.  
before
Expand|Select|Wrap|Line Numbers
  1.  valString = dataFile.readLine();
Feb 19 '07 #4
r035198x
13,262 8TB
On second look,

What is the post method supposed to do here? It can only return one String. If you are using that returned string for your JSP then you are always going to have null. To display the file's contents in the jsp, read the file in the code for jsp itself and use the out variable to print its contents.
Feb 19 '07 #5
Naha
41
Its still returning null on the webpage.
Feb 19 '07 #6
Naha
41
so do i put the code onto the jsp is that what your saying?

the post method is just a normal method, but im calling this method onto the jsp anyway so why do i have to put the code inside the jsp?
Feb 19 '07 #7
r035198x
13,262 8TB
so do i put the code onto the jsp is that what your saying?

the post method is just a normal method, but im calling this method onto the jsp anyway so why do i have to put the code inside the jsp?
Yes. Put the code for reading the file in your JSP using <% %> tags and write to the page using the implicitly defined variable out.
Feb 19 '07 #8
Naha
41
why is it that when i use out.println it says it cant recognize symbol variable out.
Feb 19 '07 #9
r035198x
13,262 8TB
why is it that when i use out.println it says it cant recognize symbol variable out.
Post the code you are using for this.
Feb 19 '07 #10
Naha
41
Expand|Select|Wrap|Line Numbers
  1. <%    public String post() 
  2.  
  3.  
  4.  
  5.  
  6.             throws IOException { 
  7.  
  8.  
  9.  
  10.  
  11.         try { 
  12.  
  13.             int recCount = 0;
  14.  
  15.  
  16.  
  17.              FileReader dFile = new FileReader("w:/www/DataFile.txt");
  18.  
  19.              BufferedReader dataFile = new BufferedReader(dFile);
  20.  
  21.  
  22.  
  23.  
  24.             valString = dataFile.readLine();
  25.  
  26.  
  27.  
  28.         while(valString != null){
  29.  
  30.             recCount++;
  31.  
  32.                 out.println(valString);
  33.  
  34.             valString = dataFile.readLine();
  35.  
  36.  
  37.  
  38.  
  39.      }
  40.  
  41.  
  42. dataFile.close();        
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52.  
  53. catch (FileNotFoundException e) {
  54. System.err.println("FileStreamsTest: " + e);
  55.  
  56.  
  57. }
  58.  
  59.  
  60.  
  61.         return valString;    
  62.         }
  63.  
  64.      %>   
Is that how i would put it in the jsp? do i need to include the class aswell?
Feb 19 '07 #11
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. <%    public String post() 
  2.  
  3.  
  4.  
  5.  
  6.             throws IOException { 
  7.  
  8.  
  9.  
  10.  
  11.         try { 
  12.  
  13.             int recCount = 0;
  14.  
  15.  
  16.  
  17.              FileReader dFile = new FileReader("w:/www/DataFile.txt");
  18.  
  19.              BufferedReader dataFile = new BufferedReader(dFile);
  20.  
  21.  
  22.  
  23.  
  24.             valString = dataFile.readLine();
  25.  
  26.  
  27.  
  28.         while(valString != null){
  29.  
  30.             recCount++;
  31.  
  32.                 out.println(valString);
  33.  
  34.             valString = dataFile.readLine();
  35.  
  36.  
  37.  
  38.  
  39.     }
  40.  
  41.  
  42. dataFile.close();        
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52.  
  53. catch (FileNotFoundException e) {
  54. System.err.println("FileStreamsTest: " + e);
  55.  
  56.  
  57. }
  58.  
  59.  
  60.  
  61.         return valString;    
  62.         }
  63.  
  64. %> 
Is that how i would put it in the jsp? do i need to include the class aswell?
No need to include the class. The jsp is actually compiled into a servlet which is a class.

Remove also the method header
public String post() . You just need to put the code for reading the file say as the first line after the body tag in the JSP. If you get any more problems post the code for the whole JSP
Feb 19 '07 #12
Naha
41
The page does not display anymore it gives the following error:
The code for the jsp is below aswell.

HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error on token "throws", interface expected

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "}" to complete InterfaceBody

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "Finally" to complete TryStatement

Generated servlet error:
Syntax error on tokens, delete these tokens

Generated servlet error:
Syntax error on token "finally", delete this token


org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error on token "throws", interface expected

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "}" to complete InterfaceBody

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "Finally" to complete TryStatement

Generated servlet error:
Syntax error on tokens, delete these tokens

Generated servlet error:
Syntax error on token "finally", delete this token


org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:297)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:276)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:264)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:303)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.


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



Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <%@ page import="java.util.*,java.io.*" %>
  4. <%@ page import="testBean.TestBean" %>
  5.  
  6.  
  7. <html>
  8. <head>
  9.  
  10. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  11.  
  12.  
  13.  
  14. </head>
  15.  
  16. <body>
  17.  
  18. <hr></hr>
  19.  
  20. <center>
  21.  
  22. <h1> 
  23.  
  24.  
  25.  Weather Forecast</h1>
  26.  
  27.  
  28.  <%
  29.  
  30.  
  31.  
  32.   private String  valString;
  33.  
  34.  
  35.  
  36.  
  37.             throws IOException { 
  38.  
  39.  
  40.  
  41.  
  42.         try { 
  43.  
  44.             int recCount = 0;
  45.  
  46.  
  47.  
  48.              FileReader dFile = new FileReader("w:/www/DataFile.txt");
  49.  
  50.              BufferedReader dataFile = new BufferedReader(dFile);
  51.  
  52.  
  53.  
  54.  
  55.             valString = dataFile.readLine();
  56.  
  57.  
  58.  
  59.         while(valString != null){
  60.  
  61.             recCount++;
  62.  
  63.                 out.println(valString);
  64.  
  65.             valString = dataFile.readLine();
  66.  
  67.  
  68.  
  69.  
  70.      }
  71.  
  72.  
  73. dataFile.close();        
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84. catch (FileNotFoundException e) {
  85. System.err.println("FileStreamsTest: " + e);
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92.         return valString;    
  93.         }
  94.  
  95.  
  96.  %>
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  <jsp:useBean id="temp"  class="TestBean" /> 
  112.  
  113.  <br><br><br>
  114.  
  115. <a href="test.html"> Local Five day forecast </a>
  116.  
  117.  
  118.  
  119. <br><br><br><br><br><br><br>
  120. <table>
  121. <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
  122. </table>
  123.  
  124.  
  125.  
  126.  
  127. <br><br><br><br><br><br><br>
  128.  
  129.  
  130.  
  131. <hr></hr>
  132.  
  133. </body>
  134. </html>
  135.  
  136.  
Feb 19 '07 #13
r035198x
13,262 8TB
The page does not display anymore it gives the following error:
The code for the jsp is below aswell.

HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error on token "throws", interface expected

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "}" to complete InterfaceBody

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "Finally" to complete TryStatement

Generated servlet error:
Syntax error on tokens, delete these tokens

Generated servlet error:
Syntax error on token "finally", delete this token


org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error on token "throws", interface expected

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "}" to complete InterfaceBody

An error occurred at line: 28 in the jsp file: /website2/weather.jsp
Generated servlet error:
Syntax error, insert "Finally" to complete TryStatement

Generated servlet error:
Syntax error on tokens, delete these tokens

Generated servlet error:
Syntax error on token "finally", delete this token


org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:297)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:276)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:264)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:303)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.


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



Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <%@ page import="java.util.*,java.io.*" %>
  4. <%@ page import="testBean.TestBean" %>
  5.  
  6.  
  7. <html>
  8. <head>
  9.  
  10. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  11.  
  12.  
  13.  
  14. </head>
  15.  
  16. <body>
  17.  
  18. <hr></hr>
  19.  
  20. <center>
  21.  
  22. <h1> 
  23.  
  24.  
  25. Weather Forecast</h1>
  26.  
  27.  
  28. <%
  29.  
  30.  
  31.  
  32. private String valString;
  33.  
  34.  
  35.  
  36.  
  37.             throws IOException { 
  38.  
  39.  
  40.  
  41.  
  42.         try { 
  43.  
  44.             int recCount = 0;
  45.  
  46.  
  47.  
  48.              FileReader dFile = new FileReader("w:/www/DataFile.txt");
  49.  
  50.              BufferedReader dataFile = new BufferedReader(dFile);
  51.  
  52.  
  53.  
  54.  
  55.             valString = dataFile.readLine();
  56.  
  57.  
  58.  
  59.         while(valString != null){
  60.  
  61.             recCount++;
  62.  
  63.                 out.println(valString);
  64.  
  65.             valString = dataFile.readLine();
  66.  
  67.  
  68.  
  69.  
  70.     }
  71.  
  72.  
  73. dataFile.close();        
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
  82.  
  83.  
  84. catch (FileNotFoundException e) {
  85. System.err.println("FileStreamsTest: " + e);
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92.         return valString;    
  93.         }
  94.  
  95.  
  96. %>
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. <jsp:useBean id="temp" class="TestBean" /> 
  112.  
  113. <br><br><br>
  114.  
  115. <a href="test.html"> Local Five day forecast </a>
  116.  
  117.  
  118.  
  119. <br><br><br><br><br><br><br>
  120. <table>
  121. <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
  122. </table>
  123.  
  124.  
  125.  
  126.  
  127. <br><br><br><br><br><br><br>
  128.  
  129.  
  130.  
  131. <hr></hr>
  132.  
  133. </body>
  134. </html>
  135.  
  136.  
Remove that throws clause from there. Use a catch block for the try to handle the IOException
Feb 19 '07 #14
Naha
41
It still given me an error:
HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
Illegal modifier for the variable valString; only final is permitted

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
Void methods cannot return a value


org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
Illegal modifier for the variable valString; only final is permitted

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
Void methods cannot return a value


org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:297)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:276)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:264)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:303)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.


--------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. <%@ page import="java.util.*,java.io.*" %>
  4. <%@ page import="testBean.TestBean" %>
  5.  
  6.  
  7. <html>
  8. <head>
  9.  
  10. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  11.  
  12. </head>
  13.  
  14. <body>
  15.  
  16. <hr></hr>
  17.  
  18. <center>
  19.  
  20. <h1> 
  21.  
  22.  Weather Forecast</h1>
  23.  
  24.  <%
  25.   private String  valString;
  26.  
  27.         try { 
  28.  
  29.             int recCount = 0;
  30.  
  31.              FileReader dFile = new FileReader("w:/www/DataFile.txt");
  32.  
  33.              BufferedReader dataFile = new BufferedReader(dFile);
  34.  
  35.             valString = dataFile.readLine();
  36.  
  37.         while(valString != null){
  38.  
  39.             recCount++;
  40.  
  41.                 out.println(valString);
  42.  
  43.             valString = dataFile.readLine();
  44.  
  45.      }
  46.  
  47.  
  48. dataFile.close();        
  49.  
  50. }
  51.  
  52. catch (FileNotFoundException e) {
  53. System.err.println("FileStreamsTest: " + e);
  54.  
  55. }
  56.          return valString;    
  57.  
  58.  %>
  59.  
  60.  <jsp:useBean id="temp"  class="TestBean" /> 
  61.  
  62.  <br><br><br>
  63.  
  64. <a href="test.html"> Local Five day forecast </a>
  65.  
  66. <br><br><br><br><br><br><br>
  67. <table>
  68. <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
  69. </table>
  70.  
  71. <br><br><br><br><br><br><br>
  72.  
  73. <hr></hr>
  74.  
  75. </body>
  76. </html>
  77.  
  78.  
Feb 19 '07 #15
r035198x
13,262 8TB
use this


Expand|Select|Wrap|Line Numbers
  1.  
  2. <%
  3.  
  4.  
  5.  
  6.   String valString;
  7.    try { 
  8.  
  9.    int recCount = 0;
  10.     FileReader dFile = new FileReader("w:/www/DataFile.txt");
  11.        BufferedReader dataFile = new BufferedReader(dFile);
  12.     valString = dataFile.readLine();
  13.     while(valString != null){
  14.      recCount++;
  15.      out.println(valString);
  16.      valString = dataFile.readLine();
  17.     }
  18.    }
  19.    catch (FileNotFoundException e) {
  20.    System.err.println("FileStreamsTest: " + e);
  21.   }
  22.   catch(IOException iOE) {
  23.    iOE.printStackTrace();
  24.   }
  25.   finally {
  26.    dataFile.close();  
  27.   }
  28.  
  29.  
  30. %>
  31.  
Do you want the values to be displayed in a table?
Feb 19 '07 #16
Naha
41
yes I want it in a table, its still givin me the following error:

HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
dataFile cannot be resolved


org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
dataFile cannot be resolved


org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:297)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:276)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:264)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:303)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ page import="java.util.*,java.io.*" %>
  3. <%@ page import="testBean.TestBean" %>
  4.  
  5.  
  6. <html>
  7. <head>
  8.  
  9. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  10.  
  11. </head>
  12.  
  13. <body>
  14.  
  15. <hr></hr>
  16.  
  17. <center>
  18.  
  19. <h1> 
  20.  
  21.  Weather Forecast</h1>
  22.  
  23.  <%
  24.  
  25.   String valString;
  26.    try { 
  27.  
  28.    int recCount = 0;
  29.     FileReader dFile = new FileReader("w:/www/DataFile.txt");
  30.        BufferedReader dataFile = new BufferedReader(dFile);
  31.     valString = dataFile.readLine();
  32.     while(valString != null){
  33.      recCount++;
  34.      out.println(valString);
  35.      valString = dataFile.readLine();
  36.     }
  37.    }
  38.    catch (FileNotFoundException e) {
  39.    System.err.println("FileStreamsTest: " + e);
  40.   }
  41.   catch(IOException iOE) {
  42.    iOE.printStackTrace();
  43.   }
  44.   finally {
  45.    dataFile.close();  
  46.   }
  47.  
  48.  
  49. %>
  50.  
  51.  <jsp:useBean id="temp"  class="TestBean" /> 
  52.  
  53.  <br><br><br>
  54.  
  55. <a href="test.html"> Local Five day forecast </a>
  56.  
  57. <br><br><br><br><br><br><br>
  58. <table>
  59. <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
  60. </table>
  61.  
  62. <br><br><br><br><br><br><br>
  63.  
  64. <hr></hr>
  65.  
  66. </body>
  67. </html>
  68.  
Feb 19 '07 #17
r035198x
13,262 8TB
yes I want it in a table, its still givin me the following error:

HTTP Status 500 -

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

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
dataFile cannot be resolved


org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:375)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


root cause

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 24 in the jsp file: /website2/weather.jsp
Generated servlet error:
dataFile cannot be resolved


org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:297)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:276)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:264)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:303)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ page import="java.util.*,java.io.*" %>
  3. <%@ page import="testBean.TestBean" %>
  4.  
  5.  
  6. <html>
  7. <head>
  8.  
  9. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  10.  
  11. </head>
  12.  
  13. <body>
  14.  
  15. <hr></hr>
  16.  
  17. <center>
  18.  
  19. <h1> 
  20.  
  21. Weather Forecast</h1>
  22.  
  23. <%
  24.  
  25. String valString;
  26. try { 
  27.  
  28. int recCount = 0;
  29.     FileReader dFile = new FileReader("w:/www/DataFile.txt");
  30.      BufferedReader dataFile = new BufferedReader(dFile);
  31.     valString = dataFile.readLine();
  32.     while(valString != null){
  33.      recCount++;
  34.      out.println(valString);
  35.      valString = dataFile.readLine();
  36.     }
  37. }
  38. catch (FileNotFoundException e) {
  39. System.err.println("FileStreamsTest: " + e);
  40. }
  41. catch(IOException iOE) {
  42. iOE.printStackTrace();
  43. }
  44. finally {
  45. dataFile.close(); 
  46. }
  47.  
  48.  
  49. %>
  50.  
  51. <jsp:useBean id="temp" class="TestBean" /> 
  52.  
  53. <br><br><br>
  54.  
  55. <a href="test.html"> Local Five day forecast </a>
  56.  
  57. <br><br><br><br><br><br><br>
  58. <table>
  59. <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
  60. </table>
  61.  
  62. <br><br><br><br><br><br><br>
  63.  
  64. <hr></hr>
  65.  
  66. </body>
  67. </html>
  68.  
Just change to
Expand|Select|Wrap|Line Numbers
  1.  <% 
  2.  
  3.   String valString;
  4.   BufferedReader dataFile = null;
  5.    try {
  6.    int recCount = 0;
  7.     FileReader dFile = new FileReader("w:/www/DataFile.txt");
  8.        dataFile = new BufferedReader(dFile);
  9.     valString = dataFile.readLine();
  10.     while(valString != null){
  11.      recCount++;
  12.      out.println(valString);
  13.      valString = dataFile.readLine();
  14.     }
  15.    }
  16.    catch (FileNotFoundException e) {
  17.    System.err.println("FileStreamsTest: " + e);
  18.   }
  19.   catch(IOException iOE) {
  20.    iOE.printStackTrace();
  21.   }
  22.   finally {
  23.    dataFile.close();
  24.   }
  25.  
  26. %>
  27.  
  28.  
  29.  
What do you want the table to look like?
Feb 19 '07 #18
Naha
41
phew!!!, it seems to be workin now, i put the datafile.close inside the try block. how would i display it in a table though, the code is rather large for a table.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ page import="java.util.*,java.io.*" %>
  3. <%@ page import="testBean.TestBean" %>
  4.  
  5.  
  6. <html>
  7. <head>
  8.  
  9. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  10.  
  11. </head>
  12.  
  13. <body>
  14.  
  15. <hr></hr>
  16.  
  17. <center>
  18.  
  19. <h1> 
  20.  
  21.  Weather Forecast</h1>
  22.  
  23.  <%
  24.  
  25.   String valString;
  26.    try { 
  27.  
  28.    int recCount = 0;
  29.     FileReader dFile = new FileReader("w:/www/DataFile.txt");
  30.        BufferedReader dataFile = new BufferedReader(dFile);
  31.     valString = dataFile.readLine();
  32.     while(valString != null){
  33.      recCount++;
  34.      out.println(valString);
  35.      valString = dataFile.readLine();
  36.      dataFile.close();
  37.     }
  38.    }
  39.    catch (FileNotFoundException e) {
  40.    System.err.println("FileStreamsTest: " + e);
  41.   }
  42.   catch(IOException iOE) {
  43.    iOE.printStackTrace();
  44.   }
  45.   finally {
  46.    //dataFile.close();  
  47.   }
  48.  
  49.  
  50. %>
  51.  
  52.  <jsp:useBean id="temp"  class="TestBean" /> 
  53.  
  54.  <br><br><br>
  55.  
  56. <a href="test.html"> Local Five day forecast </a>
  57.  
  58. <br><br><br><br><br><br><br>
  59. <table>
  60. <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
  61. </table>
  62.  
  63. <br><br><br><br><br><br><br>
  64.  
  65. <hr></hr>
  66.  
  67. </body>
  68. </html>
  69.  
Feb 19 '07 #19
r035198x
13,262 8TB
phew!!!, it seems to be workin now, i put the datafile.close inside the try block. how would i display it in a table though, the code is rather large for a table.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ page import="java.util.*,java.io.*" %>
  3. <%@ page import="testBean.TestBean" %>
  4.  
  5.  
  6. <html>
  7. <head>
  8.  
  9. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  10.  
  11. </head>
  12.  
  13. <body>
  14.  
  15. <hr></hr>
  16.  
  17. <center>
  18.  
  19. <h1> 
  20.  
  21. Weather Forecast</h1>
  22.  
  23. <%
  24.  
  25. String valString;
  26. try { 
  27.  
  28. int recCount = 0;
  29.     FileReader dFile = new FileReader("w:/www/DataFile.txt");
  30.      BufferedReader dataFile = new BufferedReader(dFile);
  31.     valString = dataFile.readLine();
  32.     while(valString != null){
  33.      recCount++;
  34.      out.println(valString);
  35.      valString = dataFile.readLine();
  36.      dataFile.close();
  37.     }
  38. }
  39. catch (FileNotFoundException e) {
  40. System.err.println("FileStreamsTest: " + e);
  41. }
  42. catch(IOException iOE) {
  43. iOE.printStackTrace();
  44. }
  45. finally {
  46. //dataFile.close(); 
  47. }
  48.  
  49.  
  50. %>
  51.  
  52. <jsp:useBean id="temp" class="TestBean" /> 
  53.  
  54. <br><br><br>
  55.  
  56. <a href="test.html"> Local Five day forecast </a>
  57.  
  58. <br><br><br><br><br><br><br>
  59. <table>
  60. <tr><TD><IMG SRC="sunny.gif" ALT="S" BORDER=0 WIDTH=100 HEIGHT=100 ></TD><TD><h4> <%= temp.post() %> </h4></TD> </tr>
  61. </table>
  62.  
  63. <br><br><br><br><br><br><br>
  64.  
  65. <hr></hr>
  66.  
  67. </body>
  68. </html>
  69.  
I suggest you put the close statement back in the finally so it always gets executed. To display the contents of the file in a table you just mix the html with the java code. You haven't said what you want to form the heading of the table and the rows and columns yet.
Feb 19 '07 #20
Naha
41
Something like this: 3 rows and 2 columns.

Expand|Select|Wrap|Line Numbers
  1.               Todays’ Forecast
  2. Temperature    (first line of data)
  3. Light                    (second line of data)
  4.  
  5.  
Thank you so much for all your help, I am really greatefull.
Feb 19 '07 #21
r035198x
13,262 8TB
Something like this: 3 rows and 2 columns.

Expand|Select|Wrap|Line Numbers
  1. Todays’ Forecast
  2. Temperature    (first line of data)
  3. Light     (second line of data)
  4.  
  5.  
Thank you so much for all your help, I am really greatefull.
You'd have to have Temperature and light in an array

Something like

Expand|Select|Wrap|Line Numbers
  1.  <table>
  2.  <tr>
  3.   <th></th>
  4.   <th></th> 
  5.  </tr>
  6. <%
  7.  
  8.   String valString;
  9.   BufferedReader dataFile = null;
  10.    try {
  11.    String[] temp = {"Temperature", "Ligth"};
  12.    int recCount = 0;
  13.     FileReader dFile = new FileReader("w:/www/DataFile.txt");
  14.        dataFile = new BufferedReader(dFile);
  15.     valString = dataFile.readLine();
  16.     while(valString != null){
  17.   %>
  18.   <tr>
  19.   <td><%=temp[recCount]%></td>
  20.   <td><%=valString%></td>
  21.  </tr>
  22. <%
  23.      recCount++;
  24.      //out.println(valString);
  25.      valString = dataFile.readLine();
  26.     }
  27.    }
  28.    catch (FileNotFoundException e) {
  29.    System.err.println("FileStreamsTest: " + e);
  30.   }
  31.   catch(IOException iOE) {
  32.    iOE.printStackTrace();
  33.   }
  34.   finally {
  35.    dataFile.close();
  36.   }
  37.  
  38. %>
  39.  
  40. </table>
  41.  
Might have to modify it a bit. I have to go. Good luck with the rest of it
Feb 19 '07 #22
Naha
41
ok, thanks for your help.
Feb 19 '07 #23
r035198x
13,262 8TB
ok, thanks for your help.
Did you get it to work like you wanted?
Feb 20 '07 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: jsaul | last post by:
Hi there, wouldn't it be useful to have a 'while' conditional in addition to 'if' in list comprehensions? foo = for i in bar: if len(i) == 0: break foo.append(i)
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
9
by: Ben | last post by:
I have two 'Do While Not' statements, that are getting information from the same recordset. If I comment out the first one I can get the results for the second one, and vice-versa. Why is this...
7
by: Mahesh Kumar Reddy.R | last post by:
Hi Can any body resolve this.. In what cases one of the loop constructs better than other interms of speed , space and any other (redability). thanks mahesh
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
12
by: Howard | last post by:
Hello everyone (total VB.NET beginner here), I'm reading the "SAMS Teach Yourself VB.NET In 21 Days" book, and came across an exercise that I can't get to work. The exercise asks that you create...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
10
by: rohitjogya | last post by:
Can anyone tell me the difference bet for loop and while loop execution? ____________________ for (i=0 ; i<10 ; i++) ; /* Do nothing*/ print i; ___________________ i=0;
5
by: Alex | last post by:
Hi I just want to clear something up in my head with while loops and exceptions. I'm sure this will probably be a no brainer for most. Check this simple pseudo-code out (vb.net): ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.