aspose file tools*
The moose likes Servlets and the fly likes Problem with servlet running on linux. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with servlet running on linux." Watch "Problem with servlet running on linux." New topic
Author

Problem with servlet running on linux.

George Kalfas
Greenhorn

Joined: Aug 05, 2004
Posts: 22
Greetings to everybody!

I don't know whether this is the right place to post this topic since it concerns both servlets and j2me. If this post does not belong here please place it where it does!

I've recently began developing j2me applications, and lastly I've written a Midlet that transmits a png image file to a server.
The server side software is based on apache tomcat 6.
I've installed tomcat on two pcs. One laptop with windows where I do my developing, and one linux machine (ubuntu to be exact) with static ip which is designated to be the server when my project ends. I've placed the same web.xml,context.xml,server.xml and tomcat-users.xml to both servers so I assume that they are configured exactly the same.

I've written a very simple servlet that receives the Image through an input stream. The code is the following :



When I execute my Midlet through the Wireless Toolkit emulator and I target my request to the tomcat that runs on localhost (the windows machine) the application works perfectly.

I've transfered this servlet to the linux machine, changed the following code to recompiled it there and tried it out, but the servlet now does not work.
A image.png file is created but it is empty. After running some tests I discovered that this loop
is never entered when it runs on the linux machine. Can anyone figure out why the InputStream receives nothing when ran on the pc with the static ip, but runs perfectly on the localhost? The Mildet is the same on both occasions.

Any help would be greatly appreciated! I hope you haven't slept half way through this post!

Thank you a lot and in advance for your reply!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410



You haven't qualified the path to the image.
With web applications, you need to be very specific about the location of files on the server. There is no guarantee as to where the current working directory is going to be. (It will be where the user that started the container was when it was started; which is not very predictable from within the web app itself; especially if the program is a daemon that was kicked off on startup.)


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Why cant we use relative paths rather than using absolute paths?

If we have the images in <webapp_name>/images/image.png then we can use image/image.png in the servlet
or
Use the ServletContext's method getRealPath() passing it /images/image.png
(path relative to webapp context) to get the absolute path of that resource on the filesystem, the path returned depends on your OS and needs no special consideration in your servlet code.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Amol,
George is talking about finding a file on the webserver's file system.
Not about creating a link to something that will be accessed by the user's browser.

I don't want this discussion to stray from the original poster's question.
If you aren't sure about something, please feel free to start your own.
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Ben what i am trying to say is,
The problem he has is because of the hard coding the filepath.
If he knows the path relative to the webapp then he can get the
path on the filesystem of the webserver by calling getRealPath.

Use that path as an argument to the java.io.File 's constructor.

Wont that work?
Amol Nayak
Ranch Hand

Joined: Oct 26, 2006
Posts: 218
Ok, Ben i got it.. .

As for this problem, can we use some property file which will specify the
directory where we write the files.

The property needs to be changed on different OS.
Will this be ok?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Amol Nayak:
If he knows the path relative to the webapp then he can get the
path on the filesystem of the webserver by calling getRealPath.


One thing to keep in mind when using getRealPath is that Java web apps can but don't necessarily mirror the server's actual file system. If your app was deployed from a file system directory, or if your container was configured to unpack war files when the apps are deployed, then getRealPath will return the location of a resource within your web application. If on the other hand, your app was deployed from a war file and not unpacked, then getRealPath will return null.

Writing code that relies on getRealPath makes your application less portable because it will depend on being deployed in a particular container; with particular settings.

If you know your file resides within the web application's directory structure, use getResourceAsStream. If not, then, yes you're better off configuring the path to your files in a properties file or as a context init param.
George Kalfas
Greenhorn

Joined: Aug 05, 2004
Posts: 22
First of all, I would like to thank you all for your so quick responses to my problem!

Ben, I tried the solution you advised(set the absolute path), but unfortunately it didn't solve my problem, expept from the fact that now the file image.png is created in a different folder, but is still null...

Now I'm a new to this but I think that the source of the problem is (as I mentioned in my original post) that the following loop
while ((len = in.read(buf)) > 0)
{
bos.write(buf, 0, len);
}
is never entered. This means that in.read(buf) returns 0 , which in turn means that no data is read, right?

Is there any reason why the InputStream does not receive anything on the remote machine, but receives normally on the localhost? Does it have anything to do with the fact that it is remote? Or is there a difference between Windows tomcat and Linux tomcat, regarding the configuration? Or is there some sort of problem with the permissions?

Thank you all very much! I hope I have not tired you much!

PS: The catalina.out file does not provide information about an error or something similar...
PS2: If this question does not violate any forum rule can you recommend me of a good book on servlets? I am a J2me developer my self but it seems that I have to learn more about servlets now... Thank you!

[ August 16, 2007: Message edited by: George Kalfas ]
[ August 16, 2007: Message edited by: George Kalfas ]
rajesh bala
Ranch Hand

Joined: Jan 14, 2003
Posts: 66
I would try to rather wrap the inputstream to some sort of bufferedInputstream or bufferedReader and check for in.available() instead of in.read
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is there any reason why the InputStream does not receive anything on the remote machine, but receives normally on the localhost?


Just to get the obvious stuff out of the way...
Does the file actually exist on the Linux box or are you trying to read a file on the client's machine?
George Kalfas
Greenhorn

Joined: Aug 05, 2004
Posts: 22
Dear all!

First of all I have used Rajesh's advice and created a bufferedInputStream, which in turn with the use of tha availiable() method returns zerol.So it says that the InputStream does not provide any bytes what so ever.

And to answer your question Ben the file does not exist on the linux box.
I'm trying to upload an Image from a mobile device to the server(linux box). The file is created on the server (which means that my wireless toolkit emulator communicates with the remote machine), but the file does not contain any data ( which means that although the server receives the request, it does not receive any bytes).

In case this helps this is my midlet code:

[ August 18, 2007: Message edited by: George Kalfas ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with servlet running on linux.