Where is Spring MVC context path set? - java

I am not sure how context path is set.
When I rename my .war file in tomcat on autodeploy, the web page goes to localhost:8080/newDirectory as expected, however for some reason wherever there's a call to pageContext.request.contextPath in a Spring based page, it still returns the old context path.
I tried to override the context path by setting:
<context path="/newDirectory" docBase="appName" override="true"></context>
in server.xml but it doesn't work.
My question is, where does Spring read its context path from? I used Maven and I did see there's a
<appContext>/${project.artifactId}</appContext>
in the pom.xml, does this mean I need to rename the artifactId to newDirectory ?
I have also tried adding that <context path="/newDirectory"...> in /META-INF/context.xml (which now I know will be ignored anyway due to my server.xml changes).
Thanks in advance for your answer.

It not depends on Spring maybe you are using a maven plugin to build your war that reads the appContext property. You can read about definig ServletContext in this thread.

Every PageRequest will get current HttpServletRequest object and get context path of current request and append your .jsp (that will work even if the context-path this resource is accessed at changes).
Eg,
if you have war file as MyCompany.war and having a page with ${pageContext.request.contextPath}/MyJspPage.jsp.
Then your context path is http://abc/MyCompany and it works as http://abc/MyCompany/MyJspPage.jsp.
Suppose if you change your war file as OurCompany.war,
then your context path changes to http://abc/OurCompany and Jsp will work as http://abc/OurCompany/MyJspPage.jsp.
It means context path will change automatically to name of the application(War file name) with out any changes.
In your case,after renaming war file name with newDirectory,your webserver will deploy newDirectory application but still newDirectory application exist in the web server.I think you should delete old application from webapp and then check by reloading your newDirectory application.

Related

How to deploy .war file with fixed path in Tomcat?

My .war file is created with a version in it. For example:
myproject-1.0.0-SNAPSHOT.war
For next version it would be second version :
myproject-2.0.0-SNAPSHOT.war
and so on.
Now if I deploy to tomcat the path would change with every new version as Tomcat just takes the filename as the context path.
Of course this is not acceptable as the path should be fixed.
So I already tried to create a context.xml file in META-INF folder and set path there , but did not help.
What else could I do ?
You can include a context version in your war's file name by using "##" as delimiter, see https://tomcat.apache.org/tomcat-9.0-doc/config/context.html#Naming
It will be stripped on deployment, leaving you with the same context path as before.
You could rename the war file before deploying.
Or, if you're using the api from the manager app to deploy, set the path parameter
http://localhost:8080/manager/text/deploy?path=/foo
Reference: https://tomcat.apache.org/tomcat-8.5-doc/manager-howto.html

Tomcat 8 error: invalid character ':' in value part of property

I just started learning Java EE and came across this error when attempting to deploy a WAR file on my tomcat server.
This error happens every time I use the manager application to deploy a WAR. When I select the file located at C:\Users\julex_000\workspace\application\target\application-0.0.1-SNAPSHOT, Tomcat sets it's name as /C:Usersjulex_000workspaceapplicationtargetapplication-0.0.1-SNAPSHOT. It then creates a file called C in the Tomcat8.0\webapps folder and when I try to start the application it fails every time.
The invalid ':' seems to be the colon after C in the path C:\Users\...
I also get an IOException about this:
This error only occurs when using the manager application. When I copy and paste the WAR into the webapps directory it works just fine.
It is simply not allowed to have a : in the context path.
At the deployment you have to pass a valid context path.
For example if you are using the tomcat manager ui:
You have to set the field Context Path and the WAR or Directory URL (the lokal path on the application server where tomcat can find the war file; If you tomcat is not on your localhost you have to move the war file manually and use the path on the server).
If you are using the form with the upload field in the ui, there is no input field where you can pass the context path, but tomcat will use the filename of the war (not the complete path) as context path.
At last but not least if you are using the non-ui-api you can just send your war file with a http put request and pass the url parameter path. You can find more information about this in the Documentation.

