How to support variable interpolation in web.xml? - java

I'm packaging a web application and tomcat into a zip file. This zip should be able to use in any location or path.
This web application require to load an additional folder (response-folder) to be able to startup.
Due to the zip can be unzip anywhere, I need to find out my current path when the server is started and set the path in the startup.bat.
The following is my zip folder structure:
my.zip
- apache-tomcat
- bin
- startup.bat
- webapp
- mywebapp
- WEB-INF
- web.xml
- response-folder
In my startup.bat, I had the following line
set CATALINA_OPTS=-Dapache-tomcat-current-folder="%TOMCAT_CURRENT_FOLDER%"
I would like achieve something like below:
<servlet>
<description>MyService</description>
<display-name>MyService</display-name>
<servlet-name>MyService</servlet-name>
<servlet-class>com.my.package.MyService</servlet-class>
<init-param>
<description>Folder that contains response files</description>
<param-name>RESPONSE_FOLDER</param-name>
<param-value>${apache-tomcat-current-folder}\response-folder</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Note: I'm not able to the change the web application java code, as the web application is not belong to me.
May I know is there anyway to make web.xml to support variable interpolation or achieve behavior such as above?
Any help would be great.

To use environment variables in web.xml, you can add
set "CATALINA_OPTS=-Dapache-tomcat-current-folder=somevalue"
or if you want to reference a System environment variable
set "CATALINA_OPTS=-Dapache-tomcat-current-folder=%apache-tomcat-current-folder%"
in bin/setenv.bat (bin/setenv.sh for *nix).
You need to create this file.

Related

How to expose custom jar in lib folder of java web application?

I have an existing jetty application that i need to modify. Basically i just need one access point that can handle POST requests. I have access to the WEB-INF directory of the project. There i have web.xml file and a lib folder. As i understand from jetty documentation jars under lib will be loaded automatically. Is it possible to add a custom jar under lib folder and the somehow configure web.xml so that my jar will handle HTTP requests from a certain URL? If so how would i start with that?
You can simply create a jar with your Servlet class inside (a class that extends HttpServlet) and add the servlet declaration inside the web.xml
<servlet>
<servlet-name>A_Name</servlet-name>
<servlet-class>your.new.ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>A_Name</servlet-name>
<url-pattern>/the/path/you/want</url-pattern>
</servlet-mapping>

how to access PDF/image/video file located in java server from a browser URL?

I have an mp4 video on my GlassFish java application server. What URL do I use to access it in a browser?
The video path in the application on the server is:
myapp/WEB-INF/videos/myvideo.mp4
I tried adding this to my web.xml file:
<servlet>
<servlet-name>myvideo</servlet-name>
<servlet-class>videos.myvideo.mp4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myvideo</servlet-name>
<url-pattern>/myvideo</url-pattern>
</servlet-mapping>
and restart the server, then go to the URL: https://www.example.com/myapp/myvideo but that just gave this error:
java.lang.ClassNotFoundException: videos.myvideo.mp4
Obviously it's not a servlet, but not sure what else to try.
Why are you trying to access the video as a servlet and not as a static asset, like you would an image? Using a servlet mapping is obviously wrong.
Instead if you have a running WebApp in GlassFish you should be able to access it via https://www.example.com/myapp/videos/myvideo.mp4 assuming you've put the videos folder under your webapp's j2ee root.
The j2ee root would be configured in your application.xml as the web-uri. In your case it is likely that the myapp folder is your j2ee root. So move your videos up a folder.
(Converted comment 'discussion' into an answer).

Manually adding file to classpath

I'm trying to test a war that I deployed in Tomcat 7, however it seems its missing a xml configuration:
web.xml
<servlet>
<servlet-name>StorageEngine</servlet-name>
<servlet-class>com.jpeterson.littles3.StorageEngine</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:StorageEngine-servlet.xml</param-value>
</init-param>
</servlet>
I have downloaded the StorageEngine-servlet.xml separately, to be able to satisfy this init-param where do I need put the
xml file in the \webapps\littleS3-2.3.0\WEB-INF folder, just relative to the web.xml? Or?
No, it belongs in WEB-INF/classes of your WAR file. That is always in the CLASSPATH of a Tomcat app, loaded by the app class loader.
Given the mapping it would need to be placed in the root of your classpath. Try placing it within the root of any source folder

GWT Tomcat problem to RPC call

