I want to access file (for example from http://localhost:8080/myweb/styles.css), but i dont want to write servlet for each static file on my web. Is there any way how to access that file without servlet? I am using tomcat 7.
You do not have to write servlet for this. All files located in root of your web application are accessible via HTTP.
For example if your web application has only one file styles.css, i.e. you have folder myweb that contains this file under $TOMCAT_HOME/webapps it is accessible automatically.
Related
I have one webapp (abc.war) deployed on tomcat and there is an image folder , under this several images lying and I can access these images by URL: xx.xx/abc/image/hello.jpg
This project is running live but I am not able to get how these images are retrieved.
Now I have another REST based project deployed in tomcat and i want similar behaviour e.g
xx.xx/hellorest/images/anil.jpg, but right now I am not able to access using above URL.
Need help how this can possible. I don't want to use any Servlet to read and write the Image.
First, /WEB-INF is not accessible from the outside.
Second, if you use /images/temp.jpg, the browser will load images from the root of the web server, and not from the root of your web-app (which is deployed under a context path).
Use the JSTL tag to generate absolute paths from the root of the web-app:
" alt=.../>
My project is an EJB. I want to store image in the oracle database. I understand its more efficient to store the file path in database and the images in a folder. My folder "images" is located in ejb. Please if the image is java.jpg, how do I construct the file path? I have tried both images/java.jpg and images\java.jpg and none worked.
The real path to the image location is "C:\Users\Kate\Documents\NetBeansProjects\Kate_Assig\Kate_Assig-ejb\images" and "Kate_Assig-ejb" is the ejb project and that's where the images folder that contains the images is.
The challenge I have right now is to store the file path in database. I have tried this option "..\images\java.jpg" and it didn't work. It didn't work because images are not displayed when I query the table from the client side.
The client is web application.
Let say that you stored image in some directory and its path in database and you have just JEB module, though you want to access images over web using URL. What you need to understand is that EJB does not provide web access. You have to create web module as well and configure it to serve your images.
There shall be servlet that accepts image identifier in parameter or in path. It will then load path from database, open the file and return it back. If you store the file in some public place within web module (not under WEB-INF) then you do not need to use database at all. The parameter / path could be constructed that it holds enough information to find and serve it.
There is some tutorial on web module by Oracle: http://docs.oracle.com/javaee/5/tutorial/doc/bnadx.html Basicly you need to create a war file with following structure:
index.jsp
WEB-INF\
classes\
lib\
web.xml
Directory WEB-INF is protected and browser cannot access it. It contains classes directory to store web module classes, lib folder for dependencies (ejb module jar, libraries) and web.xml. This file is important as it defines your servlets, filters and URL mappings.
You can study following samples to implement your solution that will serve images stored on filesystem:
http://www.java2s.com/Code/Java/Servlets/Sendworddocument.htm
http://www.javatpoint.com/example-to-display-image-using-servlet
I hope that this will help you to proceed.
I'm maintaining a legacy Java servlet webapp ( nwp ). My goal is to learn Spring and gradually update the webapp to use Spring as much as possible.
The servlet webapp, nwp, now runs on WebLogic 9.2. It is packaged and deployed as nwp.war. Every HTTP Request gets submitted to a unique servlet, which process the request and prints out a web page/screen. Each servlet will read in various resource files from a remote location outside of nwp.war to use for headers, footers, etc.
Yes, it is primative, which is why I want to update it. It also made sense to have the "include files" in a remote location outside of the war as 3 applications use those files. However, as part of updating the nwp app I plan on consolidating the other two ( similarly primative ) apps into just the nwp. Eventually.
As a first step in converting this application to Spring I have rearranged the directory tree to have these subdirectories under the WEB-INF dir:
images
js
css
The servlet generated HTML references images as
"
My problem is that right now the servlet generated HTML can not find images in the WEB-INF/images directory inside of the nwp.war.
Right now, the nwp.war file contains a file called weblogic.xml to map the URLs for images to where they sit on the server:
<wls:virtual-directory-mapping>
<wls:local-path>/common/resources/images</wls:local-path>
<wls:url-pattern>/images/*</wls:url-pattern>
<wls:url-pattern>*.jpg</wls:url-pattern>
<wls:url-pattern>*.gif</wls:url-pattern>
</wls:virtual-directory-mapping>
I'm new to WebLogic and WebLogic 9.2.
I've tried changing that mapping in a number of was so that the servlet generated HTML will look for the pictures in the WEB-INF/images directory inside of the war.
Is this (servlet generated html finding images ) even possible or am I going to have to use the current system of getting images until I can convert the servlets into JSPs?
Thanks
Steve
The HTML won't be able to directly reference images inside the WEB-INF folder. This is for security. So you have 2 options:
Move the images so that they're directly under / rather than /WEB-INF/
Create another servlet to serve those images
If you decide to use a servlet, you can use ServletContext getResourceAsStream to access images from the /WEB-INF/images directory. For example:
servletContext.getResourceAsStream("/WEB-INF/images/test.jpg");
I would like to upload files to my the server/default/data directory using a simple upload servlet and be able to access them from within my application, but I can't seem to find how to access it. I tried
getServletContext().getRealPath("data");
but that didn't help.
to get the absolute path, just do:
System.getProperty("jboss.server.data.dir");
I have a GWT app. Am deploying it in tomcat. Within the servlet i want to write some code to create a temporary file. Right now am using
file = new File("./../webapps"+this.getThreadLocalRequest().getContextPath()+"/svg/temp/"+svgName);
But this might not hold true for all web servers due to the 'webapps' hardcoding thing. Please help me out as of how can i solve this problem.
ps: 1) The servlet extends RemoteServiceServlet.
2) "/svg/temp/"+svgName is my app specific.
Prior to the Servlets 2.2 API, there was no standard location for temporary paths. The 2.2 API adds the javax.servlet.context.tmpdir attribute to the servlet context that defines where to write something:
File directory = (File)getServletContext().getAttribute("javax.servlet.context.tmpdir");