In JSP, how can I dynamically set contentType?
I have a very simple JSP that looks like this:
<%@ page contentType="application/json" %>${actionBean.response} actionBean.response returns a String . Sometimes this line is json, which has a contentType for "application / json", but sometimes this jsonp line is of type contentType "application / javascript". But I canβt figure out how to dynamically set the contentType value.
- I tried using
<c:choose>around contentType, but it gives me a message stating that I cannot set contentType twice. - I tried using EL for the attribute value, but it does not expand.
Is there a way to dynamically set this value?
You can try using scripts (not perfect, but I'm not sure there is another way), for example:
<% if (actionBean.isJson()) { response.setContentType("application/json"); } else if (actionBean.isJsonp()) { response.setContentType("application/javascript"); } %> Edit: And since Joop mentions in the comments, make sure you don't set contentType using the @page directive.
I do not think jsp is for this. You would capture the JSON response in a javascript function without having to declare a content type.
I also never saw any jsp page turn into a javascript file.
You can use <s:property name="something" escapeHtml="false" escapeJavascript="false"/> for both.