2

I am trying to import javascript from a file into my jsp to be used as an in line script:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script>
    <c:import url="/path/to/file.js" />
</script>

The above code works 99% of the time but I see a few errors like this in my tomcat logs:

Jun 20, 2013 1:25:33 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspTagException: 304 /path/to/file.js
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.acquireString(ImportSupport.java:329)
    at org.apache.taglibs.standard.tag.common.core.ImportSupport.doEndTag(ImportSupport.java:171)
    at org.apache.jsp.WEB_002dINF.jsp.common.head.scripts_jsp._jspx_meth_c_005fimport_005f0(scripts_jsp.java:182)
    at org.apache.jsp.WEB_002dINF.jsp.common.head.scripts_jsp._jspService(scripts_jsp.java:85)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:605)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:544)
    at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
    at org.apache.jsp.WEB_002dINF.jsp.game_jsp._jspService(game_jsp.java:181)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    .......

I found the code that throws the above exception in ImportSupport.java:

// disallow inappropriate response codes per JSTL spec
if (irw.getStatus() < 200 || irw.getStatus() > 299) {
    throw new JspTagException(irw.getStatus() + " " + stripSession(targetUrl));
}

So it looks like the servlet response code is 304.

The question is why? Is this a bug or am I missing something?

Update:

The problem seems to only occur if the incoming request has the If-Modified-Since header on

Update 2:

I solved the problem by removing the If-Modified-Since header from every request except for static files.

panos2point0
  • 170
  • 2
  • 8

2 Answers2

1

I'm using Spring MVC's ResourceHttpRequestHandler (configured with mvc:resources) to serve my static resources from /resources/* and it returns 304 on incoming requests that use the If-Modified-Since header. So when I include such a static resource with c:import and the incoming request has an If-Modified-Since header I get the javax.servlet.jsp.JspTagException: 304 error.

I need dynamic including so @include isn't an option, and jsp:include produces an IllegalStateException when trying to include a URL handled by ResourceHttpRequestHandler. So I was left with writing my own include that reads from the file and writes it out in the JSP:

<%@ page import="org.springframework.web.context.support.ServletContextResourceLoader, org.springframework.core.io.Resource" %>
<%@ page import="org.apache.commons.io.IOUtils" %>

<%
    String path = "/resources/fileName.html";
    ServletContextResourceLoader loader = new ServletContextResourceLoader(getServletConfig().getServletContext());
    Resource resource = loader.getResource(path);
    IOUtils.copy(contentResource.getInputStream(), pageContext.getOut());
 %>
tekumara
  • 8,357
  • 10
  • 57
  • 69
0

Try by adding one "/" at the end url e.g Yours -- <c:import url="/path/to/file.js"/> try with -- <c:import url="/path/to/file.js/"/>