Tomcat: Get short URL for Jersey based Rest Service

I have a Jersey based Rest service running on a tomcat server. There is no UI, just a server that offers some rest services. Now, to access this service the URL that i have to type in is pretty long. Something like localhost:8080/MyApp/url_pattern/classPath/method where MyApp is the webapp that i deployed, url_pattern is the pattern that i defined in the servlet-mapping in web.xml, classPath and method being the #Path annotations for the Class and method respectively. Is it possible to shorten it such that I get rid of the MyApp and url_pattern part of this URL. Something like localhost:8080/classPath/method.
PS: There is just one webApp running on this server, so no point having the MyApp part
I don't think you can remove all what you desire from the url but you can definitely remove the MyApp part by making it the root application for tomcat.
Answer on this related link describes it pretty well, how to set your application as the root application. So you can access your REST services without having the app name in url:
Setting default application in tomcat 7
Content copied from the above link:
First Method:
first shutdown your tomcat [from the bin directory (sh shutdown.sh)]
then you must delete all the content of your tomcat webapps folder (rm
-fr *) then rename your WAR file to ROOT.war finally start your tomcat [from the bin directory (sh startup.sh)]
Second Method:
leave your war file in CATALINA_BASE/webapps, under its original name
- turn off autoDeploy and deployOnStartup in your Host element in the server.xml file. explicitly define all application Contexts in
server.xml, specifying both path and docBase. You must do this,
because you have disabled all the Tomcat auto-deploy mechanisms, and
Tomcat will not deploy your applications anymore unless it finds their
Context in the server.xml.
Note:
that this last method also implies that in order to make any change to
any application, you will have to stop and restart Tomcat.
Third Method:
Place your war file outside of CATALINA_BASE/webapps (it must be
outside to prevent double deployment). - Place a context file named
ROOT.xml in CATALINA_BASE/conf//. The single element in this context
file MUST have a docBase attribute pointing to the location of your
war file. The path element should not be set - it is derived from the
name of the .xml file, in this case ROOT.xml. See the Context
Container above for details.

do we need to deploy war after changing property file

Do we need to deploy war after changing the property file in java?
At least you may have to restart the application so that it reads the new property file. So re-deploying the entire application may not be necessary.
you do need war to deploy if property file is not loaded each time it is needed
Yes, unless you make your context reloadable. To do that update your conf/context.xml, adding the "reloadable" attribute to the root node like so:
<Context reloadable="true">
Take a look at the Tomcat Config Docs.

customize web application root context in apache

I have web application abc.war and I want to deploy it on Apache Tomcat.
The problem is that, by default, the path to this application is http://<server-name>/abc
but I want to access it as http://<server-name>/xyz.
I put into WAR's META-INF folder the file context.xml that is :
<Context path="/xyz" docBase="abc" override="true" />
The application WAR abc.war is located under %CATALINA_HOME%\webapps and it is extracted to %CATALINA_HOME%\webapps\abc folder.
Also, while deployment, the file context.xml from abc/META-INF is copied to %CATALINA_HOME%\conf\Catalina\localhost as abc.xml
It seems that this should work, but I still can't access my application through http://<server-name>/xyz, but only through http://<server-name>/abc
In addition, I still see in apache log the following line while deployment of abc.war :
context path = /abc
Could anybody, please, help while this doesn't work, or tell if there is any way of deploying of web application on apache such that application could be accessed by customized path (that does not relate to war-file name) ?
Thanks in advance.
Take a look at the docs:
The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing. All of the context paths within a particular Host must be unique. If you specify a context path of an empty string (""), you are defining the default web application for this Host, which will process all requests not assigned to other Contexts.
The value of this field must not be set except when statically defining a Context in server.xml, as it will be inferred from the filenames used for either the .xml context file or the docBase.

Categories

Resources