You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Peter Severin <pp...@architekt.ro> on 2001/07/19 09:34:00 UTC

Subclass ActionServlet to behave as central controller: Solution

 Hi ppl,


	I have done some research on this ( looking into the struts sources )
and found an interesting solution. This solution permits using
ActionServlet as a single entry point to the web application. 

Step 1. Add a default action which will handle uknown path requests.
    <action    path="/unknown"
               type="ro.architekt.struts.DefaultAction"
               unknown="true">
    </action>

The DefaultAction.perform method is the following:

    public ActionForward perform(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
	    throws IOException, ServletException
    {
        
        String requestURI = request.getRequestURI();
        String contextPath = request.getContextPath();
        
        String forwardPath = requestURI.substring( contextPath.length()
);
        forwardPath = forwardPath.substring( 0,
forwardPath.indexOf("do")) + "jsp";
        
        System.out.println("Requested URI = " + requestURI);
        System.out.println("Forwarding to: " + forwardPath);

        return new ActionForward(forwardPath);
    }

The code above will forward requests to the path like /some_page.do to
the page /some_page.jsp . Now you can request all the jsp pages using
the .do extension. Eventually DefaultAction can be modified to forward
request to  jsp pages under /WEB-INF directory so you can prevent direct
requests to the jsp page.

Now step 2:

2. Subclass ActionServlet and add your own logic like authentication to
be done before calling super.perform() method. Other interesting things
can be done here: insert page nocache headers, check paths which
should/shouldn't be accessed through ssl (and redirect)., add a template
mechanism like sitemesh ( I don't like template tags very much ). I know
this things can be done using filter API. But tomcat4 is still very
buggy so I can't use it in production.

Hope you find this usefull,

Peter.