6
server.port=8080
server.contextPath=/myapp

Consider the above configuration in the application.properties of a spring boot application. What is the need of contextPath if we can't really run another spring boot application in the same port 8080. Can anyone provide some real time scenarios where this contextPath useful?

[edit] In my experience, when we deploy multiple web applications (web applications contexts) to single tomcat instance running on 8080 port the context path is used to distinguish the applications. This is what kept me thinking about the possibility of running multiple spring applications in a single port. Otherwise, I don't see the value of having an option to change the contextPath. Also considering the fact that we will have nginx or apache as a web server front and this spring applications are going to be run only inside the firewall.

  • Possible duplicate of [TCP: can two different sockets share a port?](https://stackoverflow.com/questions/11129212/tcp-can-two-different-sockets-share-a-port) – Raedwald Apr 29 '19 at 11:57

3 Answers3

7
  1. No you can have only one application listening on a specific port at a time.

  2. Changing the context path can be helpful in order to globally add a prefix to all your controller endpoints. For instance if your application is serving rest API and you want all the urls to start with /api then one option would be to add this prefix to each request mapping (for example @RequestMapping("/api/user)), or change the context path to /api then you can have your request mapping now as @RequestMapping("/user)

Ahmed Abdelhady
  • 699
  • 3
  • 7
1

Nope same PORT cannot be used by multiple process/applications

RAJKUMAR NAGARETHINAM
  • 1,408
  • 1
  • 15
  • 26
0

We cannot run multiple applications on the same port.

Vamsi Vegesna
  • 87
  • 1
  • 15