By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,606 Members | 1,672 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,606 IT Pros & Developers. It's quick & easy.

Is it Posible to read text/html file in jsp ?

P: 38
hi every one.
i am new to this forum.

i have two doubts.

one is , is it possible to read a text/html file in Jsp?

if so, how can we find out the end of each line in the text/html file?

please help me..

Thanks in Advance.
Nov 12 '08 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
You can use Java code in JSPs so you can read any file the same way you would do it in your normal Java programs. The important thing to realize is that JSPs run on the server and so any paths you specify are relative to the server.
Nov 12 '08 #2

P: 38
You can use Java code in JSPs so you can read any file the same way you would do it in your normal Java programs. The important thing to realize is that JSPs run on the server and so any paths you specify are relative to the server.

i have did coding like this,it shows error.. please do verify this reply for correct solution..

Expand|Select|Wrap|Line Numbers
  1. <%@ page import="java.io.*" %>
  2. <html>
  3. <head>
  4. <h2> Sample File Reading through Jsp </h2></head>
  5.  
  6. <%
  7. try
  8. {
  9.  
  10. BufferedReader input = new BufferedReader(new FileReader("mm.html"));
  11. String line = "";
  12. while ((line=input.readLine()) != null)
  13. {
  14.    out.println(line);
  15. }
  16. output.flush();
  17. input.close();
  18. }
  19. catch(Exception e)
  20. {
  21. out.println(e);
  22. }
  23. %>
  24. </html>
Nov 12 '08 #3

10K+
P: 13,264
1.) Please use code tags when posting code.
2.) What was the error message that you got.
3.) Your HTML page does not have a body?
Nov 12 '08 #4

P: 38
1.) Please use code tags when posting code.
2.) What was the error message that you got.
3.) Your HTML page does not have a body?
thanks for reply. i will use code tags for next post,
and i have got an exception when
the browser was trying to display the text file.
the exception which was shown in my browser ,
"java.io.FileNotFoundException: mm.html
(No such file or directory) " so where i have to
place my text file. so that i can avoid this problem. i am using linux os where i have installed apache -tomact server and i have placed both files that is jsp file,and html file in the same directory. so please be tell me where i was made a mistake?
could you not understand anything detaily means i will send a next post with detailed with all information. my main intention is to read a text file in jsp and display the contents in browser .. thats what i need it.
Nov 12 '08 #5

10K+
P: 13,264
You need to use the servlet context to get to that file's path, something like

Expand|Select|Wrap|Line Numbers
  1. URL url =config.getServletContext().getResource("mm.html");
  2. BufferedReader br =new BufferedReader(new InputStreamReader(url.openStream()));
Nov 12 '08 #6

P: 38
You need to use the servlet context to get to that file's path, something like

Expand|Select|Wrap|Line Numbers
  1. URL url =config.getServletContext().getResource("mm.html");
  2. BufferedReader br =new BufferedReader(new InputStreamReader(url.openStream()));
when i am trying to post the new post the post cant be sent shows message like Below mentioned. i have typed lengthy message. i dont know why it message shows like that..

" 1. The message you have entered is too short. Please lengthen your message to at least 20 characters."
Nov 12 '08 #7

10K+
P: 13,264
Make sure your post exceeds 20 characters and is not inside quote tags. Just use the Post reply button at the bottom which doesn't include the usually unnecessary quote.
Nov 12 '08 #8

P: 38
[quote] java.io.FileNotFoundException: mm.html (No such file or directory) [quote]

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ page import="java.io.*" %>
  3. <%@ page import="javax.servlet.ServletConfig.*" %>
  4. <%@ page import="javax.servlet.ServletContext.*" %>
  5. <%@ page import="javax.servlet.ServletException.*" %>
  6.  
  7.  
  8. <html>
  9. <head>
  10. <h2> Sample File Reading through Jsp </h2></head>
  11.  
  12. <%
  13. try
  14. {
  15.  
  16.  
  17. File file = new File("mm.html");
  18.  
  19.         InputStreamReader streamReader =
  20.                 new InputStreamReader(new FileInputStream(file));
  21.  
  22.         BufferedReader br = new BufferedReader(streamReader);
  23.  
  24.         String line = new String();
  25.         System.out.println(file.getName());
  26.         System.out.println("================");
  27.         while (br.ready()) {
  28.            line = br.readLine();
  29.             System.out.println(line);
  30.         }
  31.  
  32. }
  33. catch(Exception e)
  34. {
  35. out.println(e);
  36. }
  37. %>
  38.  
  39. </html>
  40.  
  41.  
Nov 12 '08 #9

10K+
P: 13,264
Did you see my reply #6?
Nov 12 '08 #10

Post your reply

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