Reputation: 111
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:
doPost
method (works - I get the data in the doPost
method).Notes:
doPost
method correctly.doPost
method does a request.getRequestDispatcher("/faces/xxx.jsp").forward(request, response);
(The request is correctly loaded with the data). 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.
Upvotes: 0
Views: 2778
Reputation: 1248
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 }
Upvotes: 0
Reputation: 1109132
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).
Upvotes: 1
Reputation: 15240
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();
Upvotes: 0