[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

I want to output to notepad

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
skychan
Asked:
skychan
1 Solution
 
MogalManicCommented:
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
 
sudhakar_koundinyaCommented:
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now