Servlet load picture/jpg won't work - java

I have xampp installed. With it, I got the tomcat required for java servlets to work. C:\xampp\tomcat\webapps\SavedFiles. This is the path for tomcat, considering it is installed with the xampp.
I have the following :
D:\School\Uni\S1\WEB\UploadFileToServer\out\artifacts\UploadFileToServer_war_exploded this is the output directory for my artifacts (I use intellij, if this matters).
SavedFiles contains: 1.jpg, 2.jpg, picture.bmp.
I know that I need to add a context and a path or something like that, so that I can display images stored on my computer but I am lost here.
I have images in SavedFiles.
What do I need to add to the servlet or tomcat server.xml so that I can display the images like this <img src='SavedFiles/1.jpg'>?

It seems like the problem was that I used the path instead of getRealPath("/"). This solved the problem for me. Maybe this will help someone in future as well.

Related

Issues With Tomcat 8

I am newbie in Tomcat(8) and having some issues with Tomcat. i want it to display the default Tomcat homepage like the way it was when i installed it for the first time. I am running/compiling some maven project in netbeans and it seems they are automatically deployed to Tomcat.
I have looked into the folders of Tomcat i can't see the app anywhere but its "index.html" file displays when i launch "localhost" . This makes the index.html file of the maven application displays as the default localhost page.
At the moment i want the localhost to display the default Tomcat page like it was when i first installed it.
I have changed my "C:\servers\tomcat8\conf\Catalina\localhost\ROOT.xml" file to the following but it is not working::
<?xml version="1.0" encoding="UTF-8"?>
<!--Context antiJARLocking="true" docBase="C:\projects\spring\PersistenceWithHibernate\target\PersistenceWithHibernate-1.0-SNAPSHOT" path="" /-->
<Context antiJARLocking="true" docBase="C:\servers\tomcat8\webapps\examples" path=""/>
I have pointed the docBase to docBase="C:\servers\tomcat8\webapps\manager" and comment out the one that is displaying the index.html page,
yet its not working
How do i fix it?
I am not certain if i understand what you are asking for, could you clarify the question up a bit?
But when you do a clean tomcat install, the default directory where the projects are deployed at, is under the tomcat install folder ../tomcat/webapps/ROOT.
The manager part is a tool to manage the tomcat, you sohuldn't need to configure it's docbase.
Check http://tomcat.apache.org/tomcat-8.0-doc/ for reference. At least the few first chapters, its all explained.
It's hard to say what (many things) you did to get to where you are. If you unzip a default tomcat installation next to your current installation and compare the directories, you'll be able to spot the differences.
As this is probably not what you want to hear: The default tomcat installation has the default web application in webapps/ROOT (because there needs to be an encoding for the "empty" context name, answering on www.example.com/
Restoring the default webapp might be as simple as restoring the original ROOT.xml, remove that file, restore webapps/ROOT or anything in combination.
Frankly, your problem description "but it is not working" doesn't really help. If this answer doesn't solve your problem, you might want to give more detailed description than that. What do you actually see. But then, you might as well state what differences you find between the original and your installation.

Why can't I create a folder in %CATALINA_TMPDIR%?

This is the weirdest thing ever. I'm working on an app that I run using Tomcat though IntelliJ IDE. Part of this app creates a folder in the configured %CATALINA_TMPDIR%. For some reason no matter where I point %CATALINA_TMPDIR% to java can't create a folder there, but it can create a folder everywhere else.
For example, If
CATALINA_TMPDIR=C:\Users\dallend\.IntelliJIdea14\system\tomcat\Unnamed_datasite_7\temp then my app can't create a folder there, but can create it in C:\Temp, a folder I created.
Then if I change to CATALINA_TMPDIR=C:\Temp, the app won't be able to create a directory there but will be able to create one at C:\SomewhereElse.
From debugging through things it appears that java.io.File tries to create the directory but that the WinNTFileSystem.public native boolean createDirectory() method can't successfully create it.
Why might this happen? Could there be something I need to tweak with IntelliJ, or Java, or my OS? Has anyone encountered this before?
EDIT: I've tried running Tomcat stand-alone, and the problem persists.
Try to use forward slashes in your directory names. I've pulled out a lot of hair with backslashes actually escaping a path, e.g. C:\temp pointing to C:<tab>emp. I've completely given up on using backslashes even on Windows: Java will translate forward slashes just fine: C:/temp/

HTTP 404 when trying to run a jsp file in eclipse

I get 404 error when trying to run a jsp file eclipse. The confusing thing is the server is running and I can go to localhost:8080 without any problems. I have attached an image of my configuration of server, the jsp file and also the location of the file and the error.
I embed the image cause of the reputation thing so I have linked it
More information:
I am using TOMCAT 7, ECLIPSE KEPLER I have checked the location of helloworld.jsp its in WebContent. I have restarted the server. I have created another dynamic web project nothing works. I get the same problem I can go to localhost:8080 but I cant run an JSP file
SOLVED:
Okay guys its pretty simple to solve this problem. There are two things you need to try.
1)Right click your project and export the war file into C:\ProgramFiles\Tomcat7 folder
If this does not work then.
2) Right click your project and export the war file into some directory and copy the war file into C:\ProgramFiles\Tomcat7 then start the Tomcat server and there you go your JSP file work.
You will probably end up doing step 2 because of stupid windows file permissions
Also thanks for all the help guys really appreciate it.
Make sure that u have put your Jsp files into the WebRoot and check the location and even check your Web.xml and after that run and still if its not working then post your Web.xml here.
Unfortunately, user3135774's solution does not work for me, as am using an Apple computer.
I have read and researched another problem (Error 500: No Output Folder), which I have along with Error 404, carefully, and I think this is related to permission issues.
The JSP file is supposed to be compiled and then deployed under Tomcat/work/Catalina/localhost/YourProjectName. The 404 error message means that there is no files in that directory. Since Eclipse has no write permission to this directory, you have to change the access permissions. After changing the permission to 777, there we have the compiled .java and .class files.
Since you do not have any of them
index.html/index.htm/index.jsp/default.html/default.htm/default.jsp page
To run your jsp page :
In Eclipse:
=>goto Java Resource
=>right click on your jsp page
=>in that select Run as Server.
After applying everythings, it did not work for me to read image file in eclips.
Finally, It works! after that I change deploy path.
Server Locations>Deploy path(Set with "Workspace" of Eclipse)

