1

let me give you an idea about how the system works.

I am using JAAS login module for login and role management. I can access specific pages depending on the role i have.

I type my url in the address bar, hit enter.

The login page appears and after correct login, it redirects me to the correct page(for now lets call it page1.jsf).

I want to call a server side method on page load.

Can you help me please?

** EDIT **

Assume i have to access page1.jsf which is accessible to role1 only.

In the address bar, i type http://localhost:8080/myapp/page1.jsf

JAAS shows up the login page and after correctly inputting the credentials, i am redirected to page1.jsf

As soon as page1.jsf is requested or on page load, i want to call a server side method from my class to reload page1.jsf

sameer
  • 220
  • 2
  • 6
  • 18

2 Answers2

2

If you are using JSF 2, you can use the above page snippet:

<html xmlns="http://www.w3.org/1999/xhtml"
  ... >
 <f:view contentType="text/html">
    <f:event type="preRenderView" listener="#{permissionManager.checkRoles}" />
    <f:attribute name="roles" value="ROLE" />

  ...
  </f:view>
</html>

you can add an attribute containing the role and in the PermissionManager.checkRoles() perform redirect to the corret page.

@Named
@ApplicationScoped
class PermissionManager { 
  ...
  public void checkRoles(ComponentSystemEvent event) {

       String acl = "" + event.getComponent().getAttributes().get("roles");
       //Check user role
       ...
       //Redirect if required
       try {

        ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler) context
                .getApplication().getNavigationHandler();
        handler.performNavigation("access-denied");
    } catch (Exception e) {
        ...
    }

   }

}

Check out this example and take a look at this related question

Community
  • 1
  • 1
landal79
  • 1,520
  • 1
  • 11
  • 26
0

Yes this works. Instead of accessing a jsp or jsf page, you can also access Servlets. So create a new servlet. E.g.:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public static void yourMethod() {
         // do something useful
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        yourMethod();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

Then create a new entry in the web.xml file, in order to map the Servlet to /.

<servlet>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>your.packages.TestServlet</servlet-class>
  </servlet>  
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>

After this, you should be able to call localhost:8080/TestServlet which then calls your method.

nimrod
  • 5,595
  • 29
  • 85
  • 149
  • no servlet is to be used mate. I just want to call my server side method as soon as the page is requested/loading – sameer Apr 23 '12 at 07:21