Run tomcat application from any subdomain, subfolder or ROOT

1.6k Views Asked by At

I'm new to java web development so bear with me.

I have created a application which runs on localhost:8080. The application uses a simpel MVC setup (controller servlet forwards request to jsp page).

If I deploy my application to a remote server in a subfolder (www.myserver.com/myapplication) the application does not work, because all url's/paths in the application are based on the server ROOT and not a subfolder.

I want any one to be able to install the application in any subfolder, subdomain or in the server ROOT, without configuring the tomcat server.

Is there a way to write the url's/paths in a way that the application will run from any subfolder, subdomain or in de server ROOT? This includes the url's/paths in the: servlet, the jsp page and the servelet mapping (web.xml or annotation)

2

There are 2 best solutions below

0
On BEST ANSWER

Maybe my question was unclear, after a lot of searching and trying I got my application to work, no matter in what subfolder it is installed.

I did this bij adding request.getContextPath() to the beginning of every url in de jsp (view) page.

For example to get the css file:
<link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/style.css">

For de form action I used:
<form action="<%= request.getContextPath() %>"/controlerServelet" method="post">

The only thing I still don't understand is why I don't have to do the same thing in my servlet code. For example my code to forward the request to the view.jp:
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(response.encodeURL("/WEB-INF/view.jsp")); dispatcher.forward(request, response);

0
On

If you want to deploy your application in a sub-folder and url should point to the subfolder than you have to create a host for every sub-folder.

Am giving you the XML which you have to add in server.xml . Please find it

<Host name="www.aaa.com" appBase="webapps\AEE"
             unpackWARs="true" autoDeploy="true">
             <Alias>AEE</Alias>
             <Context path="" docBase="."/>
             </Host>

here appBase=webapps\Your Project folder