当我尝试使用JSP更改响应头时收到错误

时间:2011-06-27 16:44:03

标签: jsp jstl

我添加了这一行来更改JSP <c:set target="${pageContext.response}" property="Access-Control-Allow-Origin" value="*"/>中的响应头..我收到这样的错误..

    HTTP ERROR 500

Problem accessing /. Reason:

    Invalid property in &lt;set&gt;:  "Access-Control-Allow-Origin"

Caused by:

javax.servlet.jsp.JspTagException: Invalid property in &lt;set&gt;:  "Access-Control-Allow-Origin"
    at org.apache.taglibs.standard.tag.common.core.SetSupport.doEndTag(SetSupport.java:202)
    at org.apache.jsp.WEB_002dINF.pages.search_jsp._jspx_meth_c_set_2(org.apache.jsp.WEB_002dINF.pages.search_jsp:397)
    at org.apache.jsp.WEB_002dINF.pages.search_jsp._jspService(org.apache.jsp.WEB_002dINF.pages.search_jsp:112)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:486)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
    at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
    at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:381)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.inject.servlet.FilterChainInvocation.doFilter(FilterChainInvocation.java:67)
    at com.google.inject.servlet.ManagedFilterPipeline.dispatch(ManagedFilterPipeline.java:122)
    at com.google.inject.servlet.GuiceFilter.doFilter(GuiceFilter.java:110)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
    at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:536)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:915)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:539)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:405)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

这是我在下面添加该行的jsp代码。

<!doctype html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="search" uri="/kit" %>
<%@ taglib prefix="solr" uri="/kit/solr" %>
<%@ taglib prefix="workflow" uri="/kit/workflow" %>
<%@ taglib prefix="security" uri="/kit/security" %>
<%@ taglib prefix="widget" tagdir="/WEB-INF/tags/widgets" %>
<%@ taglib prefix="visualization" tagdir="/WEB-INF/tags/visualizations" %>
<%@ taglib prefix="client" tagdir="/WEB-INF/tags/client" %>
<%@ page contentType="text/html" pageEncoding="UTF-8" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<search:twigkit mode="PRODUCTION">
    <solr:platform host="http://stg.xyz.com:8800/lucene/master/" 
        defaultQuery="*:*" defaultFacets="docType,imType,pLine,originator,technology"/>
    <search:query var="query" parameters="*" 
        fields="*" />
    <search:response var="response" platform="${platform}" query="${query}">
        <workflow:processor name="kit.linguistics.processors" title="title" summary="text" />   
    </search:response>
</search:twigkit>

<c:set var="res" value="http://stg.xyz.com:8800/dps/"/>
<c:set var="dpsURL" value="http://stg.xyz.com:8800/dps"/>
<c:set target="${pageContext.response}" property="Access-Control-Allow-Origin" value="*"/>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <title>Search :: ${query.value.display}</title>
    <client:head/>
    <script src="${res}dpsjsclient/dps.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="${res}dpsjsclient/dps-main.css" type="text/css" />

</head>

1 个答案:

答案 0 :(得分:1)

HttpServletResponse确实没有setAccess-Control-Allow-Origin()方法。即使它有一个,当你基本上在响应体内时设置/更改标题也不总是保证工作(JSP基本上是响应体的一部分!)。您只会冒险在服务器日志中看到IllegalStateException: response already committed

您希望在提交响应之前设置响应标头。最好的地方是使用filter。创建一个implements Filter的类,并使用doFilter()方法完成工作。

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin", "*");
    chain.doFilter(req, res);
}

将此过滤器映射到所需的URL模式。