javajsfservletsxhtml

render xhtml file from servlet


I wonder if there is a way to load, interpret and render a .xhtml file from a servlet. What I want is to have a xhtml file witch contain for example a custom row within a list, to be loaded from a servlet or a bean, to be interpreted, and to obtain the html result.


Solution

  • Sounds to me like you're not in need of Servlets or JSF, but rather JSP (should look familiar, coming from PHP).

    Notice that people here will tumble over themselves telling you to not put the logic into the page (and for good reason), but I think with your background, you'll be helped by starting from a JSP only approach. Than start moving the logic to a servlet. You can use a number of techniques to communicate the data between the servlet and the jsp. I like to use

    request.setAttribute("someString", valueObject);
    

    In your jsp you than can use:

    <%= request.getAttribute("someString") %>
    

    Calling your jsp from the servlet is done via

    RequestDispatcher rd = req.getRequestDispatcher("/path.to.your.jsp");
    

    If you really need a .xhtml file (your original question), you can use this last line as well, but it'll be real xhtml, like it would be served from a plain webserver (!= appserver)