2

I'd like to simplify the URL's to access a Glassfish V3 application by removing file extensions and otherwise shortening URL's.

I've already set my application as the default application, so that there is no need to include the context root in the URL.

I'd like to:
* Remove file extensions
* Shorten the URL to files deep in the folder structure

I'd like to do this using pattern matching rather than on a per file basis (Site is small at the moment but will change frequently and grow).

Some examples of what I'd like to do:
* foo.com/bar.html -> foo.com/bar
* foo.com/folder1/folder2/bar2.html -> foo.com/bar2

Any help would be greatly appreciated. Thanks.

Cheers,

Jin

3 Answers3

2

Taken from http://www.sun.com/bigadmin/sundocs/articles/urlrdn.jsp seems like this is what you are looking for.

URL Redirection Within a Domain

You can use the url-prefix element of the redirect_ property to forward a URL to another URL in the same domain.

The following procedure shows how to enable visitors to a web site to type in http://www.mywebsite.com/myproduct1 and be redirected or forwarded to http://www.mywebsite.com/mywarname/products/myproduct1.jsp.

  1. Log in to the Admin Console of Sun Java System Application Server or GlassFish.
  2. In the Admin Console, expand the Configurations node.
  3. Expand the server-config node.

    Ignore this step if you are running a developer domain (a domain that does not have clustering capability).

  4. Expand HTTP Service.

  5. Expand Virtual Servers.
  6. Click server.
  7. On the Edit Virtual Server page, click the Add Property button.
  8. In the Name column, type redirect_1.
  9. If you are using Application Server 9.0, type from=/<context-root>/myproduct1 url-prefix=/mywarname/mypages/products/myproduct1.jsp in the Value column.

    Note - The value of the <context-root> you provide here needs to match the value of the context root specified in the web.xml or application.xml file.

    If you are using Application Server 9.1, type from=/myproduct1 url-prefix=/mywarname/mypages/products/myproduct1.jsp in the Value column.

Prix
  • 4,881
  • 3
  • 24
  • 25
  • Thanks for this, but I believe this requires each page to be set up individually - please let me know if this is not the case. I'd like a solution where I can use wildcards to specify multiple redirections (e.g. remove the extensions for all HTML files). –  Aug 12 '10 at 06:05
  • 1
    From what i saw you have 2 choices beside the above one: A) use Tuckey's Url Rewrite Filter (http://www.tuckey.org/urlrewrite/) B) or use apache as your front end with mod_rewrite and mod_proxy – Prix Aug 12 '10 at 06:37
1

In this case you should write a javax.servlet.Filter implementation which recognizes the shortened URLs and forwards the requests to the real target.

/**
 *
 * @author vrg
 */
@WebFilter(filterName = "SimplifiedUrlRequestDispatcher", urlPatterns = {"/*"})
public class ShortenedUrlRequestDispatcher implements javax.servlet.Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //reading configuration from filterConfig
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String shortURI = httpRequest.getRequestURI();
        String realURI = getRealURI(shortURI);
        if (realURI == null) {
            //in this case the filter should be transparent
            chain.doFilter(request, response);
        } else {
            //forwarding request to the real URI:
            RequestDispatcher rd = request.getRequestDispatcher(realURI);
            rd.forward(request, response);
        }
    }
    /**
     * Calculates the real URI from the given URI if this is a shortened one,
     * otherwise returns null.
     * @param shortURI
     * @return 
     */
    private String getRealURI(String shortURI) {
        //TODO implement your own logic using regexp, or anything you like
        return null;
    }

    @Override
    public void destroy() {
    }

}
0

привет! Excellent primer, but one more thing - RequestDispatcher rd = request.getRequestDispatcher(realURI); operates with servlet path, getting the URI with application name, it will give an error. so - req.getServletPath() gives a right path without context and dispatcher works like it should.