<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
<document>
	<header>
		<title>Frequently Asked Questions</title>
	</header>
	<body>
        <section><title>How Do I Ensure that JSPs are Accessible Only Through the Controller Class?</title>
		<p>To prevent users from navigating directly to a JSP
page, register the following filter class.  The filter class will intercept direct requests for JSPs
and redirect the user to the Controller class in the JSP's parent directory.		</p>
<source>package example;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class JspToPageFlowFilter implements Filter
{
    public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain )
        throws IOException, ServletException
    {
        // Redirect to the begin method in the parent directory
        String pagePath = ( ( HttpServletRequest ) request ).getContextPath() + ( ( HttpServletRequest ) request ).getServletPath();
        int lastSlash = pagePath.lastIndexOf( '/' );
        String parentDir = pagePath.substring( 0, lastSlash + 1 );
        String beginAction = parentDir + "begin.do";
        ( ( HttpServletResponse ) response ).sendRedirect( beginAction );
    }

    public void init( FilterConfig filterConfig ) 
        throws ServletException
    {
    }

    public void destroy()
    {
    }
}</source>
<p>Register the class in <code>WEB-INF/web.xml</code> as shown below:</p>
<source>   &lt;filter>
       &lt;filter-name>JspToPageFlowFilter&lt;/filter-name>
       &lt;filter-class>example.JspToPageFlowFilter&lt;/filter-class>
   &lt;/filter>

   &lt;filter-mapping>
       &lt;filter-name>JspToPageFlowFilter&lt;/filter-name>
       &lt;url-pattern>*.jsp&lt;/url-pattern>
       &lt;dispatcher>REQUEST&lt;/dispatcher>
   &lt;/filter-mapping></source>
			</section>
	</body>
	<footer>
		<legal>Java, J2EE, and JCP are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.<br/>
	&copy; 2004, Apache Software Foundation
	</legal>
	</footer>
</document>
