• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Servlet and I/O

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sending from my servlet some data to a java-class, wo saves objects in an ObjectOutputStream. But Tomacat saves this textfile in tomcat_home/bin-folder. I want to save it in my webapp! How can I do this? What parameter can I put in a File file = new File(?); to do that?
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a web application it's undefined what the default directory is. Try using absolute paths instead, e.g. ServletContext.getRealPath("/WEB-INF/my-file.txt").
 
Ina Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, it didn't work! I tried this as well, but it didn't work either:

File f = new File("ServletContext.getRealPath()/" + "my.txt");

Ina
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
File f = new File("ServletContext.getRealPath()/" + "my.txt");

That's because there are quotes around ServletContext.getRealPath()/, so it takes that as a literal string. Try this:

File f = new File(ServletContext.getRealPath() + File.separator + "my.txt");
 
Ina Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your proposals! But it didn't work either! Then I tried this, but I couldn't solve my problem

File f = new File("ServletContext.getRealPath()" + "/" + "my.txt");

Is ServletContext.getRealPath() the right solution?
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are still placing a method call in double quotes. Remove the quotes around the first part of your statement. Putting those quotes there means you are inserting what is between those quotes into the string exactly as you see it in the code. It is not being executed, and you need it to execute.

[ July 24, 2006: Message edited by: Martin Simons ]
[ July 24, 2006: Message edited by: Martin Simons ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did u get proper answer?
 
Ina Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't work either! I gave it up! I don't how to save my textfile in my webapp! I think the problem is, that it's not a servlet, but a "normal" Java-Class, which saves my objects in a textfile!
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should have a look at our FAQ on How To Ask Questions On JavaRanch. You are leaving out valuable information that we need to know to help you. You state "it didn't work" but you don't tell us what didn't work. Did it compile? Did it throw an exception? What did the server log files say? Did the file get written, just not where you think?
My guess is that the line above did not compile. getRealPath() is not a static method so it cannot be invoked on the class. If you are attempting to invoke this method from a non-Servlet class, you will have to pass an instance of ServletContext along or resolve the path in the servlet and pass that into your non-Servlet class.
 
Ina Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry that my explanation wasn't exact, but my writing english skills aren't as good as my comprehensive skills. Thank for helping and I hope I got the important idea of your How To Ask Questions On JavaRanch.
You are right saying getRealPath() is not a static method so it cannot be invoked on the class.

But I don't no to do that:
If you are attempting to invoke this method from a non-Servlet class, you will have to pass an instance of ServletContext along or resolve the path in the servlet and pass that into your non-Servlet class. How can I get an instance of ServletContext? As far as I know it is a Interface?

For better understanding my problem, I'm sending my jsp, my servlet and my i/o class (I'm sorry there's some german in it, but I think you can understand my code):

my jsp



the doGet()-methode of my servlet:



this is my i/o-class, where I want to save my Object in an ObjectOutputStream:

 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ina Thomas:
I'm sorry that my explanation wasn't exact, but my writing english skills aren't as good as my comprehensive skills.


Nothing to be sorry about. That's why we have the FAQ. Get you pointed in the right direction.

Originally posted by Ina Thomas:
How can I get an instance of ServletContext? As far as I know it is a Interface?


Tomcat implements ServletContext and makes that class available to you. Have a look at the javadoc for ServletContext and you should see how.
 
Ina Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I'm completely lost with this part of the API, so my question is: Is the topic implicit object one step in the direction to understand this much better?
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is the topic implicit object one step in the direction to understand this much better?


I don't know what you mean by "topic implicit object"
 
Ina Thomas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was just an idea! I was looking for a solution and doing that I found implicit objects and I thought this could be a solution. But now I'm not sure by that! I also tried it with the following in the web.xml

<context-param>
<param-name>texts</param-name>
<param-value>/WEB-INF</param-value>
</context-param>

You can get the value in a jsp like that

getServletContext().getInitParameter("texts"));

but that's not possible in a normal java-class!
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So change the speichern method to take two arguments, the product and the path. Then you can use the servlet to get the configuration parameter or the real path and pass it on.
[ July 27, 2006: Message edited by: Joe Ess ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic