1

I saw that ExternalContext in JSF 2.0 has a method setResponseStatus, how can I do that using JSF 1.2? Thanks in advance!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107

1 Answers1

2

In JSF 1.x, you need to get the HttpServletResponse by ExternalContext#getResponse() and then invoke setStatus() on it. This is also basically what the new JSF 2.0 method is doing under the covers.

HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks! And is it safe to cast the return of `getResponse()` to an `HttpServletResponse`? Why the need to cast? – Elias Dorneles Dec 02 '11 at 16:01
  • Yes it is safe if you run JSF on top of a JSP/Servlet webapp. JSF can however also be used on top of a Portlet webapp, the cast would then fail. See also the javadoc of `ExternalContext`, it mentions differences for servlets and portlets in behaviour of the existing delegate methods: http://docs.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html – BalusC Dec 02 '11 at 16:02