Displaying a file in a JSP

Hello, I am new to the world of JSP programming. I was wondering if anyone would be able to help me with some advice?
I need to be able to display the contents of a simple text file on a web page written in JSP and Java.
I am happy with writing a JAVA program to read a file but my problem comes with how to pass the file content back to the JSP.
Should I pass the file content back to the JSP in a line at a time or buffer the output into a StringBuffer (for example) and return that to the JSP. Which would be easier or the best way to handle this?
How do I get the JSP to display the return content.
Any help or advice happily received.
Thanks.

Your problem is not very clearly defined, please give more detail on what you really want to do.
e.g. where the text file is stored? client side? server side? Is the file created in run time? Is your Java program running in the local machine?

Similar Messages

  • Displaying pdf file in a jsp page...

    hi,
    I want to display a pdf document in a jsp page. i got that pdf file from a servlet class(MVC Architecture) through session. now my problem is to display that pdf file in my jsp page. can anyone give me a idea to solve this problem..

    hi,
    i used that code in scriplets. Actually my problem is I got one pdf file from session and i stored that file in a File object. the code is ..
    File pdfDocument = (File) session.getAttribute(CommonConstants.EBILL_PDF_DOCUMENT);
    now i have to display this pdf file in jsp..

  • Error in  displaying  doc file  on a jsp

    Hi
    I have an Web appln which retrieves a file stored in the DB. I am using a clob datatype
    I have the data stored in an InputStream . When i try to display the file the file contains junk values and the data is not displayed properly . Sometimes i get an error saying that the word document is corrupt
    Here is my code :
    String fileName = bioFrm.getBiography().getBioFileName();
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    response.setHeader("cache-control", "must-revalidate");
    InputStream in = bioFrm.getBiography().getBioFileData();
    BufferedReader bf = new BufferedReader(new InputStreamReader(in));
    byte[] buf = new byte[1024];
    int count;
    while ((count = in.read(buf))>= 0) {
         String line = bf.readLine();
         out.write(line);
    in.close();
    I had tried storing the data in a file array but I do not know how to convert a byte[] into an outpustream. I cannot use response.getOutputStream() bcoz the user should have the functionality to download the file twice which i guess is not possible using response.getOutputStream(). I am using struts so i tried doing the same thing using an Action class but still the file was not getting downloaded correctly . Thanks in advance

    I think the problem is in your read loop. Try:
    String line = null;
    while ((line  = bf.readLine()) != null) {
           out.write(line);
    }

  • Displaying .shp file in a JSP page

    hi all,
    For my application i have to display a map. That one is a .shp file. And it is not a stored one in my system.In that map i have to show a car moving at that particular time, so it will we keep on changing and that one i have to get it from other client every movement and have to display.
    Freinds please tell me how to do that.
    i will be waiting for u.
    thanks in advance
    regards
    srinivas

    I fail to see the relevance with JSP/JSTL
    To read the File I suggest you examine the Java File API
    To render moving grahpics I suggest you examine the Java Graphics classes
    To connect Multiple client I suggest you study Java Sockets, both server and client Sockets.

  • Display uploaded file on JSP page

    Hi All,
    I want to display uploaded file in JSP.
    Initially I am displaying File Name after saving the file & click on edit link.
    i.e on JSP Page I have File Upload component & save button.With that save button I am saving my file to databse.
    So when I click on edit link I am getting that file name from Databse then I am displaying it.
    But now I want to display uploaded file name on JSP page before saving to databse.i.e I will have browse & Upload button.When I click on browse button I will open window from where I will select file.
    This is working fine as,<x:inputFileUpload id="uploadfile" value="#{errorLotBean.file}" storage="file"></x:inputFileUpload>
    But when I click on upload button that uploaded file should be displyed there only.Can anyone please tell me how to do this ?
    Thanks
    Sandip

    Thanks a lot Siefert,
    I tried the way mentioned in URL.
    But what if I want to display all uploaded file on my screen.
    i.e. If user click on browse & select File A then click on upload button.
    (Here the File A will be displayed) with code
    <h:outputtext value="#{benaName.file.name}"
    But what if after displaying File A if user decide to upload another file File B.
    How to display that ?
    with <h:outputtext value="#{benaName.file.name}" its dispalying one file only.
    Thanks
    Sandip

  • Displaying a generated pdf in jsp

    I've generated a pdf using jasper report, which is present in the dir(d:/reports), i need to display this file in the jsp(reports.jsp),i cannot create the report in the jsp.....

    You can write a Servlet which would read the file contents using the standard input/output stream of java and render the response...
    For reference find the code snippet:
    String fileName=file.getName();
    response.setHeader("Content-Disposition","attachment; filename=\""+fileName+"\"");
    response.setContentLength((int)file.length());
    response.setContentType("application/zip");
    java.io.InputStream in = null;
    java.io.OutputStream out = null;
    try {
    in = new java.io.BufferedInputStream(new FileInputStream(file) );
    out = new java.io.BufferedOutputStream(response.getOutputStream() );
    pipe(in,out,2048);
    } finally {
    if( in != null ) try {
    in.close();
    } catch( IOException ioException ) { ioException.printStackTrace(); }
    in.close();
    out.flush();
    out.close();
    public  long pipe(java.io.InputStream in, java.io.OutputStream out,
                   int chunkSize) throws IOException {
              if( chunkSize < 1 ) {
                   throw new IOException("Invalid chunk size.");
              byte[] buf = new byte[chunkSize];
              long tot = 0;
              int n;
              while( (n=in.read(buf)) != -1 ) {
                   out.write(buf,0,n);
                   tot += n;
              return tot;
         }

  • How to display XML file(as markup) in jsp page..?

    Hi All,
    * I have to display the XML file(as markup) in jsp page (Tree Format)....
    * My XML file is an java.io.file object , and how to view this XML file on my JSP page...........
    Thanks in Advance,
    JavaImran

    You mean you want to see the XML source?
    You need to replace the characters '<' and '&' with corresponding entities '&lt;' and '&amp;'. You can use replaceAll, but do the ampersands first.
    Then I suggest you probably want to put them in a <PRE> block.

  • How to display RTF File in Browser Using Servlets/JSPs

    Hi All,
    I have some RTF Files, which contains some data.
    The data needs to be displayed in the frame of jsp page .
    How i can display the RTF File Content using either servlets/jsps
    Can any body have idea on this.
    Help me out on this.
    With Regards
    Hari

    If you only need to display it, consider your HttpServletRequest just as byte stream. :) Just read from ServletInputStream and write it back into ServletOutputStream of HttpServletResponse.
    If you want to process incoming XML, please give some details first, what exactly you want to do. :)

  • Want to display the contents of a text file in a jsp

    hi
    i am new to jsp, i want to display the contents of a text file in a jsp file ,whenever jsp file is loaded from browser.
    -thanx

    i had come up with the code
    <!-- form.jsp -->
    <%@ page import="java.io.*, java.lang.*" %>
    <%@ page language="java" %>
    <html>
    <head>
    <title>Display</title>
    </head>
    <body>
    <%
    String sample[100];
    String line;
    %>
    <%! int i=0; %>
    <%!File f1 = new File("c:\\file.txt"); %>
    <% try { %>
    <%!FileReader fi = new FileReader(f1); %>
    <% } catch(FileNotFoundException io) { } %>
    <%!BufferedReader in = new BufferedReader(fi); %>
    <%
    while((line=in.readLine())!= null)
    sample= line;
    i++;
    in.close();
    %>
    i will display sample here
    </body>
    </html>
    but i am getting error
    display_jsp.java:16: unreported exception java.io.FileNotFoundException;
    must be caught or declared to be thrown
    FileReader fi = new FileReader(f1);
    ^
    plz help
    -thanx

  • How to display pdf file with java code?

    Hi All,
    i have a jsp pagein that page if i click one icon then my backing bean method will call and that method will create pdf document and write some the content in that file and now i want to display that generated pdf document.
    it should look like you have pressed one icon and it displayed pdf file to you to view.
    java code below:
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext ec = fc.getExternalContext();
    HttpServletResponse resp = (HttpServletResponse) ec.getResponse();
    resp.setHeader("Content-Disposition", "filename=\"" + temppdfFile);
    resp.encodeRedirectURL(temppdfFile.getAbsolutePath());
    resp.setContentType("application/pdf");
    ServletOutputStream out = resp.getOutputStream();
    ServletUtils.returnFile(temppdfFile, out);
    out.flush();
    and above temppdfFile is my generated pdf file.
    when i am executing this code, it was opening dialog box for save and cancel for the file, but the name of the file it was showing me the "jsp file name" with no file extention (in wich jsp file i am calling my backing bean method) and type is "Unknown File type" and from is "local host"
    it was not showing me open option so i am saving that file then that file saved as a pdffile with tha name of my jsp file and there is nothing in that file(i.e. empty file).
    what is the solution for this. there is any wrong in my code.
    Please suggest me.
    Thanks in advance
    Indira

    public Object buildBarCodes() throws Exception
    File bulkBarcodes = null;
    String tempPDFFile = this.makeTempDir();
    File tempDir = new File("BulkPDF_Files_Print");
    if(!tempDir.exists())
    tempDir.mkdir();
    bulkBarcodes = File.createTempFile
    (tempPDFFile, ".pdf", tempDir);
    //bulkBarcodes = new File(tempDir, tempPDFFile+"BulkBarcode"+"."+"pdf");
    if(!bulkBarcodes.exists())
    bulkBarcodes.createNewFile();
    Document document = new Document();
    FileOutputStream combinedOutput = new FileOutputStream(bulkBarcodes);
    PdfWriter.getInstance(document, combinedOutput);
    document.open();
    document.add(new Paragraph("\n"));
    document.add(new Paragraph("\n"));
    Image image = Image.getInstance(bc.generateBarcodeBytes(bc.getBarcode()));
    document.add(image);
    combinedOutput.flush();
    document.close();
    combinedOutput.close();
    FacesContext facesc = FacesContext.getCurrentInstance();
    ExternalContext ec = facesc.getExternalContext();
    HttpServletResponse resp = (HttpServletResponse) ec.getResponse();
    resp.setHeader("Content-Disposition", "inline: BulkBarcodes.pdf");
    resp.encodeRedirectURL(bulkBarcodes.getAbsolutePath());
    resp.setContentType("application/pdf");
    resp.setBufferSize(10000000);
    ServletOutputStream out = resp.getOutputStream();
    ServletUtils.returnFile(bulkBarcodes, out);
    out.flush();
    return "success";
    This is my action method which will call when ever i press a button in my jsp page.
    This method will create the barcode for the given barcode number and write that barcode image in pdf file
    (i saw that pdf file which i have created through my above method, This PDF file opening when i was opening manually and the data init that is also correct means successfully it writes the mage of that barcode)
    This method taking the jsp file to open because as earlier i said it was trying to open unknown file type document and i saved that file and opended with editplus and that was the jsp file in which file i am calling my action method I mean to say it was not taking the pdf file which i have created above it was taking the jsp file

  • I used tag to show PDF file inside my JSP page, It is working properly in IE but nothing shows in Mozilla. What should I do?

    I've used to display a PDF file inside my JSP page. It is working fine in IE. However, many of users of this application are using Mozilla and the platform is Ubuntu so it is vital that the file is shown in Mozilla as well. How can I solve this problem.
    == This happened ==
    Every time Firefox opened
    == I run my jsp page

    Such conditional code will only work in IE and not in other browsers like Firefox.
    You can ask questions and advice about web development at the mozillaZine Web Development/Standards Evangelism forum.<br />
    A good place to ask questions and advice about web development is at the mozillaZine Web Development/Standards Evangelism forum.<br />
    The helpers at that forum are more knowledgeable about web development issues.<br />
    You need to register at the mozillaZine forum site in order to post at that forum.<br />
    See http://forums.mozillazine.org/viewforum.php?f=25

  • Display .XLS file in Browser

    Hi All,
    I am trying to display the .XLS file from a JSP page.
    by just using a PopUp using Java Script.
    But is just displaying some junch data (which may be binary)
    Can some one help me out to resolve this.
    Thanks
    Shravan

    Are you using the right contentType?
    ContentType="application/vnd.ms-excel"

  • Displaying Korean Characters on my jsp

    I would like to display korean characters in my jsp....the characters that i want to display are read from an xml file using the sax parser... how will i do this?

    Case 1 - In a JSP only.
    In a JSP you need to set the following and it will display your respective charset. Substitute xxx for your required character set.
    <%response.setContentType="charset=xxx"%>.
    Case 2 - In an XML file and JSP
    If you have an XML file, be sure to define the encoding. Be sure to change the encoding to your Korean charset.
    <?xml version="1.0" encoding="ISO-8859-2"?>
    You will the need to write an XSL which you can apply to your XML file using Xalan and output the resulting HTML in a JSP. I assume your user-agent will be a web browser, although you can use WML, cHTML and VoXML or even another XML file.
    Rajesh Thiharie
    New Delhi, India
    91 124 6455511 x 109 Work

  • How to display the file contents???

    Hi all,
    I have a JSP page which is suppose to display resource contents on clicking of href like this:
    <td><a href="">Click here</td>But the problem is, these contents are stored some where on the hard disk of the system inside the folder \\LMS\Resources\doc\knowledge.doc.
    some other file I have like:
    \\LMS\Resources\pdf\display.pdf
    It can be any file (PDF,DOC,SOUND AUDIO).
    Can anyone please tell me how to open the contents of the file directly from href???
    Or I should use some other way to display those files.??I just want a way to start with.......
    Thanks in advance
    Namrata

    Hello Namrata,
    There are millions of ways to do this.
    1. You can create a shared drive in your server as "\\LMS\Resources" pointing to K Drive etc... You can write a simple servlet which picks up the relative path from "\\LMS\Resources" which is "pdf\display.pdf" and it can read the file and then based on the file extension it can resend the response mime type for the browser to pick it up. This can be done in both Windows and Unix environments.
    2. Have a Content Management System like Lotus Domino, Fatwire, etc and upload all contents and point the href to the Content Management System URL.
    3. You can upload the documents into a database as BLOB data and then use it to render content.
    4. You can also have a regular program which synchronizes the data from \\LMS into the Server hardware in a particular location.
    In Options 1, 3, 4 you need the servlet to render the content out to the Client browser.
    All this can be done based on the Technology environment, cost available to spend, Skill sets available and other factors.
    Do let me know if you need any more information
    Thanks,
    Pazhanikanthan. P

  • Attachement file links in JSP dont open

    Hi All,
    In my JSP page, I have attachment file links which are as follows:
    xlserror.ppt
    When I click on the link, a new browser opens and it shows all garbage. The browser is not able to find PowerPoint application to display the file. What could be the problem? Am using Win NT as my server and Tomcat as the JSP container.
    Previously I had this application in IIS, ASP combination which opened all the attachments in the browser by finding the correct application.
    Another question is:
    Is how you name the file critical? Coz following is one of the prblems that I have seen. If I name the file "xlserror#1.ppt" then following is the link:
    xlserror#1.ppt
    When I click on the link, the browser goes to the following location:
    http://domain/MyApp/Attachments/757/xlserror
    and therefore I get "resource not found" error. Is using '#' in the filename not good practice? I never had this experience with ASP.
    Regards,
    m_asu

    1) For your browser to know what application to use you have to declare the kind of content you are sending to the browser.
    So you have to set the content type of the response object with the proper MIME type.
    For ppt files, i think is application/vnd.ms-powerpoint or something similar, check it.
    2)About the # character: If you have a # in your url ( www.server.com/app/docum#ent) ,your browser will ask for the www.server.com/app/docum and then will scroll down the page till the ent anchor.
    (This is basic html. Maybe you should go to the w3schools.com ;) )
    HTH

Maybe you are looking for