Solved

I want to output to notepad

Posted on 2004-09-23
2
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

724 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