开发者

Make all requests to mysite.com/user/specified/path run the same JSP

I want to allow users to create groups in my application and access them via URL. For example, if you made a group called "sweethatclub," you could access it at http://mysite.com/sweethatclub. Of course, the same code will run for /sweethatclub and /drillteam and even /students/yearbook

I'm running in a Java serv开发者_如何学JAVAlet environment, and can't quite get the paths to align for this. I can write a filter that intercepts all requests and adds information to the request by parsing the URL, but then I want to run the code of an index.jsp. I don't want to map index.jsp to all URLs, because, for example, /images/smiley.jpg still needs to respond the with appropriate file instead of index.jsp.

Is there a way to send all requests to a servlet, unless the request is matched by a plain-old file? Or, is there some other way to accomplish what I want here?

Please let me know if I need to supply more information. I'm new to this environment.


The URL patterns in the web.xml are not supposed to be smart enough to figure out target URL's nature. If you can tolerate it, the easiest way would be to place all the user specified paths under a a well known root... someplace separate from the static files. So you end up with user specified paths like http://mysite.com/sites/sweethatclub.

Alternatively, you can move all your static content under http://mysite.com/static/, and set up the servlet mappings or filters to treat anything starting with 'static' different from the dynamic URL space.


If you are in a Unix invironment, you could just create all the "group sites" as virtual directories that just point to your default one.


  1. Map the servlet on a specific URL pattern

    <url-pattern>/groups/*</url-pattern>
    
  2. Put all static content in a common folder, e.g. /static and fix all URLs in the pages to point to that URL instead.

  3. Create a filter which is mapped on

    <url-pattern>/*</url-pattern>
    

    and does the following job in doFilter() method

    String uri = ((HttpServletRequest) request).getRequestURI();
    
    if (uri.startsWith("/static/")) {
        chain.doFilter(request, response); // Goes to default servlet.
    } else {
        request.getRequestDispatcher("/groups" + uri).forward(request, response);
    }
    

    No, this does not end up with /groups in browser address bar URL. It's fully transparent. You can if necessary make "/static" and/or "/groups" an <init-param> of the filter so that it's externally configureable.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