开发者

Servlet + redirection

开发者 https://www.devze.com 2023-03-19 11:55 出处:网络
I need some suggestions. I have defined servlet mapping as <servlet-mapping> <servlet-name>My Servlet</servlet-name>

I need some suggestions. I have defined servlet mapping as

<servlet-mapping>
 <servlet-name>My Servlet</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>

However there are some static html files. So i have mapped them to the default servlet to serve the static html files

<servlet-mapping>
 <servlet-name>default</servlet-name>
 <url-pattern>*.html</url-pattern>
</servlet-mapping>

However, i want the user to have access to them only when the user has logged in. The welcome page is Login.html. If the user tries to access any other static file and has not logged in i开发者_如何学Go.e there is not session then i should redirect user to the login page. But with current mapping the user is able to access index.html file as the request is served by default servlet.

Please suggest .


Your intent is to have a front controller servlet, not to replace the default servlet. So you should actually not be mapping your front controller servlet on /.

You should map the controller servlet on a more specific URL pattern, such as /app/*. To keep URLs transparent, your best bet is to create a filter which determines the request URI and continues the chain on static content like HTML and dispatches the remnant to the controller servlet.

E.g.

String uri = request.getRequestURI();

if (uri.endsWith(".html")) {
    chain.doFilter(request, response);
} else {
    request.getRequestDispatcher("/app" + uri).forward(request, response);
}


You can extend the DefaultServlet of your web server.The extended servlet will be your front controller. In the doGET or doPOST method forward your static pages to the super class. DefaultServlet is the servlet that is mapped to url "/" by default. I have used it with jetty server but it can be implemented in tomcat as well.

public class FrontController extends DefaultServlet {

@Override
public void init() throws UnavailableException {
    super.init();
}

@Override
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    String uri = request.getRequestURI();

    /*
     * if request is trying to access inside /static then use the default
     * servlet. YOU CAN USE YOUR OWN BUSINESS LOGIC TO FORWARD REQUESTS 
     * TO DEFAULTSERVLET
     */
    if (uri.startsWith("/static/")) {

        super.doGet(request, response);
        return;
    } else {

        // else use your custom action handlers
    }
  }
}

In the above code samples I have forwarded all the requests starting with /static/ to the default servlet to process. In this way you can map the FrontController to "/" level .

<servlet>
<description></description>
<display-name>FrontController</display-name>
<servlet-name>FrontController</servlet-name>
<servlet-class>FrontController</servlet-class>
<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>/</url-pattern>
0

精彩评论

暂无评论...
验证码 换一张
取 消