image not loading in jsp deployed inside webapps ROOT folder - image

When i deploy the war file named 'myapp.war' inside the tomcat 6 webapps folder the images in jsp are loading properly whereas if i deploy it inside webapps/ROOT folder the images are not loading in jsp.
war file structure
-- myapp
- WEB-INF
-jsp
-images
In the jsp page the image inside the images folder are called like
<img src="images/img1.png"/>

You should use an absolute path for referencing images. If you include the contextPath in it, the link should work even if your application path changes. Try
<img src="${pageContext.request.contextPath}/images/mobile.png" height="34" width="38" alt="mobile" />

I have found the solution finally.
What happens is when the code is deployed inside ROOT or the war deployed inside webapp and made as ROOT application by changing the context in tomcat server.xml
in either way tomcat is considering like the complete war contents is directly inside the tomcat webapp folder .
I was already having a folder named images directly inside webapps (for some other purpose) because of this the images folder was overridden . when i removed/renamed that folder the expected images (the images called from jsp) started appearing.

Related

Read Spring properties in React

I have a Spring/React webapp. In my application.properties file I defined spring.data.rest.base-path = /apiso when running the app locally, everything is accessible on localhost:8080/api. If I deploy this to my tomcat, the all the stuff goes to localhost:8080/warname/api.
I can easily define my warname in my properties file. And in React,
path: '/api/myStuff'
I can access my data.
Also I can change that to
path: '/warname/api/myStuff'
and everything will work. But to make things easier, it would be better to read the warname from my pom.xml so I wouldn't have to change every path in my .js. How to get that done?
If you have a directory named ROOT in your Tomcat directory, you have to remove it and change the name of your war to ROOT.war so that when Tomcat explodes the war it will be the main root project.

serving files from a spring boot war file

I've followed the guide here for turning a "hello, world" level Spring Boot app to a war file. I can run this war like a jar and it will return the simple template.
What I don't understand is why I can't access a main.css file I've created. I've placed it in the resources directory under "static/css/main.css" and according to the docs here Spring Boot will automatically server files under "resources", "static", "public", and "META-INF/resources". However, when I build my war file and run it I can't query those files in the browser (like http://localhost:8080/static/css/main.css). Have a missed a step? If I peek into the created war file I see the "static" directory in "WEBINF/classes" right beside the "templates" directory and the directory holding my application.
Files in src/main/resources/static are served from / so you don't need static in the path. You CSS file should be available from http://localhost:8080/css/main.css

Can't deploy site on tomcat on mac

I cannot figure out how to deploy a website (not app) on Tomcat on my mac. I just want to have Tomcat serve the file index.html. I tried creating the file myapp.xml in library/Tomcat/conf/Catalina/localhost with the content:
<Content path="/myapp" docBase="/Users/jerryk/Development/myapp" />
But when I attempt to access the site with
"http://localhost:8080/myapp" or "localhost:8080/myapp/index.html"
I get a resource not found error.
http://localhost:8080 brings up the Tomcat control panel.
Any thoughts on what I am doing wrong? FWIW, this works fine on windows.
Thanks,
Jerry
I'm not sure what's going on with Windows, but theoretically, the docBase must point either to a .war file, or a directory that has the same layout as a war file.
So, you can't just have an index.html in that directory, you also need at least a WEB-INF directory with a minimal web.xml in it.
See, for example, http://oreilly.com/java/archive/tomcat.html

ClassNotFoundException on Tomcat Server

I am using this tutorial to set up Tomcat Server. After I have put the HelloServlet.java in classes and Web.xml in the WEB-INF folder and I'm giving the command
localhost:9999/hello/sayhello
On the browser. I'm always getting ClassNotFoundException. If anyone can tell me where am I going wrong.
I'm using JDK1.6.0_30, and Tomcat7 for my sample application.
You need to put the compiled HelloServlet.class file (not the .java file) in the WEB-INF/classes folder.
Compile HelloServlet.java and place the output class file HelloServlet.class into
<TOMCAT_HOME>\webapps\hello\WEB-INF\classes\HelloServlet.class
One thing you should check is that, the HelloServlet.class should be in a package. Looks like you have a long way to go with Servlet and JSP... I recommend Head First Servlet and JSP for your reference.
In your 'WEB-INF' of 'classes' folder place the .class files and if your using JDBC, jsp's just Copy the .jar executable files into 'lib' folder. and make sure that xml file should contain the proper information.
you should follow below Web Application Directory Structure
WEB-INF/ --
web.xml --xml file
classes/ ---classes folder here we keep .class files Myservlet.class
lib/ ---lib folder here we keep all .jar files. Myapp.jar
Welcome.html
Welcome.jsp

Mapping a directory outside the web-app to URL in TOMCAT

I need to map an directory containing images which resides outside tomcat webapps folder, so that application can serve those images.
I am making a J2EE Web application running under tomcat 6. User can upload/delete images in the application. Currenly I store these images to a directory under application's WebContent folder, but I want to take it outside the tomcat (e.g. C:/test/images).
I need to know how to I configure tomcat so that if I access URL http://.com/images/abc.jpg , it serves the image from directory C:/test/images
Thanks,
Add a <Context> tag in server.xml, inside the <Host> tag:
<Context path="/images" docBase="C:/test/images/" />
Docs will be accessible at http://localhost:8080/images
in Tomcat8 you can also add PotsResources to you META-INF/context.xml as follow :
<Context>
<Resources allowLinking="false">
<PostResources readOnly="false"
className="org.apache.catalina.webresources.DirResourceSet"
base="path-to-your-local-folder"
webAppMount="/images"/>
</Resources>
...
</Context>
I had the same issue but found a solution.
If you are using Eclipse and a Tomcat plugin then please note that the Eclipse Tomcat plugin creates a separate CATALINA_BASE under the Eclipse workspace directory.
You can go to this location and you will find server.xml.
Use that server.xml and it will work.
My actual tomcat directory is:
C:\apache-tomcat-7.0.62x64\apache-tomcat-7.0.62\conf
and my Eclipse Tomcat server uses:
C:\workspace\JSF\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf
Use the workspace path and server.xml from this location.
Add this in server.xml inside the host tag:
< Context docBase="D:/personal" path="/images" />
and it will work if D:/personal has 1.png, and then the url http://localhost:8080/images/1.png will load the image.

Resources