0

I'm implementing an enterprise application with Java EE on Glassfish server. I need to my application to execute some logic to show the proper output for a specific subset of URLs.

Problem description:

My web pages folder has this structure:

Web Pages
  Protected
    - CorrectPage.xhtml
    - A.xhtml
    - B.xhtml
  - Index.xhtml

I want the user to access the URL:

/Protected/CorrectPage.xhtml

But the user must not be able to access the following URLs:

/Protected/A.xhtml
/Protected/B.xhtml

When the URL /Protected/CorrectPage.xhtml is entered I want to execute some logic and depending on the outcome of this logic I want to show either A.xhtml, or B.xhtml, without any visible URL changes (redirects).

Solutions tried so far:

I thought about using a servlet mapped to /Protected/*.xhtml while leaving the Faces Servlet deal with any other URL in my application.

and having :

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    if(*Requested URL is /Protected/CorrectPage.xhtml*) {
       if(logic())
          *Show A.xhtml*
       else
          *Show B.xhtml*
    } else
       *Show 404*

My issue is that I don't know how to implement the Show A.xhtml. I basically want to print to the client my xhtml page.

I also thought about solving this last issue by using the response PrintWriter.

PrintWriter pw = response.getWriter();

But than again this doesn't solve my issue since I don't know how to print the xhtml file while also having the evaluation of the expression language contained in it.

Conclusion

Any help is extremely appreciated. Even if that means changing something in the structure I proposed. Naturally if the creation of a servlet isn't the correct solution for my issues I will leave that track.

I'm interested only in the outcome the user will experience.

Thanks in advance

sofask
  • 47
  • 8
  • Make those resources (XHTML files - A.xhtml, B.xhtml...) available under `WEB-INF` and forward the request to them based on this, "*depending on the outcome of this logic I want to show either A.xhtml, or B.xhtml, without any visible URL changes*". (make the associated action method return an appropriate navigation case outcome such as `return "/WEB-INF/pages/A.xhtml";`). – Tiny Jan 18 '15 at 23:25
  • "I want to show either A.xhtml, or B.xhtml, without any visible URL changes (redirects)", you want the user to see a different page without changing the url, how is it even possible? – Biu Jan 18 '15 at 23:29
  • I'll try that solution Tiny. I didn't thought about that. – sofask Jan 18 '15 at 23:33
  • @Tiny I was able to do what you suggested, and I'm able to access those files using the servlet's logic. The problem I'm experiencing is that for some reason the expression language contained in those files I put in WEB-INF isn't evaluated at all. – sofask Jan 19 '15 at 00:02
  • I attempted this approach in my real application. I could see everything happening correctly. Evaluation of EL, `` etc happened all correctly. I just forwarded a request to a resource in question using an `action` method associated with a `` or `` such as `return "/WEB-INF/pages/A.xhtml";`. – Tiny Jan 19 '15 at 15:52
  • I checked and I was doing another mapping wrong, sorry. This solved my issue completely. Thanks Tiny! – sofask Jan 19 '15 at 16:49

1 Answers1

0

You may use request.getRequestDispatcher("/protected/page[A|B]").forward(request, response)

Alessandro Santini
  • 1,943
  • 13
  • 17
  • Please note that `page[A|B]` shall be read as a regular expression, i.e. `pageA` or `pageB` – Alessandro Santini Jan 18 '15 at 22:29
  • Thanks for you answer but I think that would call recursively my servlet. My servlet is mapped also to "/protected/A.xhtml" to avoid the direct access to those pages from URL. – sofask Jan 18 '15 at 23:05
  • I think you can simply include some code in the pages so to prevent direct invocation. – Alessandro Santini Jan 18 '15 at 23:07
  • Could you specify some? I don't know what code are you talking about specifically, but I think that would solve my issue. – sofask Jan 18 '15 at 23:09
  • I am from a tablet at the moment so including code is difficult but: – Alessandro Santini Jan 18 '15 at 23:12
  • The servlet adds a variable in the request scope. The page immediately checks if this variable is in the request connects and if not raises an exception out generates a 404 – Alessandro Santini Jan 18 '15 at 23:14
  • I updated my OP with pseudo-code I derived from your suggestion to make sure I understood it. But I don't think so since that doesn't solve the issue since I still can't show either A or B. – sofask Jan 18 '15 at 23:32