0

I'm using Java EE for an enterprise application on Glassfish.

I have some xhtml files in WEB-INF to prevent direct URL access. Those files are displayed by a servlet like this:

request.getRequestDispatcher("/WEB-INF/HiddenFile.xhtml").forward(request, response);

The only problem I'm experiencing is that the expression language in those files that references some managed beans is not getting evaluated.

Any help on getting the Expression Language evaluation for those files is greatly appreciated.

Thanks in advance

EDIT

I didn't consider the information useful but since it has been requested here it is.

The page to be displayed, WEB-INF/HiddenFiles/FileA.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>PAGE A</title>
    </h:head>
    <h:body>
        A - #{testBean.testString}
    </h:body>
</html>

The extract from the servlet responsible of producing a response:

public class RedirectorServlet extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    if (logic()) {
        request.getRequestDispatcher("/WEB-INF/HiddenFiles/fileA.xhtml").forward(request, response);
    } else {
        request.getRequestDispatcher("/WEB-INF/HiddenFiles/fileB.xhtml").forward(request, response);
    }

    *other methods including logic()*

}

The web.xml in WEB-INF:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>RedirectorServlet</servlet-name>
        <servlet-class>servlet.RedirectorServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>RedirectorServlet</servlet-name>
        <url-pattern>/RedirectorServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sofask
  • 47
  • 8

1 Answers1

1

EL is only evaluated by FacesServlet in case of Facelets files and by JspServlet in case of JSP files.

You've mapped the FacesServlet on /faces/*. However, your "redirector servlet" isn't forwarding to an URL matching that pattern. So the FacesServlet won't be invoked and hence the Facelets file with all taglibs and EL on it won't be parsed and executed but just returned like plain text.

You need to change the FacesServlet mapping from /faces/* to *.xhtml, so that it can also be invoked on XHTML files placed in /WEB-INF which is forwarded by a servlet or filter.

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

Unrelated to the concrete problem, the term "redirect" is misplaced here. You're here actually performing a forward. And, you should actually be using a servlet filter for the job.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you for the help, that solved my issue! I also used a filter instead of the servlet as you suggested. – sofask Jan 19 '15 at 14:07
  • Actually I had another problem. I opened a new discussion since, from what I observed, it's not strictly related to this one but I'm not totally sure. The mentioned issue is described here: http://stackoverflow.com/questions/28036049/use-of-hform-in-facelets-files-in-web-inf-opened-by-requestdispatcherforward – sofask Jan 20 '15 at 11:33
  • As hinted in the answer, use a filter (and don't put public files in WEB-INF) This whole approach is indeed awkward. I'm not sure how you decided to go this path. Perhaps you looked too much at decade-old JSP/Struts/Spring based examples? – BalusC Jan 20 '15 at 11:36
  • Thank you for the answer. I did this since this was the only apparently working solution in another question where I asked how to make those files inaccessible via direct URL while also being able to them at a ficticious URL based on internal logic. And I still used a servlet after my first comment, since another person I'm doing this project in collaboration with told me that using a filter dispatching to the files in WEB-INF, the primefaces theme wasn't getting loaded on the landing page so he decided to use the servlet. We are re-doing this with filters – sofask Jan 20 '15 at 11:54
  • Use this example: http://stackoverflow.com/questions/14580267/authorization-redirect-on-session-expiration-does-not-work-on-submitting-a-jsf-f/ In the future, be very careful with XY problems. It'll otherwise only waste your time: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – BalusC Jan 20 '15 at 12:02
  • Thank you for being so patient, and for providing that example which I'll definetely make use of. I'll definetely try to be more careful with the XY problem in the future. This time I actually followed the answers given in this question http://stackoverflow.com/questions/28015491/java-ee-show-page-based-on-internal-business-logic?noredirect=1#comment44442089_28015491 thinking that the solution provided by Tiny was correct for my purposes. In the future I'll make sure to always post my actual problem (like I did in that question but more specifically I suppose). – sofask Jan 20 '15 at 12:27
  • In future JSF questions, use [jsf] tag to attract the right audience. – BalusC Jan 20 '15 at 13:37