Any clever ways of handling the context in a web app?

前端 未结 13 1460
一整个雨季
一整个雨季 2020-12-02 13:37

In Java, web apps are bundled in to WARs. By default, many servlet containers will use the WAR name as the context name for the application.

Thus myapp.war gets depl

相关标签:
13条回答
  • 2020-12-02 13:50

    I've used helper classes to generate img tags etc. This helper class takes care of prefixing paths with the application's contextPath. (This works, but I don't really like it. If anyone has any better alternatives, please tell.)

    For paths in css files etc. I use an Ant build script that uses a site.production.css for site.css in production environment and site.development.css in development encironment.

    Alternatively I sometimes use an Ant script that replaces @token@ tokens with proper data for different environents. In this case @contextPAth@ token would be replaced with the correct context path.

    0 讨论(0)
  • 2020-12-02 13:56

    Except for special cases, I'd recommend against using absolute URLs this way. Ever. Absolute URLs are good for when another webapp is pointing at something in your webapp. Internally -- when one resource is pointing at a second resource in the same context -- the resource should know where it lives, so it should be able to express a relative path to the second resource.

    Of course, you'll write modular components, which don't know the resource that's including them. For example:

    /myapp/user/email.jsp:
    Email: <a href="../sendmail.jsp">${user.email}</a>
    
    /myapp/browse/profile.jsp:
    <jsp:include page="../user/email.jsp" />
    
    /myapp/home.jsp:
    <jsp:include page="../user/email.jsp" />
    

    So, how does email.jsp know the relative path of sendmail.jsp? Clearly the link will break on either /myapp/browse/profile.jsp or it will break on /myapp/home.jsp . The answer is, keep all your URLs in the same flat filepath space. That is, every URL should have no slashes after /myapp/ .

    This is pretty easy to accomplish, as long as you have some kind of mapping between URLs and the actual files that generate the content. (e.g. in Spring, use DispatcherServlet to map URLs to JSP files or to views.)

    There are special cases. e.g. if you're writing a browser-side application in Javascript, then it gets harder to maintain a flat filepath space. In that case, or in other special cases, or just if you have a personal preference, it's not really a big deal to use <%= request.getContextPath() %> to create an absolute path.

    0 讨论(0)
  • 2020-12-02 13:57

    The Servlet API and JSP have facilities to help manage this. For example, if, in a servlet, you do: response.sendRedirect("/mypage.jsp"), the container will prepend the context and create the url: http://example.com/myapp/mypage.jsp".

    Ah, maybe, maybe not - it depends on your container and the servlet spec!

    From Servlet 2.3: New features exposed:

    And finally, after a lengthy debate by a group of experts, Servlet API 2.3 has clarified once and for all exactly what happens on a res.sendRedirect("/index.html") call for a servlet executing within a non-root context. The issue is that Servlet API 2.2 requires an incomplete path like "/index.html" to be translated by the servlet container into a complete path, but doesn't say how context paths are handled. If the servlet making the call is in a context at the path "/contextpath," should the redirect URI translate relative to the container root (http://server:port/index.html) or the context root (http://server:port/contextpath/index.html)? For maximum portability, it's imperative to define the behavior; after lengthy debate, the experts chose to translate relative to the container root. For those who want context relative, you can prepend the output from getContextPath() to your URI.

    So no, with 2.3 your paths are not automatically translated to include the context path.

    0 讨论(0)
  • 2020-12-02 13:57

    You can use request.getContextPath() to build absolute URLs that aren't hard-coded to a specific context. As an earlier answer indicated, for JavaScript you just set a variable at the top of your JSP (or preferably in a template) and prefix that as the context.

    That doesn't work for CSS image replacement unless you want to dynamically generate a CSS file, which can cause other issues. But since you know where your CSS file is in relation to your images, you can get away with relative URLs.

    For some reason, I've had trouble with IE handling relative URLs and had to fall back to using expressions with a JavaScript variable set to the context. I just split my IE image replacements off into their own file and used IE macros to pull in the correct ones. It wasn't a big deal because I already had to do that to deal with transparent PNGs anyway. It's not pretty, but it works.

    0 讨论(0)
  • 2020-12-02 13:59

    I agree with tardate. I have paid my attention at filters too and have found the solution in face of project UrlRewriteFilter. The simple configuration like following:

    <rule>
        <from>^.+/resources/(.*)$</from>
        <to>/resources/$1</to>
    </rule>
    

    helps to forward all requests for */resources path to /resources pass (including context path prefix). So I can simply put all my images and CSS files under resources folder and continue using relative URLs in my styles for background images and other cases.

    0 讨论(0)
  • 2020-12-02 14:00

    One option is to use "flat" application structure and relative URLs whenever possible.

    By "flat" I mean that there are no subdirectories under your application root, maybe just few directories for static content as "images/". All your JSP's, action URLs, servlets go directly under the root.

    This doesn't completely solve your problem but simplifies it greatly.

    0 讨论(0)
提交回复
热议问题