Solved

I want to output to notepad

Posted on 2004-09-23
2
210 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Multiple Images Upload [JSP Web-application] 3 49
instanceof  operator in java 26 112
throw exception 21 75
collection output issue 9 89
When using a search centre, I'm going to show you how to configure Sharepoint's search to only return results from the current site collection. Very useful when using Office 365 with multiple site collections.
A hard and fast method for reducing Active Directory Administrators members.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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