GWT RPC call don't seems to work when i deploy my war file to TOMCAT (tomcat/webapps/ROOT/war).
It gives me an error:
The requested resource
(/war/myproject/call) is not
available.
If i change the directory structure and then deploy directly war contents (not war directory itself), like (tomcat/webapps/ROOT/project.html, project.css, project, etc...) then it works.
Can someone please explain me whats going on?
I think there might a problem at:
<servlet>
<servlet-name>callServlet</servlet-name>
<servlet-class>com.myproject.server.dao.Call</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>callServlet</servlet-name>
<url-pattern>/myproject/call</url-pattern>
</servlet-mapping>
The thing is that a single Tomcat server can have multiple applications deployed, each in its so-called context. The applications are deployed in the webapps folder and each folder is mapped to one context, while the ROOT folder is the default (no-context).
To access an application on Tomcat, you specify the context after the URL. For example if you had an application (context) Test in webapps/Test folder, you would access it like this:
http://localhost:8080/Test
But applications in the ROOT folder have no context and are accessed by simply going to localhost:8080. And this is your case. Tomcat is looking for you application directly in the ROOT folder but you have your app in the ROOT/war folder. In other words, the RPC call expects the myproject folder to be under the ROOT folder and not under the ROOT/war folder. That's why it's not working.
If you still wanted to have your war folder within the ROOT folder, you would have to change the url-pattern to /war/myProject/call.
Well i found the solution, it had to do with Tomcat's way of operation.
Open your project war directory
Select all the files (html/jsp , images, WEB-INF etc...)
Compress all the files into a single project.zip archieve
Rename the project.zip into project.war
Copy project.war into Tomcat/webapps/
Restart Tomcat server
You will now notice inside webapps directory that project.war has been decompressed into a project directory, if you open it you will find all the .war contents(html/jsp, images, WEB-INF, etc...)
Access it from here http://localhost/project or http://localhost/project/index.html or index.jsp.
The error was: I was compressing only the war directory (not it's inside contents) into project.war.
look like, servlet is not initializing for you war try to change SERVLET tag as
i.e. add tag
<load-on-startup>1</load-on-startup>
this tag ensures that servlet should be loaded
<servlet>
<servlet-name>callServlet</servlet-name>
<servlet-class>com.myproject.server.dao.Call</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
hopefully this will work

Tomcat6 + Ubuntu + Servlet

I am trying to make servlet.
I have installed tomcat6 on ubuntu with admin examples and docs. I am able to run the examples provided. But when i try to make my own servlet it doesnt work.
I did following steps
Under the ROOT i create folder with
-ROOT
----myapp
------WEB-INF
---------classes
I made two files one is index.html which have a button and action on form to call the servlet. Second is .java file. I compiled the .java file and .class is done. So now tree look like
-ROOT
----myapp
------index.html
------WEB-INF
---------classes
-----------TestServ.java
-----------TestServ.class
Now i open this in browser using http://localhost:8080/myapp
It shows up with index.html page with button. But when i click on the button it says
Error 404:
http://localhost:8080/myapp/TestServ not found !!
I dont know where m going wrong. I have set the CATALINA_HOME too. But still this problem continue.
You need to create a web.xml in the WEB-INF directory, and define the servlet mapping in web.xml, so that the myapp/TestServ URL is forwarded to the TestServ servlet class.
Here is a page describing web.xml, and has the example and elements you need to set up. For your class, these elements will probably look something like this:
<servlet>
<servlet-name>testServ</servlet-name>
<servlet-class>TestServ</servlet-class>
</servlet>
<servlet-mapping>
<!-- For any URL starting with /content/, the rewriter servlet will be called -->
<servlet-name>testServ</servlet-name>
<url-pattern>/TestServ</url-pattern>
</servlet-mapping>
You shouldn't be deploying any of your code under ROOT.
You shouldn't have any Java class in the default package. Try putting your TestServ.java in a package.
Your deployment should NOT include any .java files.
You've got to register your servlet properly in web.xml. Include a mapping to a particular URL.
Your best best is to create a WAR file named myapp.war, which includes WEB-INF/classes and WEB-INF/lib and a web.xml for your situation. Put that in the Tomcat /webapps and start the container. If you've registered your servlet properly you should be able to access it via http://localhost:8080/myapp/TestServ.
I'd read the deployment docs carefully.

Categories