0

Below is the summary of understanding on context path and servlet path which is a research effort

1. server.servlet.context-path  (old prop: server.context-path)
   Context path of the application. The context path is the path to 
   which the whole web application is mapped.
   "Context path" refers to the path of the URL that refers to the root of your 
   application. Usually, this path is equal to the name of your WAR file.
   defines the context, if not set then it's the root context (/)
   ex: if you set to foo, then your app will listen on http://localhost:8080/foo

2. spring.mvc.servlet.path (old prop: server.path)

   sets the path to the dispatch servlet
   The servlet path is the path to which Spring MVC's DispatcherServlet is mapped.
   it immediately follows the server.servlet.context-path
   If you set this to bar and don't have server.servlet.context-path set, your app 
   will listen on http://localhost:8080/bar If you have the server.servlet.context-path 
   set to foo and spring.mvc.servlet.path set to bar, your app will be listening on 
   http://localhost:8080/foo/bar.
   "Servlet path" refers to a path that refers a specific servlet within your application.

Conclusion:

    server.path = spring.mvc.servlet.path
    server.context-path = server.servlet.context-path

Old property New property

server.context-parameters.*     server.serv1et.context-parameters.*
server.context-path             server. servlet. context-path
server.jsp.class-name           server. servlet. jsp. class-name
server.jsp.init-parameters .*   server. servlet.jsp .init-parameters.*
server.jsp.registered           server. servlet. jsp.registered
server.path                     server.servlet. path

Please add your feedback/comments whether everything is correct.

Tech Lover
  • 21
  • 4
  • Idk if those mappings are correct, but if you have a server that supports multiple applications, each app gets its own path, `http://server.com/app1/*`, `http://server.com/app2/*`. The inside the application you can define spring to run only on a subset of paths (where it installs it's dispatcher).`http://server.com/app2/spring/*` would be context-path = `/app1` and server-path=`/spring` if I'm not mistaken. – zapl Sep 30 '22 at 20:59
  • Zapl, your explanation is good, can you please explain more on the server path of the 2 apps – Tech Lover Oct 01 '22 at 02:13
  • Multiple apps is mostly for Enterprise (as in JavaEE) application servers where you can upload multiple `.war` files, https://stackoverflow.com/a/1594818/995891 for example isn't a bad explanation. Nowadays with spring boot and microservices and so on you have none of that and all those paths are always just `/`. Changing the dispatcher servlet path is super uncommon because that's only useful if you want plain servlets without spring dispatching the requests. The context path is usually the only thing changed when people want to "move" all their request urls from "/*" to "/something/*". – zapl Oct 01 '22 at 02:24
  • context path is a concept of those application servers, anything within that path goes to one application. The servlet path is a spring thing it's dispatcher servlet is the entrypoint into spring and in rare cases you may want to move that within the application. https://docs.spring.io/spring-boot/docs/2.7.4/reference/htmlsingle/#actuator.cloud-foundry.custom-context-path vs https://docs.spring.io/spring-boot/docs/2.7.4/reference/htmlsingle/#howto.spring-mvc.switch-off-dispatcherservlet – zapl Oct 01 '22 at 02:34
  • @zapl so does app1 and app2 have separate spring application context or spring application context will be ONE ? i'm trying to get the application context -> then spring bean inside a servlet and have this question – Tech Lover Oct 01 '22 at 02:51
  • They are different applications, they don't know each other and don't even need to be both spring applications. They share nothing basically. The webserver decides based on each app's context path which application needs to deal with it. Once the request is in the application, then there's spring and so on, if there is spring. Btw, why are you so interested in this? Is there an issue you actually want to ask about but stumbled on those paths? – zapl Oct 01 '22 at 02:55
  • The reason i am planning to get the spring application context from servlet. in that case i am thinking whether in any case it would return single application context (running in same port or different port?) – Tech Lover Oct 01 '22 at 03:14
  • Not sure what "get the spring application context from servlet" means but different applications have different contexts and the ports are configured on the server you can maybe configure different ports for different applications but that would be a server config, Once you have your server configured with ports, you upload applications onto it so they get requests from whatever ports the server listens on. – zapl Oct 01 '22 at 03:19
  • Zapl, i have put my understanding or thoughts in https://stackoverflow.com/questions/73915393/application-context-servlet-path-for-2-apps-loaded-in-tomcat-webapps-directory -> can you please add your conclusion or throughts. i have summarized. – Tech Lover Oct 01 '22 at 03:20
  • ServletRequestContext is not the spring application context, that's a thing the server (I think) provides with each request and it has information about the current request, e.g. the concrete path, http method, parameters of the request. I think in spring it's represented by https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestContext.html (in this case spring puts references to it's own app/web context into the request context but this is only internal, you don't get other application contexts) – zapl Oct 01 '22 at 03:30

0 Answers0