[opencms-dev] Setting manual Content-Type header from a jsp

Mike Dikan dikeman at gmail.com
Tue Jun 16 17:23:51 CEST 2009


I found a solution, if anyone encounters the same problem.  After  
looking through the source, It seems like opencms expects you to use  
CmsJspBean to modify SOME PARTS of the response object.  Stephan was  
right, you can use the top response object to add headers like  
location or content-type, but you can only modify the status code by  
using CmsJspBean.setStatus, as the flex cache seems to ignore the  
status headers set on the response object.  Alternatively, you can  
just set the cache property to 'bypass' on the jsp or folder that  
contains your jsp(s).

So to get at the methods on CmsJspBean, this was the tag I used:

<jsp:useBean id="cms" class="org.opencms.jsp.CmsJspActionElement"></ 
jsp:useBean>
<%
         cms.init(pageContext, request, response);
%>

And then it is as easy as calling setStatus or setContentType:

<%
	cms.setStatus(301);
         cms.setContentType("text/javascript");
%>

If you need to add arbitrary headers into your response, like  
location, or content-type headers, use the top response object as  
suggested by Stephan:

<%@page import="org.opencms.flex.*" %>
<%
	HttpServletResponse topLevelResponse =  
CmsFlexController.getController(request).getTopResponse();
	topLevelResponse.setHeader("Location", "http://www.google.com/" );
         topLevelResponse.setHeader("Content-Type", "text/javascript");
%>

So, to send a 301 redirect, use the setStatus method from CmsJspBean,  
and the setHeader method from the top response object.  I didn't  
mention in my earlier post that I was trying to find a way to  
accomplish both of these tasks (for different pages).

-Mike

On Jun 15, 2009, at 6:19 PM, Mike Dikan wrote:

> Sorry, just found the static  
> CmsFlexController.getController(request) method to answer the first  
> part of my last email.
>
> As for the second part, I attempted this:
>
> HttpServletResponse topLevelResponse =  
> CmsFlexController.getController(request).getTopResponse();
>
> But calls to modify the headers on topLevelResponse don't seem to be  
> taking effect.
>
> Any pointers?
>
> -Mike
>
> On Jun 15, 2009, at 5:01 PM, Stephan Hartmann wrote:
>
>> Hi Mike,
>>
>> the reason why your attempts for setting the content type by either
>> using the response object or the contentType attribute in the page
>> directive is that the JSP is usually embedded by using the include
>> dispatcher of the request. From within an included webapp resource
>> (either servlet or JSP or filter) there is no way to modify response
>> headers, as defined in the servlet spec.
>> For your case, it would be sufficent to set the cache property of  
>> your
>> JSP to "bypass" which will lead to a forward instead of an include,  
>> what
>> will give you control over your response headers as well. Note that  
>> you
>> will lose flex cache functionality with that property set.
>> Another way to set response headers from an included JSP would be  
>> to get
>> hold of the top response object through the CmsFlexController  
>> object of
>> your request.
>>
>> Regards,
>> Stephan
>>
>> Mike Dikan schrieb:
>>> I have need for a jsp that spits out dynamic javascript called
>>> something like javascript_dynamic_variable_init.jsp.  In opencms, if
>>> you have a file on the vfs that has extension .js, the content-type
>>> header that is returned is of type 'application/x-javascript', and  
>>> if
>>> you have a file that ends in extension .jsp, the content-type header
>>> is 'text/html'.  I need to change the content-type header returned  
>>> for
>>> a file ending in .jsp to the .js header, but any attempts to do so
>>> either by using the <%@page contentType="" %> tag, or operating on  
>>> the
>>> response directly is clobbered by opencms.  Does anyone know how  
>>> to do
>>> this in opencms 7.0.5?
>>>
>>> And to head off any questions, i can't rename the file to .js,  
>>> because
>>> the file needs to be brought in as a jsp opencms 'type' via the
>>> synchronize functionality, which will only create types based on  
>>> file
>>> extensions, as far as I know.
>>>
>>> Thanks!
>>> -Mike
>>>
>>> _______________________________________________
>>> This mail is sent to you from the opencms-dev mailing list
>>> To change your list options, or to unsubscribe from the list,  
>>> please visit
>>> http://lists.opencms.org/mailman/listinfo/opencms-dev
>>>
>>>
>>
>>
>> _______________________________________________
>> This mail is sent to you from the opencms-dev mailing list
>> To change your list options, or to unsubscribe from the list,  
>> please visit
>> http://lists.opencms.org/mailman/listinfo/opencms-dev
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20090616/2e830a80/attachment.htm>


More information about the opencms-dev mailing list