Search code examples
javajspjnlp

"getOutputStream() has already been called" while using jnlp tags inside jsp page


I'm trying to go to a jnlp file from a jsp page to get the fingerprints of users registered, the file is as below..

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String protocol = request.getScheme();
    String domain = request.getServerName();
    String port = Integer.toString(request.getServerPort());
    String a = protocol + "://" + domain + ":" + port + path;
    path = protocol + "://" + domain + ":" + port + path + "/";
%>
<%@page contentType="application/x-java-jnlp-file" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="<%=path%>" href="">
    <information>
        <title>Mytitle</title>
        <vendor>Myvendor</vendor>
    </information>
    <security><all-permissions/></security>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+"  href="http://java.sun.com/products/auto/j2se"/>
        <jar href="jnlp/FingerPrintApplet.jar" main="true"/>
    </resources>
    <application-desc main-class="ui.InvitationApplet">
        <argument>${firstName}</argument>
        <argument>${lastName}</argument>
        <argument>${loginId}</argument>
        <argument>${roleId}</argument>
        <argument>${urlCode}</argument>
        <argument>${mainRecordOfficer}</argument>
        <argument>${middleName}</argument>
        <argument>${employeeId}</argument>
        <argument>${createdBy}</argument>
        <argument><%=a%></argument>
        <argument>${invitedUnder}</argument>
        <argument>${login_type}</argument>
    </application-desc>
</jnlp>

But every time jsp page is hit, jnlp gets downloaded but I get exception

java.lang.IllegalStateException: getOutputStream() has already been called for this response

I also came to know that this exception is due to "blank spaces" for new lines in the compiled jsp file, for that I also added below line at the top of jsp

<%@ page trimDirectiveWhitespaces="true" %>

But still I get same issue, and then I also did some stupid things like removing blank spaces in jsp file manually like

<%@ page .....%><%@ page.....%>

But still same exception.


Solution

  • This occurs because, white spaces are preserved by default in jsp page, which may get added to output as a line, so the above explained exception comes, to get rid of that just add following lines in web.xml and this should be fixed.

    <jsp-config>
      <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
      </jsp-property-group>
    </jsp-config>