0
public void dispatch1(String path) {
        HttpServletRequest request = (HttpServletRequest).getRequest();
        boolean dontRender = false;
        if (request.getSession().getAttribute("dontRender") != null) {
            dontRender = (boolean) request.getSession().getAttribute("dontRender");
        }
        RequestDispatcher requestDispatcher = request.getRequestDispatcher(path);
        HttpServletResponse response = (HttpServletResponse) getResponse();
        ServletResponse response = response;

        FacesContext facesContext = FacesContext.getCurrentInstance();

        try {
            if (response.isCommitted()) {
                requestDispatcher.include(request, response);
            } else {
                if (!dontRender) {

                    requestDispatcher.forward(request, response);

                } else {
                    // Dont render response
                    // create a new httpservlet request with savebutton clicked and send to faces
                    // servlet
                    Runnable runner = new Runnable() {
                        public void run() {
                            HttpServletRequest req = (HttpServletRequest).getRequest();
                            HttpServletResponse resp = (HttpServletResponse) getResponse();
                            req.setAttribute("Save", "Submit Q");
                            req.setAttribute("action", "test2.jsf");

                            try {
                                req.getRequestDispatcher("test1.jsf").forward(req, resp);

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            } catch (ServletException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    };
                    Thread thread = new Thread(runner);
                    thread.start();
                }

            }
        }

        catch (ServletException e) {
            e.printStackTrace();
            throw new FacesException(e);
        }
    }
test1.jsp
    <input name="Save" id="Save"  type="submit" value="Submit Q">

If dontRender is set, I dont want to send the response to client, but I want to create a HttpServletRequest message that matches whatever the browser would normally send when the user clicks save button in test1.jsp and then send that to the Faces servlet. Not sure if this is correct approach, as I am getting null pointer exception?

  • What is your functional requirement? I have a sort of deja-vu https://stackoverflow.com/questions/52647470/myfaces-renderresponse-skip-browser.. You still seem te be solving something in a way that is not the right one. http://www.xyproblem.info – Kukeltje Oct 19 '18 at 14:22
  • The client has a requirement that the original logic be preserved. So the rendered jsp must NOT be seen in the browser( which works when dontRender is set), instead a "save" must be triggered and sent to myfaces lifecycle. What is not working is sending a HTTPServletRequest –  Oct 19 '18 at 16:57
  • Like stated before, then just make sure you send the user to the new page instead of faking the request. Lots of problems with that. Just send an HTTP redirect 302 to the page maybe with additional request params and on loading that new page, do something like getting request params if need (or temporarily set something in session scope) and retrieve like https://stackoverflow.com/questions/10724428/how-do-i-process-get-query-string-url-parameters-in-backing-bean-on-page-load. **THAT** is the JSF way of doing things (and btw for other web frameworks as well. Your attempt is 'strange/uncommon' – Kukeltje Oct 20 '18 at 10:04

0 Answers0