开发者

HttpServletRequest for jsf

i have a jsp site where i get an attribute using request.getAttribute. i'm looking for a way to get that attribute in jsf (running开发者_JAVA技巧 in the same tomcat). this is what i found on the internet:

HttpServletRequest requestObj = (HttpServletRequest)         
FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String value =  (String) requestObj.getAttribute("property");

but the result (value) stays null.

what could possible reasons be that it works in the first case but not in the second?


what could possible reasons be that it works in the first case but not in the second?

If the attribute is not there anymore, then it simply concerns a completely different request. Probably you have sent a redirect, or the webbrowser has sent a new request, etc. The real answer to your problem is hard to give since you didn't elaborate anything about the functional requirement in your question. You just posted some code snippet and said "why doesn't this work?".

At any way, the request attributes live as long as the request/response itself and they are not retained in subsequent requests. For that you'd rather like to store it as a session attribute instead, or to pass as a request parameter in case of a redirect. Or, when you're already on JSF 2.0, storing it as a property of a view scoped bean should also do for the case the bean is to be accessed from the same view subsequently.

To learn more about the lifecycle of the HTTP servlet request/response, I'd suggest to get yourself through this answer.


Unrelated to the concrete problem: whenever you need to haul the raw Servlet API from under the JSF covers, then you should really take a break and think twice if you're really doing things the right way (read: without the need to fall back to the raw javax.servlet API). Shouldn't it better be a fullworthy JSF managed bean, for example?

At any way, request attributes are also accessible by ExternalContext#getRequestMap().

Map<String, Object> requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
String value = (String) requestMap.get("property");

No need to utilize the raw Servlet API.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