我想通过删除文件扩展名或缩短URL来简化URL来访问Glassfish V3应用程序。
我已经将应用程序设置为默认应用程序,所以不需要在URL中包含上下文根。
我想:
*删除文件扩展名
*缩短URL到文件夹结构深处的文件
我想使用模式匹配,而不是每个文件的基础上(网站是小,但会经常变化和增长)。
我想要做的一些例子:
* foo.com/bar.html – > foo.com/bar
* foo.com/folder1/folder2/bar2.html – > foo.com/bar2
任何帮助将不胜感激。 谢谢。
干杯,
斤
采取从http://www.sun.com/bigadmin/sundocs/articles/urlrdn.jsp似乎这就是你在找什么。
域内的URLredirect
您可以使用redirect_属性的url-prefix元素将URL转发到同一个域中的另一个URL。
以下过程显示了如何使访问者能够在网站上inputhttp://www.mywebsite.com/myproduct1并将其redirect或转发给http://www.mywebsite.com/mywarname/products/myproduct1.jsp 。
展开服务器configuration节点。
如果您正在运行开发人员域(不具有群集function的域),请忽略此步骤。
展开HTTP服务。
redirect_1 。 如果您使用的是Application Server 9.0,请在“值”列中inputfrom=/<context-root>/myproduct1 url-prefix=/mywarname/mypages/products/myproduct1.jsp 。
注 – 您在此处提供的<context-root>的值需要与web.xml或application.xml文件中指定的上下文根的值相匹配。
如果您使用的是Application Server 9.1,请在值列中inputfrom=/myproduct1 url-prefix=/mywarname/mypages/products/myproduct1.jsp 。
在这种情况下,您应该编写一个javax.servlet.Filter实现,它可以识别缩短的URL并将请求转发给实际的目标。
/** * * @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() { } }
привет! 优秀的引物,但还有一件事 – RequestDispatcher rd = request.getRequestDispatcher(realURI); 使用servletpath运行,获取带有应用程序名称的URI,会给出错误信息。 所以 – req.getServletPath()提供了一个正确的path,没有上下文和调度程序的工作就像它应该。