Solved

Servlet: Servlet Output Stream

Posted on 2002-03-27
12
1,632 Views
Last Modified: 2013-11-24
I am running an java web appilcation build on WebLogic Server 6.0 and implementing servlet specification v2.2:

In order to output html i used the printwriter in my servlets:

    PrintWriter out = res.getWriter();
    res.setContentType("text/html");

However a BEA engineer pointed out a memory issue possibly being caused by using the PrintWriter instead of the ServletOutputStream as follows:

    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");


However, a general error catching block at the end of my servlet, namely:

catch (Exception e) {
      out.println("<PRE>" + e.getMessage() + "<BR>");
      e.printStackTrace(out);
    }

chokes on the ServletOutputStream as follows:

Error #: 300 : method printStackTrace(javax.servlet.ServletOutputStream) not found in class java.lang.Exception at line 99, column 9

Does anyone know if there is an equivalent method for the ServletOutputStream that i could use to catch general exceptions?
0
Comment
Question by:yfud
  • 6
  • 4
  • 2
12 Comments
 
LVL 2

Expert Comment

by:userquin
ID: 6899692
Hi:

In a servlet out = response.getOutputStream().

Bye.

J.
0
 

Author Comment

by:yfud
ID: 6899741
J, thanks.

thats why i'm switching them, but i'm wondering about catching general throwables with .printStackTrace. It takes a PrintWriter... so is there an equivalent method if i want to output the exceptions or errors?
0
 
LVL 2

Expert Comment

by:userquin
ID: 6899745
Hi:

Sorry, try:

e.printStackTrace(new PrintWriter(response.getOutputStream()));

or

e.printStackTrace(response.getWriter());

Bye.

J.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 200 total points
ID: 6899875
There should be no memory problems with PrintWriter, especially if you remember to call out.close() when you are done with it.  Also, call res.setContentType() BEFORE you get the writer.

When you call response.getWriter(), this is what happens (in your HttpSAervletResopnse method):

public PrintWriter getWriter() {
  return new PrintWriter (new BufferedWriter(new OutputStreamWriter(getOutputStream, charset)));
}

Whether you call getOutputStream() or getWriter(), you are using the same stream: the Writer is just a thin wrapper over the outputstream.

Again, if you are losing memory and you can narrow it down to the PrintWriter, be sure you are calling out.close() when you are done with it.

BTW - Both of userguin's suggestions will cause an IllegalState exception to be thrown if because you can only get stream once (assuming you have already used the outputstream).
0
 

Author Comment

by:yfud
ID: 6900089
thanks m_onkey_boy:

you were right about the memory i think i was looking at the problem wrong. below is the actual error message. From the thread dumps I can see a lot of following messages:

"ExecuteThread: '2' for queue: 'default'" daemon prio=5 tid=0x378678 nid=0xf runnable [0xe7680000..0xe76819e0]
        at weblogic.servlet.internal.ChunkUtils.getEnd(ChunkUtils.java:23)
        at weblogic.servlet.internal.ChunkUtils.str2chunk(ChunkUtils.java:42)
        at weblogic.servlet.internal.ResponseHeaders.writeHeaders(ResponseHeaders.java:335)
        at weblogic.servlet.internal.ServletResponseImpl.writeHeaders(ServletResponseImpl.java:718)
        at weblogic.servlet.internal.ServletOutputStreamImpl.flush(ServletOutputStreamImpl.java:130)
        at weblogic.servlet.internal.ServletOutputStreamImpl.flushWithCheck(ServletOutputStreamImpl.java:412)
        at weblogic.servlet.internal.ServletOutputStreamImpl.checkForFlush(ServletOutputStreamImpl.java:548)
        at weblogic.servlet.internal.ServletOutputStreamImpl.write(ServletOutputStreamImpl.java:210)
        at weblogic.servlet.internal.WLOutputStreamWriter.write(WLOutputStreamWriter.java:99)
        at weblogic.servlet.internal.WLOutputStreamWriter.write(WLOutputStreamWriter.java:111)
        at java.io.Writer.write(Writer.java:129)
        at java.io.PrintWriter.newLine(PrintWriter.java:236)
        at java.io.PrintWriter.println(PrintWriter.java:387)
        at java.io.PrintWriter.println(PrintWriter.java:498)
        at bmw.servlet.personalRide.List.doGet(List.java:160)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
        at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:213)
        at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:1265)
        at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:1641)
        at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:137)
        at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)

I'd like to try putting in the proper ServletOutputStream, but I don't know how to duplicate the printStackTrace without using the printwriter.
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6900127
Give me a few minutes to look over the dump, but in the meantime - it is better to call response.getWriter() for text/html output.  The ServletOutputStream is meant for situations where you want to transmit binary data.
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6900142
In other words, getWriter IS the proper method, and it will work with e.printStackTrace(out)
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6900153
Were you setting any response headers manually?  It looks like there was a problem with a header string, possibly a '\n' in a header?
0
 

Author Comment

by:yfud
ID: 6900324
The only use of the response in the servlets are

res.setContentType("text/HTML");
res.encodeURL
and
res.sendRedirect

0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6900399
Try removing the res.encodeURL call.
0
 
LVL 4

Expert Comment

by:m_onkey_boy
ID: 6900402
Also, lower-case html in text/html
0
 

Author Comment

by:yfud
ID: 6943042
thanks for the help m_onkeyboy... i'm still working on completely solving the problem but apologize for taking so long to award you the points and appreciate the help

yfud
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 45
login jsp example 24 62
Tomcat: Unable to run tomcat service. 2 23
Notify sent to other threads in Java 9 33
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

789 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