0

I am trying to write a jsp function that will take the path to a text file as an input, then print the lines of the text file to the webpage.

The following error is thrown: "out cannot be resolved"

<%! public void displayData(String file){ 
     BufferedReader reader = new BufferedReader(new FileReader(file)); 
     String line; 
     while ((line = reader.readLine()) != null) { 
         out.print(line); 
     } 
     reader.close();        
 }%>

 <% displayData(application.getRealPath("/") + "../../test.txt"); %>

What am I doing wrong?

Thank you in advance

user1334130
  • 1,131
  • 3
  • 18
  • 25

1 Answers1

0

The out variable is not declared in the class scope. It's declared in the method local scope of the service method of the JSP file.

To achieve the concrete functional requirement, move the file to public web content and use JSTL <c:import>.

<c:import url="test.txt" />
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi BalusC, which directory is the "public web content" folder? – user1334130 Mar 28 '13 at 00:17
  • There where you put your public web files, like the JSP file which is trying to import that text file. It's the folder where the `/WEB-INF` folder is also sitting in. – BalusC Mar 28 '13 at 00:30
  • That's where I placed it, am I using the , in place of the "file" for the filereader, or am I putting it somewhere else? Sorry, I'm completely new to JSP and very lost :( – user1334130 Mar 28 '13 at 00:38
  • Have you clicked the "JSTL" link in my answer to learn more about it? Have you installed JSTL and declared the core taglib in top of JSP? Have you checked the JSP-produced HTML output by rightclick and *View Source* in browser? – BalusC Mar 28 '13 at 00:39
  • Yes I have been going through the link, the .jar has been installed to the WEB lib folder and the core has been declared. For the project however I don't believe I can use any other library, is there any other way to display the text to the page. Is there a command similar to the php echo or a different print command that is within the scope??? – user1334130 Mar 28 '13 at 00:55
  • Then don't create another method but just do the job in the current method. Or use a [servlet](http://stackoverflow.com/tags/servlets/info), using *scriptlets* is namely strongly [discouraged](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202) since a decade. – BalusC Mar 28 '13 at 00:57
  • Ok, I'll keep going at it. Thanks. – user1334130 Mar 28 '13 at 01:10