Can a servlet read a jsp file and display its contents?

Hi,
I would like to know if Servlets can read a Jsp file and display its contents.. Right now for our website, I am using a html file to be displayed after a successful post operation through the Servlets...
-Thanks!

I posted this in another thread.
Here's some code that reads using a URL. This doesn't work with JSPs as the server executes JSP when it connects but it's useful for other file types. Note that the filename is a URI</h1>This is <code>show.jsp</code></h1>
<hr>
<%! String file = null; %>
<%! java.io.InputStream is = null; %>
<%
file = request.getParameterValues("filename")[0];
try {
   is = (new java.net.URL(file)).openStream();
} catch (java.io.IOException ioe) { out.write(file + " not found!<br>"); }
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(is));
String line = "";
%>
<pre>
<%
while((line = in.readLine()) != null) {
%>
<%=line%>
<%
%>
</pre>

Similar Messages

  • How To read lib*.so files and verbose its content

    Hi
    Is there any command with which we can read shared library files and see whats its contents are i.e. whats are the functions available inside it and how its control flow.
    Regards
    Jeet

    I know of none that shows flow control but you can do a man against nm(1) to see what is in the library (nm libc.so.1).

  • Read a excel file and split its sheets to seperate file ?

    Dear all :
    How to split a excel file into several file, each have one sheet.my code is as below,thanks:
    import org.apache.poi.hssf.usermodel.*;
    import java.io.*;
    import java.util.*;
    import org.apache.poi.poifs.filesystem.*;
    public class Extractxls {
    public static void main(String[] args) throws Exception
    try {
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(args[0]));
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    int sheetCount = wb.getNumberOfSheets();
    System.out.println(sheetCount);
    try {
         while(sheetCount > 0) {
         sheetCount--;
         HSSFWorkbook wb1 = new HSSFWorkbook();
         HSSFSheet s = wb1.createSheet();
         String sheetName = wb.getSheetName(sheetCount);
         HSSFSheet sheet = wb.getSheet(sheetName);
    //     HSSFSheet sheet = wb.getSheetAt(sheetCount);
         System.out.println(sheet.toString());
         s = sheet;
    FileOutputStream fileOut = new FileOutputStream("atest" + sheetCount + ".xls");
    wb.write(fileOut);
    fileOut.close();
    } catch (Exception e) {
         System.err.println("Error: " + e.getMessage());
    } catch (Exception e) { e.toString();}

    The HSSFWorkbook class is part of the POI project:
    http://jakarta.apache.org/poi/
    Regards,
    Dave Gilbert
    JFreeChart Project Leader
    I understand poi porject, my problem is that POI can not read a excel file and split its sheets into seperate file (each have one sheet) ?Does any one know how to do it.

  • How to create a file and store its contents into another file?

    Hi,
    I'm having some trouble trying to create a code where I have to to create a file and store its contents into another file?
    I read the API, but I'm not certain how this file thing works.
    Here's my code so far:
    public static void main(String[] args) throws Exception
              File file = new File("tasks.txt");
              if (file.exists())
                   System.out.println("File already exists");
                   System.exit(0);
              Scanner scan = new Scanner(System.in);
              Scanner scan2 = new Scanner(System.in);
              //Scans the input line by line
              scan.useDelimiter("\\n");
              //Scans the input by tabs
              scan2.useDelimiter("\\t");
              PrintWriter outputs = new PrintWriter("newtasks.txt");
              outputs.print("ok");
              outputs.println(3);
              outputs.close();
         }

    I managed to change my text into uppercase, but how do I store the uppercase content into another file.
    -So this is what I did so far, I took a text file and modified its strings to uppercase.
    -Now I need to put those modified strings into another text file, is there a way where I can do that with my current code?
    -I already tried printwriter, but it doesn't seem to work
    public static void main(String[] args)throws IOException
              //Task[] oneHundredTasks = new Task[100];
              String uppercase;
              String combine;
              Scanner scan = null;
              FileInputStream in = null;
            FileOutputStream out = null;
            PrintWriter output = null;
            try
                 scan = new Scanner(new BufferedReader(new FileReader("tasks.txt")));
                 scan.useDelimiter("\\n");
                 scan.useDelimiter("\\t");
                while (scan.hasNext())
                     if(!scan.hasNext())
                          scan.next();
                     combine = scan.next();
                     uppercase = combine.toUpperCase();
                     System.out.println(uppercase);
            finally
                if (scan != null)
                    scan.close();
            //The program will try the input and output files
            try
                 in = new FileInputStream("tasks.txt");
                out = new FileOutputStream("newtasks.txt");
                int c;
                //The number "-1" is used to indicate that it has reached the end of the stream.
                while ((c = in.read()) != -1)
                    out.write(c);
            finally
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
         }

  • How to browse a file and extract its contents

    i want to browse a file and extract its contents.My code is given below but its not working .Can anybody help me please
    JFileChooser fc = new JFileChooser();
              int returnVal = fc.showOpenDialog(jfrm);
              if (returnVal == JFileChooser.APPROVE_OPTION)
              File file = fc.getSelectedFile();
              //This is where a real application would open the file.
              fileName=file.getName();
              //This is a file function to extract data from a file.
              //It reads data from a file that can be viewed on cmd
              jlab3.setText(fileName);
              try
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream(fileName);
    // Convert our input stream to a
    // DataInputStream
              DataInputStream in =new DataInputStream(fstream);
    // Continue to read lines while
    // there are still some left to read
    // while (in.available() !=0)
    // Print file line to screen
              fileName1=in.readLine();
              jlab3.setText(fileName1);
              in.close();
    catch (Exception e)
              System.err.println("File input error");
              }

    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(jfrm);
    if (returnVal == JFileChooser.APPROVE_OPTION)
    File file = fc.getSelectedFile();
    //This is where a real application would open the file.
    fileName=file.getName();
    //This is a file function to extract data from a file.
    //It reads data from a file that can be viewed on cmd
    jlab3.setText(fileName);
    try
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream(fileName);
    // Convert our input stream to a
    // DataInputStream
    DataInputStream in =new DataInputStream(fstream);
    // Continue to read lines while
    // there are still some left to read
    // while (in.available() !=0)
    // Print file line to screenHere you are reading the first line of the file
      fileName1=in.readLine();
    //}and here you are displaying the line in a (I suppose) JLabel. A JLabel is not very suitable for displaying a file content, except is the file contain few characters !!!
    jlab3.setText(fileName1);
    in.close();
    catch (Exception e)
    System.err.println("File input error");
    }If you want to read the complete file content you must uncomment lines inside while instruction, like this
    while (in.available() > 0) {  // I prefer to test in.available like this, instead of !=
      // append text to a StringBuffer (variable named sb here) or directly in a textarea.
      sb.append(in.readLine());
    ...

  • Read from .txt file and output the content as two arrays

    I am using the contoured move to control the x-y stage. The trajectory datas for x and y axis are generated using my interpolation program and it is stored in a .txt file as two columns. What I want to do is read .txt file and output the content of this file as two arrays. Is there anyone has any ideas? Thanks, the .txt file is attached.
    Attachments:
    R.75.txt ‏172 KB

    Hi Awen,
    This is quite easy to do, you can merely use the "read from spreadsheet file" function to get a 2D array (2 columns and n rows) and then use the index array function to get whatever row/colums you want..
    Hope the attached VI helps you
    When my feet touch the ground each morning the devil thinks "bloody hell... He's up again!"
    Attachments:
    read sprdsheet file.vi ‏27 KB

  • Reading from a text file and displaying the contents with in a frame

    Hi All,
    I am trying to read some data from a text file and display it on a AWT Frame. The contents of the text file are as follows:
    pcode1,pname1,price1,sname1,
    pcode2,pname2,price2,sname1,
    I am writing a method to read the contents from a text file and store them into a string by using FileInputStream and InputStreamReader.
    Now I am dividing the string(which has the contents of the text file) into tokens using the StringTokenizer class. The method is as show below
    void ReadTextFile()
                        FileInputStream fis=new FileInputStream(new File("nieman.txt"));
                         InputStreamReader isr=new InputStreamReader(fis);
                         char[] buf=new char[1024];
                         isr.read(buf,0,1024);
                         fstr=new String(buf);
                         String Tokenizer st=new StringTokenizer(fstr,",");
                         while(st.hasMoreTokens())
                                          pcode1=st.nextToken();
                               pname1=st.nextToken();
              price1=st.nextToken();
                              sname1=st.nextToken();
         } //close of while loop
                    } //close of methodHere goes my problem: I am unable to display the values of pcode1,pname1,price1,sname1 when I am trying to access them from outside the ReadTextFile method as they are storing "null" values . I want to create a persistent string variable so that I can access the contents of the string variable from anywhere with in the java file. Please provide your comments for this problem as early as possible.
    Thanks in advance.

    If pcode1,pname1,price1,sname1 are global variables, which I assume they are, then simply put the word static in front of them. That way, any class in your file can access those values by simply using this notation:
    MyClassName.pcode1;
    MyClassName.pname1;
    MyClassName.price1;
    MyClassName.sname1

  • Upload 2 files and compare its contents

    Hi guys,
    I want to upload 2 files in SAP and compare its contents.
    How am i going to do that?
    Please provide sample code... Thanks a lot!
    rgds,
    Mark

    Hi Mark,
    I expected to use this code, once u can try this code.
      OPEN DATASET lv_filename FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
      IF sy-subrc = 0.
        LOOP AT gi_output1 INTO gwa_output1.
          CLEAR lv_string.
          CONCATENATE gwa_output1-matnr
                      gwa_output1-maktx
                      gwa_output1-werks
                      gwa_output1-bmeng
                      gwa_output1-posnr
                      gwa_output1-idnrk
                      gwa_output1-ojtxp
                      gwa_output1-mngko
                      gwa_output1-mmein
                      gwa_output1-flag
                      INTO lv_string
                      SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
          TRANSFER lv_string TO lv_filename.
          IF sy-subrc <> 0.
            MESSAGE e006(zpdm).
          ENDIF.              "if sy-subrc <> 0.
        ENDLOOP.
         OPEN DATASET lv_filename FOR appending IN TEXT MODE ENCODING DEFAULT.
      IF gi_output2[] is not initial.
        LOOP AT gi_output2 INTO gwa_output2.
          CLEAR lv_string.
          CONCATENATE gwa_output2-matnr
                      gwa_output2-ojtxb
                      gwa_output2-werks
                      gwa_output2-bmeng
                      gwa_output2-posnr
                      gwa_output2-idnrk
                      gwa_output2-ojtxp
                      gwa_output2-mngko
                      gwa_output2-mmein
                      gwa_output2-flag
                INTO  lv_string
           SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
          TRANSFER lv_string TO lv_filename.
          IF sy-subrc <> 0.
            MESSAGE e007(zpdm). " WITH text-006.
          ENDIF.           "if sy-subrc <> 0.
        ENDLOOP.
      ENDIF.

  • Embedding a text file to display its contents on a button click

    I am quite a beginner in this platform. I have text file with me and I want to display its contents to the user(Read Only) on a button click event in my app.(using C#)
    I wish to know how to link/ embed this text file to my app and once it is embedded, how to deploy the file reading.
    P.S. i would really appreciate it if you can provide some sample code.

    thx Rob..plz check this piece of code..
    namespace Syllabus
        public partial class MainPage : PhoneApplicationPage
            public MainPage()
                InitializeComponent();
            private async Task Button_Click(object sender, RoutedEventArgs e)
                try
                      System.IO.Stream src = Application.GetResourceStream(new Uri("Syllabus;vbu.txt", UriKind.Relative)).Stream;
                      string text;
                      using (StreamReader sr = new StreamReader(src))
                          text = sr.ReadToEnd();
                      TextBlock t = new TextBlock();
                      t.Text = text;
                  catch
                      MessageBox.Show("File Not Found");
    On running the code always chokes within the constructor itself ...what's wrong?

  • Reading a unicode file and displaying it on jsp !!!!!

    I've one file that consists of unicode character. I've to read it and display it on the jsp page. I'm able to display it other contents but for unicode i'm unable to display it properly. Could anyone help me how to achieve this problem.....
    in advance thanks a lot...............

    Read this.

  • Reading a Blob (CSV file) and displaying the contents

    Hello Experts,
    I’m currently working on a system that allows the users to upload an Excel spreadsheet (.xls) in the system. The upload page is a PL/SQL cartridge. Then I’ve written a Java servlet (using Oracle Clean Content) to convert the XLS into a CSV and store it back in the database. (it is stored in the “uploaded_files” table as a blob). I’m trying to create another procedure to read the contents of the blob and display a preview of the data on the screen (using an html table (will be done using cartridge)). After the preview, the user can choose to submit the data into the database into the “detail_records” table or simply discard everything.
    Can anyone provide me any guidelines and/or code samples for reading the contents of a blob and displaying it as an html table? My data contains about 10 rows and 20 columns in the spreadsheet. I’ve been looking into using associative arrays or nested tables but I’m getting confused about how to implement them in my situation.
    Any help is greatly appreciated.

    BluShadow,
    Thanks for your response. The reason that it is a blob is that we use the same table for uploaded files for several applications. Some files are text, some excel, some other formats. Hence the table has been set up in a generic sense.
    However, in my procedure, I'm using
    DBMS_LOB.createtemporary and
    DBMS_LOB.converttoclob
    Is there any way you could possibly provide an example of how to read the contents of the file?

  • Reading an XML file and write the contents to another xml file in java

    Hi,
    I am new to xml parsing.My requirement is that I am getting a message (xml) using ibm MQ in the ByteArrayInputStream format.I have to read this xml message and write to another file.
    I am creating a POC for this.
    First I used simple reading and writing concept but the output is "java.io.FileInputStream@3e25a5 "
    Sample xml file
    - <Client>
    <ClientId>1234</ClientId>
    <ClientName>STechnology</ClientName>
    <DTU_ID>567</DTU_ID>
    <ClientStatus>ACTIVE</ClientStatus>
    - <LEAccount>
    <ClientLE>678989</ClientLE>
    <LEId>56743</LEId>
    - <Account>
    <AccountNumber>9876543678</AccountNumber>
    </Account>
    </LEAccount>
    - <Service>
    <Cindicator>Y2Y</Cindicator>
    <PrefCode>980</PrefCode>
    <BSCode>876</BSCode>
    <MandatoryContent>MSP</MandatoryContent>
    </Service>
    </Client>
    code:
    import java.io.ByteArrayInputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    public class ByteArrayInputStreamToXml {
         public static void main(String srg[]) throws IOException{
              InputStream inputStream= new FileInputStream("C:\\soft\\test2\\sample1.xml");
              byte currentXMLBytes[] = inputStream.toString().getBytes();
              ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(currentXMLBytes);
              OutputStream out = new FileOutputStream("C:\\soft\\test\\data.xml");
              int read=0;
              byte[] bytes = new byte[1024];
              while((read = byteArrayInputStream.read(bytes))!= -1){
              out.write(bytes, 0, read);
              out.write( '\n' );
              inputStream.close();
              out.flush();
              out.close();
              System.out.println("New file created!");
    Please suggest me how can I use DOM/SAX parser ,I can see several code on net for reading xml file using SAX/DOM parser but writing an xml file after reading it using ByteArrayInputStream I am not getting .A help through some example Link will also be helpful for me.
    Thanks
    Sumit
    Edited by: user8687839 on Apr 30, 2012 2:37 AM
    Edited by: user8687839 on Apr 30, 2012 2:43 AM

    Thanks I got the result.
    package com.sumit.collections;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    public class ByteArrayInputStreamToXml {
         public static void main(String srg[]) throws IOException{
              InputStream inputStream= new FileInputStream("C:\\soft\\test2\\sample1.xml");
              ByteArrayOutputStream buffer = new ByteArrayOutputStream();
              int nRead; byte[] data = new byte[1024];
              while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
              buffer.write(data, 0, nRead); } buffer.flush();
              byte currentXMLBytes[]= buffer.toByteArray();
              /* byte currentXMLBytes[] = inputStream.toString().getBytes();*/
              ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(currentXMLBytes);
              OutputStream out = new FileOutputStream("C:\\soft\\test\\data.xml");
              int read=0;
              byte[] bytes = new byte[1024];
              while((read = byteArrayInputStream.read(bytes))!= -1){
              out.write(bytes, 0, read);
              out.write( '\n' );
              inputStream.close();
              out.flush();
              out.close();
              System.out.println("New file created!");
    }

  • Read a text file and display a xy graph

    Hi!
    I am using LabVIEW 8.5, and I am trying to read columns from
    a file and plot with xy graph. I can do it with two channels, but I am having
    problems when the numbers of channels connected increase (maybe six or ten).
    My text file looks like this:
    [Where first column x = time, the rest of the columns are
    channels = y]
    8.828        22.604        23.076            [The number of channels connected can
    change]
    9.828        22.604        22.604
    10.828      21.658        22.131
    11.828      22.131        22.131
    12.828      22.131        22.131
    13.828      22.604        22.604
    14.828      21.658        22.604
    15.828      22.131        22.131
    16.828      21.658        23.076
    17.828      22.604        22.131
    18.828      22.131        22.604
    I attached a picture of what I already try… Now I am trying
    a combination of those. If anyone has some
    advice or suggestion, please let me know.
    Sincerely,
    Julieta.
    Solved!
    Go to Solution.
    Attachments:
    xygraph.PNG ‏25 KB

    Thanks for the advice!... I just modified what I had a
    little bit and it is working, maybe is not the optimums solution…
    I would like to make the time invisible in the graph… It that possible? I attached a picture of my graph, because my English is not good.  There are two lines near to 22 (two channels
    connected at that moment), and a line near to 0 which is time (the strong pink one).  I would like to make invisible the strong
    prink one only.  I try to make invisible,
    but I fail… I changed colors looking for another solution and I fail again,
    because it is taking the colors that I choose for the channels.  I can not disable because I need it to make
    the x axes works with the time that I already saved. 
    If someone has an advice or suggestion I really appreciate it…
    Sincerely,
    Julieta.
    Attachments:
    xygraph.PNG ‏9 KB

  • Reading the XML file and displaying teh string with teh desired output

    Hi Gurus,
    I have an xml file as below.
    catalog>
    <book>
    <id>101</id>
    <genre>Computer</genre>
    <author>Jim Cortez</author>
    <title>XML for dummies</title>
    <price>44.95</price>
    <description>An in-depth look at creating mashed potatoes
    with XML.</description>
    </book>
    <book>
    <id>102</id>
    <author>George Bush</author>
    <title>I'm the decider</title>
    <genre>Fantasy</genre>
    <price>0.95</price>
    <description>I like milk and cookies.</description>
    </book>
    </catalog>
    I would like to display the Output as
    [<catalog>:1]
    [<book>:1]
    [<id>:1]
    [101:3]
    [</id>:2]
    [<genre>:1]
    [Computer:3]
    [</genre>:2]
    [<author>:1]
    [Jim Cortez:3]
    [</author>:2]
    [<title>:1]
    [XML for dummies:3]
    ............ etc., etc.,,
    here is the code template.......
    import java.io.*;
    class TagScanner implements TokenStream {
    public static final int BEGIN_TAG_TYPE = 1;
    public static final int END_TAG_TYPE = 2;
    public static final int TEXT_TYPE = 3;
    protected Reader reader = null;
    /** Lookahead char */
    protected char c;
    /** Text of currently matched token */
    protected StringBuffer text = new StringBuffer(100);
    public TagScanner(Reader reader) throws IOException {
    this.reader = reader;
    nextChar();
    protected void nextChar() throws IOException {
    c = (char)reader.read();
    public Token nextToken() throws IOException {
         if ( start of a tag ) {
         // scarf until end of tag
         // type is either BEGIN_TAG_TYPE or END_TAG_TYPE
         if ( end of file ) {
         type = Token.EOF_TYPE;
         text = "end-of-file";
         else {
         // scarf until start of a tag
         type = TEXT_TYPE;
         if ( just whitespace ) {
         // ignore and get another token
    return new Token(type, text.toString());
    Can someone please provide me the logic for the code please........... here is the complete link of the excersie http://www.antlr.org/wiki/display/CS652/Lexer+for+XML
    Many Thanks
    -M

    Can someone please provide me the logic for the code please..........The logic is pretty well spelled out for you in the description of the assignment and the outline for the code you provided. If you mean
    Can someone please do my homework for me....The answer to that is usually yes, someone can do your homework for you, but no, they usually won't actually do it for you.
    However, if you are really stuck on what to do, you should consider:
            if ( start of a tag ) {How would you know that you are at the start of a tag? Once you can answer that, the rest sort of works itself out as long as
                // scarf until end of tagyou realize what it means to 'scarf' and how to determine when an end-of-tag is reached (hint, very similar as to how to determine if you are at the start-of-tag).

  • Reading the XML file and displaying the string with the desired output

    Hi Gurus,
    I have an xml file as below.
    catalog>
    <book>
    <id>101</id>
    <genre>Computer</genre>
    <author>Jim Cortez</author>
    <title>XML for dummies</title>
    <price>44.95</price>
    <description>An in-depth look at creating mashed potatoes
    with XML.</description>
    </book>
    <book>
    <id>102</id>
    <author>George Bush</author>
    <title>I'm the decider</title>
    <genre>Fantasy</genre>
    <price>0.95</price>
    <description>I like milk and cookies.</description>
    </book>
    </catalog>
    I would like to display the Output as
    [<catalog>:1]
    [<book>:1]
    [<id>:1]
    [101:3]
    [</id>:2]
    [<genre>:1]
    [Computer:3]
    [</genre>:2]
    [<author>:1]
    [Jim Cortez:3]
    [</author>:2]
    [<title>:1]
    [XML for dummies:3]
    ............ etc., etc.,,
    here is the code template.......
    import java.io.*;
    class TagScanner implements TokenStream {
    public static final int BEGIN_TAG_TYPE = 1;
    public static final int END_TAG_TYPE = 2;
    public static final int TEXT_TYPE = 3;
    protected Reader reader = null;
    /** Lookahead char */
    protected char c;
    /** Text of currently matched token */
    protected StringBuffer text = new StringBuffer(100);
    public TagScanner(Reader reader) throws IOException {
    this.reader = reader;
    nextChar();
    protected void nextChar() throws IOException {
    c = (char)reader.read();
    public Token nextToken() throws IOException {
    if ( start of a tag ) {
    // scarf until end of tag
    // type is either BEGIN_TAG_TYPE or END_TAG_TYPE
    if ( end of file ) {
    type = Token.EOF_TYPE;
    text = "end-of-file";
    else {
    // scarf until start of a tag
    type = TEXT_TYPE;
    if ( just whitespace ) {
    // ignore and get another token
    return new Token(type, text.toString());
    Can someone please provide me the logic for the code please........... here is the complete link of the excersie
    http://www.antlr.org/wiki/display/CS652/Lexer+for+XML
    Many Thanks
    -M

    Can someone please provide me the logic for the code please..........The logic is pretty well spelled out for you in the description of the assignment and the outline for the code you provided. If you mean
    Can someone please do my homework for me....The answer to that is usually yes, someone can do your homework for you, but no, they usually won't actually do it for you.
    However, if you are really stuck on what to do, you should consider:
            if ( start of a tag ) {How would you know that you are at the start of a tag? Once you can answer that, the rest sort of works itself out as long as
                // scarf until end of tagyou realize what it means to 'scarf' and how to determine when an end-of-tag is reached (hint, very similar as to how to determine if you are at the start-of-tag).

Maybe you are looking for