1

I am getting split output from my JSP page, with writer.write() output first and second the JSP with tags omittted.

I have the following simple JSP page

<%@ page import="com.AAA.SiteTemplate.RenderPageInterface" %>
<%@ page import="java.io.Writer" %>
<%@ page import="java.io.IOException" %>
<%@ page import="com.NatureRelaxation.SiteTemplate.RenderPageInterfaceNull" %><%--
  Created by IntelliJ IDEA.
  User:
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%
        RenderPageInterface obj = (RenderPageInterface) request.getAttribute("renderPageInterface");
        if (obj == null){
            obj = new RenderPageInterfaceNull();
        }
    %>
    <title><% response.getWriter().write(obj.getTitle()); %></title>
    <style type="text/css">
    </style>
</head>
<body>
<%
obj.renderHTML(response.getWriter());
%>
</body>
</html>

and I include it via this small function:

public static void doRequest(HttpServletRequest req, HttpServletResponse res, RenderPageInterface renderPageInterface) throws ServletException, IOException{
    req.setAttribute("renderPageInterface", renderPageInterface);
    RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/templates/header.jsp");
    rd.include(req, res);
}

The response that I am getting now is

My page titleMypagebody....





<html>
<head>

    <title></title>

etc. There are 4 newlines in the middle. I'm thinking it must be a compiler error, but clearing caches (IntelliJ) and restarting, redeploying to Tomcat 8 doesn't fix it. All worked as normal until server was restarted, but nothing fixes it now. I even run the debugger on the JSP and all goes in order.

Update:

It runs fine without this tag "<% response.getWriter().write(obj.getTitle()); %>", but when the tag is there, both the body and the title get written at the top and the template at the bottom. getTitle() returns a String.

user1122069
  • 1,767
  • 1
  • 24
  • 52

1 Answers1

1

I figured it out fast enough, but it was quite tricky. "response.getWriter()" and JSP's "out" variable (which I did not know about), are not the same thing.

It is by coincidence that my code previously worked.

I also had to include jsp.api.jar before I could use the "out" (JSPWriter) variable.

Relevant info here:

Difference between JspWriter and PrintWriter in Java EE?

what is difference between JspWriter and PrintWriter?

Community
  • 1
  • 1
user1122069
  • 1,767
  • 1
  • 24
  • 52