Solved

Servlet: Servlet Output Stream

Posted on 2002-03-27
12
1,620 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
Comment Utility
Hi:

In a servlet out = response.getOutputStream().

Bye.

J.
0
 

Author Comment

by:yfud
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:m_onkey_boy
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Try removing the res.encodeURL call.
0
 
LVL 4

Expert Comment

by:m_onkey_boy
Comment Utility
Also, lower-case html in text/html
0
 

Author Comment

by:yfud
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

762 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

9 Experts available now in Live!

Get 1:1 Help Now