Solved

Servlet: Servlet Output Stream

Posted on 2002-03-27
12
1,637 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
[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
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java exception bubble up 2 26
sql import cannot be resolved jsp 3 117
Why is enum singleton a better approach than static factory 3 67
learn programming 8 95
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

751 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