简体   繁体   中英

Check server version from javascript

In our development environment we use jetty, in our production we use tomcat.

For some functionality we use javascript but there are some hardcoded locations for the use of jetty or tomcat.

I know it's a bit weird to use two server versions but it just the way it is.

So now when we are building the application, sometimes people forget to change the server version in the javascript file.

Is there a way to automatically check if the server is jetty or tomcat from javascript?

I was thinking of placing an txt file in the root of tomcat and let it check whether or not it exists but maybe there is a way to do it more natively.

you could make an ajax request to the server and if each one responds in a unique way, you'll know which is which. whether that's the existence of a different file or different content in a particular file, there are many ways the servers can differentiate themselves to the client.

Assuming one/both of your servers are sending the Server HTTP header (and Jetty usually does, and can easily be configured to do so), then you could use an XMLHttpRequest and look at the response headers.

Read more here: Accessing the web page's HTTP Headers in JavaScript

However, I would suggest that you extract the pieces of code that change between servers into 1 javascript file. eg:

/* server_info.js */
locations = {
   file1 : "/some/path",
   file2 : "/another/path"
};

And include that file as a <script> in all your pages.
Then you can have Jetty and Tomcat each use a different version of that file. It should be easy enough to have a servlet (or filter, or action, or whatever exists in your framework) that looks at the server type and serves up the right file.

If that's too much, then you could do the same thing, but simply have:

/* server_info.js */
server_type = "tomcat";

And vary that file by server (you could easily generate that file in a JSP, or something similar)

Obligatory warning: As I'm sure you know, having different servers in dev and prod is not a fantastic idea, for these sorts of reasons. Once you implement a solution to this problem, how are you going to know that the tomcat code works?
Jetty is more than capable of being a production server, and tomcat can do a good job in development. I suspect you (as a team) are making more work for yourselves than really ought to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM