Solved

I want to output to notepad

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Spring Tuorial 1 107
library class in java 1 89
using if condition without JSTL 2 101
'exit' is not valid keyword in java 14 134
A procedure for exporting installed hotfix details of remote computers using powershell
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now