Passing a JSP Stream to a Method

I created the MyClass class, which intends to display a large amount of text in the JSP. Instead of the MyClass object returning a string that will be displayed on the page, I thought it would be better to use the MyClass object to use the output stream of the page. Is this a good / affordable idea?

When testing possible ways to do this ...

They display text, but it appears in front of the page body:

response.getWriter().append("test1");
response.getWriter().println("test2");
response.getWriter().write("test3");

These errors tell me that the output stream has already received:

response.getOutputStream().println("test4");
+3
source share
2 answers

response.getWriter() , , JSP. , JSP, out JSP. , JSP response.getWriter(). , response.getWriter() JSP.

response.getWriter() response.getOutputStream(). out JSP - JspWriter, response.getWriter(), response.getOutputStream() .

JSP:

<%
  new MyClass().writeToWriter(out);
%>

MyClass:

public void writeToWriter(Writer w) {
    w.println("My data appended to correct writer");
}
+9

, . JSP .

+1

Source: https://habr.com/ru/post/1703363/


All Articles