Pregunta

¿hay alguna equivalente a FacesContext, pero en el entorno de servlet?

Tengo algunas DAOSessionManager esa transacción mangos a mi base de datos. Puedo utilizar el FacesContext para identificar la solicitud HTTP actual cuando la página actual se escribe usando JSF, pero ¿qué pasa con los servlets?

No se puede encontrar ninguna manera de obtener el contexto servlet actual, o httpRequest ...

Gracias.

PS: Sí, tener una referencia a FacesContext de mi capa DAO es una lástima, pero eso es un comienzo.

¿Fue útil?

Solución

It's the ServletContext. It's available inside servlet classes by the inherited getServletContext() method.

protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    ServletContext context = getServletContext();
    // ...
}

The major difference with FacesContext is that the ServletContext isn't ThreadLocal, so you cannot obtain it "statically" from the current thread like FacesContext#getCurrentInstance() does. You really need to pass the ServletContext reference around into the DAO methods wherever you need it:

someDAO.doSomething(getServletContext());

Or better yet, to avoid tight coupling, just extract the desired information from it and pass it:

Object interestingData = getServletContext().getAttribute("interestingData");
someDAO.doSomething(interestingData);

Otros consejos

ServletContext servletContext = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top