How to reference a DLL file in Jelastic Tomcat

I have successfully developed and deployed a java servlet in a tomcat server.
But in that servlet i call a dll file. In my computer that i run the tomcat server and i call the library
i use System.load("C:/java/src/calldllfile.dll"); and it works fine .
The problem is that in jelastic tomcat i don't really know what path i should use and where i should place my dll file for my servlet to call... and i catch in my code with a try it returns a UnsatisfiedLinkError
so it really must be the path ...
Following a similar answer in jelastic forum that is on comments below
i tried to set my file into home folder and call it with
System.loadLibrary(System.getProperty("user.home")+"/"+"calldllfile.dll");
but it didnt work.
So i don't really know what to do ... i include below an image of my deployed servlet with the location of the dll library that i cannot call. It would be just really helpfull if someone tell me what path i should use in System.load("?????");. Thanks
Folders webapps and home are located on the same level and the root folder for them is /opt/tomcat/ (equivalent of environment variable CATALINA.HOME - portion of docs). If you are talking about WEB-INF folder which is definitely located under your context (ROOT for example), to get it you need to use /opt/tomcat/webapps/ROOT/WEB-INF (or System.getProperty("catalina.home")+ "webapps/ROOT/WEB-INF/").
Concerning .dll libs you can check this
I know this is an old thread, but the answer can be useful for someone.
To access the home folder from a Tomcat installation you have to use the real path /opt/tomcat/
This way you can access your dll with the path /opt/tomcat/temp/calldllfile.dll

Running servlet program

I have recently installed Tomcat 6.0.29 on my Ubuntu 10.04 system. Process till the localhost connectivity is fine but when I tried to run a simple hello program, it giving me the following error:
type Status report
message
/opt/apache-tomcat-6.0.29/webapps/examples/WEB-INF/classes/HelloWorldExample
description The requested resource
(/opt/apache-tomcat-6.0.29/webapps/examples/WEB-INF/classes/HelloWorldExample)
is not available.
I have tried to reinstall tomcat by the different methods give on other sites but the result is same. I am not yet sure where I am going wrong. Please can anyone describe me the correct method of running a servlet program ie, where to store the actual Java and class files.
Have you got a correct web.xml file? It needs servlet and servlet-mapping tags.
Check that WAR file from which you deployed your web-app is not corrupted.
always put you class in package, eg, not HelloWorldExample, but foo.HelloWorldExample.
Tomcat (and other containers as well) doesn't like default packages.

Categories

Resources