Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Servlet: Servlet Output Stream

Posted on 2002-03-27
12
Medium Priority
?
1,660 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Accepted Solution

by:
m_onkey_boy earned 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 …
This video teaches viewers about errors in exception handling.
Suggested Courses

916 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