tags:

views:

67

answers:

3

I have a JSF/JSP page, a managed bean, and a separate file/class that extends HttpServlet with the doPost method.

I have a separate test program that sends a xml object to the doPost method via a HttpURLConnection.

What is wanted:

  1. Start the JSP page with temporary data (works).
  2. At a later time, send a new xml object to the doPost method (works - I get the data in the doPost method).
  3. Display the new data in the JSP (does not work).

Notes:

  1. When the data comes in it hits the doPost method correctly.
  2. The doPost method does a request.getRequestDispatcher("/faces/xxx.jsp").forward(request, response); (The request is correctly loaded with the data).
  3. The initial method in the JSP managed bean is hit correctly.
  4. I cannot use any Javascript in this. EL is questionable.

Problem: I have not found a way (probably really obvious) to get the request object to the JSP managed bean from the servlet.

The question: How does the JSP retrieve the request object and pass the request object to the managed bean? It there an end to end example anywhere? - have not found one.

A: 

Are you just not sure how to get the request from inside the managed bean? If so, try this from inside an action method in your managed bean:

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletRequest request = (HttpServletRequest)externalContext.getRequest();
Jim Tough
My apologies, the question was not clear. I have a JSP/JSL page with a managed bean associated with it.The page is kicked off by request.getRequestDispatcher("/faces/xxx.jsp").forward(request, response); from another source.I am asking for a way with JSTL, to retrieve the request (or an object in the request), and send it to the managed bean.Note:Assume the request key="chart" and the object=privateChartAssume the backing bean (a POJO) is "BeanA" and the method is "getChart"Currently using JSTL 1.2
+1  A: 

I have a JSF/JSP page, a managed bean, and a separate file/class that extends HttpServlet with the doPost method.

Stop using Servlets in JSF. The servlet has apparently some functionality which you'd like to reuse somewhere else. Simply refactor it into a standalone and reuseable class/method which you in turn can just import/invoke in both the servlet class and the JSF managed bean.


As to your actual problem: assuming that you actually meant "request attribute" when you said "request object", which seem to be unreachable in the JSF page, then this can have only two reasons: either the attribute name is wrong, or it is not the bean/pojo instance you expect it to be (which can happen if you create multiple instances).

BalusC
A: 

Assuming that you are using JSF, something very simple you could do from your managed bean is just to pass the data as an attribute in the request and then dispatch to the desired servlet:

FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
request.setAttribute("myData",myData);
context.getExternalContext().dispatch("/MyServlet");
context.responseComplete();

In your servlet:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String myData = (String)req.getAttribute("myData"); 
    //Do something with myData  }
Abel Morelos