Dynamic URL for a web application

1

I have a web application that is about to go into production in my company, and I use url's like the following:

http://170.70.141.252:8080/sisalbm/admin/dashboard.jsp

Where I indicate the ip of my machine and the port of connection (Glassfish), however the server area of my company, tells me that it is bad practice the way in which I map my application and that I have to correct it, since they are going to mount my application in 2 servers in cluster, whose case could complicate the logic of my application.

Handled the following statement in Java, to map the routes:

private static final String servidor = "http://170.70.141.252:8085/";

What do I have to do to make the url's of my application dynamic, that is, not depend on the server on which it is mounted?

    
asked by J. Cristian 14.03.2017 в 20:43
source

2 answers

1

It is actually a bad practice to put a literal like path of a url.

I do not know if you use a framework but you should look at the method:

ServletContext.getContextPath()

What should I return:

"http://170.70.141.252:8080/sisalbm"

And in the views something like this, for example for the action of a form

action="${pageContext.request.contextPath}/admin/dashboard.jsp"

With this you will get the "root" address of your application regardless of where you wake up.

There are other more specific methods of ServletContext depending on the url you want to obtain.

    
answered by 15.03.2017 в 12:49
0

To not depend on a url in "hard" support from Context Path, it depends on what technologies you use for web (JSF, JSP, etc), an example with JSTL:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<body>

<!-- OBtiene context path con JSTL -->

<c:out value="${pageContext.servletContext.contextPath}" />

</body>
</html>

Exit:

/ JSTLExample

    
answered by 16.03.2017 в 19:03