Solved

Servlet: Servlet Output Stream

Posted on 2002-03-27
12
1,625 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the latest versions eclipse neon 2 139
@SBGen Method 3 36
servlet  URL Rewriting 1 26
Why my table column Id is not passed to java object? 4 24
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

911 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

25 Experts available now in Live!

Get 1:1 Help Now