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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xtremebass
    New Member
    • Nov 2008
    • 38

    #1

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

    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.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • xtremebass
      New Member
      • Nov 2008
      • 38

      #3
      Originally posted by r035198x
      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..

      [CODE=java]<%@ page import="java.io .*" %>
      <html>
      <head>
      <h2> Sample File Reading through Jsp </h2></head>

      <%
      try
      {

      BufferedReader input = new BufferedReader( new FileReader("mm. html"));
      String line = "";
      while ((line=input.re adLine()) != null)
      {
      out.println(lin e);
      }
      output.flush();
      input.close();
      }
      catch(Exception e)
      {
      out.println(e);
      }
      %>
      </html>[/CODE]
      Last edited by r035198x; Nov 12 '08, 07:27 AM. Reason: added code tags

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        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?

        Comment

        • xtremebass
          New Member
          • Nov 2008
          • 38

          #5
          Originally posted by r035198x
          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.FileNo tFoundException : 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.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            You need to use the servlet context to get to that file's path, something like

            [CODE=java]URL url =config.getServ letContext().ge tResource("mm.h tml");
            BufferedReader br =new BufferedReader( new InputStreamRead er(url.openStre am()));[/CODE]

            Comment

            • xtremebass
              New Member
              • Nov 2008
              • 38

              #7
              Originally posted by r035198x
              You need to use the servlet context to get to that file's path, something like

              [CODE=java]URL url =config.getServ letContext().ge tResource("mm.h tml");
              BufferedReader br =new BufferedReader( new InputStreamRead er(url.openStre am()));[/CODE]
              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."

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                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.

                Comment

                • xtremebass
                  New Member
                  • Nov 2008
                  • 38

                  #9
                  [quote] java.io.FileNot FoundException: mm.html (No such file or directory) [quote]

                  Code:
                  <%@ page import="java.io.*" %>
                  <%@ page import="javax.servlet.ServletConfig.*" %>
                  <%@ page import="javax.servlet.ServletContext.*" %>
                  <%@ page import="javax.servlet.ServletException.*" %>
                  
                  
                  <html>
                  <head>
                  <h2> Sample File Reading through Jsp </h2></head>
                  
                  <%
                  try
                  {
                  
                  
                  File file = new File("mm.html");
                  
                          InputStreamReader streamReader =
                                  new InputStreamReader(new FileInputStream(file));
                  
                          BufferedReader br = new BufferedReader(streamReader);
                  
                          String line = new String();
                          System.out.println(file.getName());
                          System.out.println("================");
                          while (br.ready()) {
                             line = br.readLine();
                              System.out.println(line);
                          }
                  
                  }
                  catch(Exception e)
                  {
                  out.println(e);
                  }
                  %>
                  
                  </html>

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Did you see my reply #6?

                    Comment

                    Working...