Solved

I want to output to notepad

Posted on 2004-09-23
2
208 Views
Last Modified: 2010-05-18
I have a jsp page
This page run several query  and wiev data
I want to write .txt file this data
Could you help me?
0
Comment
Question by:skychan
2 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 12139825
Just set the content type to 'text/plain' and output your data (no need to output html tags):

<%@ page contentType = "text/plain" %>
<% Map[] results=(Map[]) request.getAttribute("queryResults");
for (int i=0;i<results.length;i++) {%>
<%=results[i].get("Column1")%>,<%=results[i].get("Column2")%>,<%=results[i].get("Column3")%>
<%}%>


The above outputs a comma seperated text output from query results stored in an array of Map.


0
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 30 total points
ID: 12149281
use Log4J or

Simply use this class
package org.prithvi.logger;

import java.io.*;

/**
 *
 * <p>Title: SimpleLog - A Logger Class</p>
 * <p>Description: This class is used to log the errors and messages that helps the developer to debug the messages</p>
* <p> Created on 10-Sep-2004
 * <p>Copyright: Copyright (c) 2004</p>
 * @author Sudhakar Chavali
 * @version 1.0
 */
public class SimpleLog
    {

  /**
   * Holds the stream object
   */
  private static PrintStream stream = System.err;

  /**
   * checks whether to log messages or not. By default it is true
   */
  private static boolean isDebug = true;

  private static SimpleLog log = null;

  /**
   * If stream object is null, it throws the exception.
   * It is called by log methods

   */
  private void raiseError() throws IOException {
    if (stream == null) {
      Exception e = new NullPointerException("Stream value set to null");
      ByteArrayOutputStream out = new ByteArrayOutputStream(10000);
      e.printStackTrace(new PrintStream(out));
      String stack = new String(out.toByteArray());
      throw new IOException(
          "IOException raised while logging the message. \nCause :" + stack);

    }

  }

  /**
   * default constructor
   */
  public SimpleLog() {
    log = this;
  }

  /**
   * constructor that takes the PrintSteam instance as an argument
   * @param stream PrintStream
   */
  public SimpleLog(PrintStream stream) {
    this.stream = stream;
    log = this;

  }

  /**
   * File will be used as destination stream to print the log messages
   * @param f File

   */
  public SimpleLog(File f)  {
    try
    {
      FileOutputStream out = new FileOutputStream(f);

      stream = new PrintStream(out);

    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }log = this;

  }

  /**
   * OutputStream will be used as destination stream for printing the log messages
   * @param out OutputStream

   */
  public SimpleLog(OutputStream out)  {
    try
    {
      stream = new PrintStream(out);
    }catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }

  /**
   * prints the message to destination stream. Deafult is Standard error device i.e. System.err
   *
   * @param message String
   */
  public void log(String message)  {
    try
    {
      if (isDebug) {
        raiseError();
        stream.println(message);
      }
    }
    catch(Exception ex)
    {
      stream=System.err;
      ex.printStackTrace();
    }
  }

  /**
   * prints the message along with exception stack tace to destination stream. Deafult is Standard error device i.e. System.err
   *
   * @param message String
   * @param error Throwable
   */
  public void log(String message, Throwable error)  {
    try
    {
      if (isDebug) {

        ByteArrayOutputStream out = new ByteArrayOutputStream(10000);
        error.printStackTrace(new PrintStream(out));
        raiseError();
        String stack = new String(out.toByteArray());
        stream.println(message);
        stream.println(stack);
      }
    }catch(Exception ex)
    {
      stream=System.err;
      ex.printStackTrace();
    }

  }

  /**
   * setDebug
   * sets whether to print the log values or not
   * @param debug boolean
   */
  public void setDebug(boolean debug) {
    this.isDebug = debug;
  }

  /**
   * isDebugEnabled
   * checks whether debug is enabled or not
   * @return boolean
   */
  public boolean isDebugEnabled() {
    return isDebug;
  }

  /**
   * log
   * prints the exception stack trace to destination stream. Deafult is Standard error device i.e. System.err
   * @param error Throwable
   */
  public void log(Throwable error)  {
    try
    {
      if (isDebug) {

        ByteArrayOutputStream out = new ByteArrayOutputStream(10000);
        error.printStackTrace(new PrintStream(out));
        raiseError();
        String stack = new String(out.toByteArray());
        stream.println(stack);
      }
    }catch(Exception ex)
    {
      ex.printStackTrace();
    }

  }

  /**
   * gets the new Instance of logger
   * @return Log
   */
  public static Log getInstance() {
    if (log == null) {
      log = new SimpleLog();
    }
    return log;
  }

  /**
   * gets the new Instance of logger for this PrintStream object
   * @param stream PrintStream
   * @return Log
   */
  public static Log getInstance(PrintStream stream) {
    log = new SimpleLog(stream);
    return log;
  }

  /**
   * gets the new Instance of logger for this File object
   * @param file File
   * @return Log
   */
  public static Log getInstance(File file)  {
    try
    {
      log = new SimpleLog(stream);
    }
    catch(Exception ex)
    {
      log=null;
    }
    return log;
  }

  /**
   * gets the new Instance of logger for this OutputStream object
   * @param out OutputStream

   * @return Log
   */
  public static Log getInstance(OutputStream out)  {
    try
    {
      log = new SimpleLog(out);
      return log;
    }catch(Exception ex_)
    {
      ex_.printStackTrace();
    }
    return null;

  }

  /**
   * gets the current stream of the logger
   * @return PrintStream
   */
  public static PrintStream stream() {
    return stream;
  }

  /**
   * Test program
   * @param s String[]
   * @throws Exception
   */
  public static void main(String s[]) throws Exception {
    FileOutputStream fout = new FileOutputStream("c:/a11111.log");
    SimpleLog log = new SimpleLog(fout);
    int i1 = 0;
    for (; i1 < 1000; ) {
      try {
        i1 += 1;
        int i = 1 / 0;

      }
      catch (Exception ex) {
        log.log(ex);
      }
    }

  }

  /**
   * warn
   *
   * @param message String
   */
  public void warn(String message) {
    Exception ex = new Exception(message);
    ex.printStackTrace();
    try
    {
      log(message);
    }
    catch(Exception ex1)
    {
      ex1.printStackTrace();
    }
  }

  /**
   * fatal
   *
   * @param message String
   */
  public void fatal(String message) {
    Exception ex = new Exception(message);
    ex.printStackTrace();
    try {
      log(message);
    }
    catch (Exception ex1) {
      ex1.printStackTrace();
    }
    System.exit(1);
  }

  /**
   * warn
   *
   * @param message Throwable
   */
  public void warn(Throwable message) {
    message.printStackTrace();
    try {
      log(message);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * fatal
   *
   * @param message Throwable
   */
  public void fatal(Throwable message) {
    try
    {
      message.printStackTrace();
      log(message);
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
    System.exit(1);
  }

  /**
   * warn
   *
   * @param message String
   * @param error Throwable
   */
  public void warn(String message, Throwable error) {
    System.err.println(message);
    error.printStackTrace();
    try {
      log(message, error);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  /**
   * fatal
   *
   * @param message String
   * @param error Throwable
   */
  public void fatal(String message, Throwable error) {
    System.err.println(message);
    error.printStackTrace();
    try {
      log(message, error);
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    System.exit(1);

  }

}


Example goes here
_____________________
<%
   SimpleLog log=new SimpleLog(new FileOutputStream("c:\\a.txt"));
   log.log("Your results");
 
%>
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
Riverbed Technology's webinar discusses networking for the cloud era with simplified SD-WAN cloud connectivity.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question