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

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.

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

How to retrieve web application root path in Java

I am coding my Spring MVC based web application (which was deployed with a war file) and trying to get the value of
String rootDir = request.getSession().getServletContext().getRealPath("/");
I am expecting to get
"C:\user\projects\MyApp"
but the actual value is
"C:\user\projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyApp\"
I tried to change Source Folder Output Location in Eclipse, but after that all my jsp files, web.xml and folder \WEB-INF are gone. How can I restore them?
The reason I am asking this is that I want my uploaded images to be saved in "MyApp\webapp\WEB-INF\resources\images\" folder.
Why are you expecting the path to be "C:\user\projects\MyApp"?
Your are running your application using Eclipse and Eclipse is deploying it to C:\user\projects\.metadata\.plugins\org.eclipse.wst.server.core and not running it from project source folder that is why you get "C:\user\projects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\MyApp\" from getRealPath() call.
From ServletContext#getRealPath()
Gets the real path corresponding to the given virtual path. For
example, if path is equal to /index.html, this method will return the
absolute file path on the server's filesystem to which a request of
the form http://://index.html would be
mapped, where corresponds to the context path of this
ServletContext.
The real path returned will be in a form appropriate to the computer
and operating system on which the servlet container is running,
including the proper path separators.
Resources inside the /META-INF/resources directories of JAR files
bundled in the application's /WEB-INF/lib directory must be considered
only if the container has unpacked them from their containing JAR
file, in which case the path to the unpacked location must be
returned.
This method returns null if the servlet container is unable to
translate the given virtual path to a real path.

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.

How to create a file in webroot during runtime in java web project?

Hi i must create a XML file in the project using java dynamically and must read it using the base Path URL. i can read the File when i create it manually, but not able to create it dynamically. When i use File f1 = new File("test.XML"); it create the file in the tomcat's Bin folder. The file must be created in the Project while Running in both Tomcat and Jboss EAP 6.
You can get the real path of the web application using the servlet context:
new File( servletContext.getRealPath( "/text.XML" ) );
Note: if you're running tomcat/jboss on Linux, you'll probably have to give permission to write in the webapp folder though, which is most likely forbidden by default.
Quoting from Servlet API docs of ServletContext.getRealPath():
Returns a String containing the real path for a given virtual path.
For example, the path "/index.html" returns the absolute file path on
the server's filesystem would be served by a request for
"http://host/contextPath/index.html", where contextPath is the context
path of this ServletContext..

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