Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Path when creating a new file

 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am trying to create a new file using:



The file is in WebPages when I debug over the upper line of code I get a null I need to put the source file in the app in order to be able to create the file?

Any idea, please?

Regards, Isaac
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to read a file that's part of your web pages? This folder should probably be used for templates, images, scripts, style sheets, etc.

Instead of reading them, let the web application framework deal with them. If you need to perform special operations using a specific file, it probably shouldn't be in the web pages folder.

Be more detailed what and especially WHY you want to achieve this. Please remember this for future questions.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the Servlets forum because I assume that's the environment that we're talking about -- which is indeed very different from run-of-the-mill file writing.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment I am testing a web written in servlets it has to read a file and compare the content with a given value. Then when I am working in local using an absolute path it works fine but I need to create a .war and upload it to a server for run it there. So I need a relative path in my app to the location of the file in the app itself

Any idea, please?
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what kind of file is this? Is it content that should be publicly visible from your web application? Is it internal to your application? Can the file be changed by the application?

Like I said, your web pages folder should probably only contain content that is presented to the outside world. If the file is only used internally, you can make an extra source folder named something like "resources", and then use the Class.getResourceAsStream() approach.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you reading, or writing? You should never write files into the web app at run time. Reading with getResourceAsStream() is fine and can be read from anywhere within the web context.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a file for read from it it is used inside the app the content is not shown out.


This is the code I m using now, "fileToAnalyse" is a folder inside Web Pages folder. The file is a .rtf

Any idea, please?

 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the documentation for Class.getResourceAsStream()? Have you listened to what we told you about using the web pages folder for internal resources?
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have checked the doku. No I havent listened about what you told.... but I have read it.

I have implemented this class:


and I located it at "Other Sources -> src/main/resources" ->config.config.properties

the error is like this:


Any idea, please?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ServletContext.getResourceAsStream() would be a better choice.
 
Stephan van Hulst
Bartender
Pie
Posts: 6503
83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your project a Maven project?
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using



This part doesnt compile:



Error:


Yes it is a Maven project.

Any idea, please?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your statement inside a servlet?
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need it in an external independent file I tries in a Servlet but it got messy

Another idea, please??
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course you cannot call a servlet method in a class that's not a servlet.

You will need to do the path resolution in a servlet. You can then pass that to any other class.

Or, you could try using the Class.getResourceAsStream() method, but then the file needs to be in the classpath, not anywhere else in the web context.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should absolutely positively never attempt to create, modify or delete a file in a WAR. Even when it works, it's trouble waiting to happen. Nor should you assume that there's such a thing as a "current directory" to create files in when you're running in a webapp.

For permanent files, use an absolute path that's external to both your WAR and to your webapp server. For temporary files, use the java.io.File tempfile facility.

You can safely read files inside a WAR, however, if the WAR is a true WAR (file), and not an exploded (unzipped) WAR, the attempt to get the file's path will fail, since objects inside a ZIP/JAR file don't have filesystem paths. The better way to read such resources is via the J2EE request getResource and getResourceAsStream methods.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written this code inside the Servlet:



The value of the "path" variable, should be available for the whole app. Is that possible having into account the other classes are not Servlet and then the request is not available.

Any idea, please?
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this, instead:



Although actually having a "src" folder within a webapp - or actually for any executable app - is highly questionable.

If you're using Maven as a build tool, Maven would automatically copy config.properties from the project's src/main/resources folder into the WAR's WEB-INF folder or some place like that, so the more realistic coding would be more like
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This part doesnt compile





Any idea, please?



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65339
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The request variable isn't declared.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is passed using a Servlet like this:



Any idea, please?
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My idea would be to look at all of your code instead of just isolated lines of code. For example look at the method which contains the line of code which has the error message. My guess is that it isn't the method whose signature you just posted -- but since we can't see it, that's a guess. Post the code if you want us to debug it.
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a slight aside, processRequest implies you're doing the same thing for a GET and a POST.
That's almost never correct.

I know some IDEs (Netbeans?) use a template for a servlet that adds this method, but it's not correct.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
I know some IDEs (Netbeans?) use a template for a servlet that adds this method, but it's not correct.

Yes, it's NetBeans, and whoever designed this abomination should be dragged into the street and beaten severely about the head.

In this thread I provided an improved servlet template. It's easy to change.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a more complete piece of code:


Any more idea, please?
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your src directory deployed with your webapp?

Actually, I see this has already been asked, but I can't see an answer?
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could be possible to use a relative path instead of this:



for the moment just for testing?

Yes the src is in the webapp.

Any idea, please?
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, where exactly is the src directory located?
If it's not on the webapp classpath (which does not include the webapp directory, but only the WEB-INF/classes and the jar files in lib, possibly WEB-INF itself?) then getResourceAsStream won't pick it up.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed it like this:

Over red line wit message cannot find symbol


Any idea, please?
Screen Shot 2015-12-17 at 16.51.54.png
[Thumbnail for Screen Shot 2015-12-17 at 16.51.54.png]
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's your project structure.
Where, in the web app structure (the bits deployed on the server) is the file located?
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case as the file is very small for testing purposes it is in the same location when the .war is created
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The J2EE getResource methods do NOT take relative classpaths. The proper form is "getResourceAsStream("/dir/dir/dir/file"). Relative paths are meaningless/dangerous in webapps, since the J2EE standard does not recognize the concept of a "current/woking directory"/

The "absolute" filesystem root ("/") is always the root of the WAR.

You cannot reach up outside of the root of the WAR to obtain files or resources. If you could, that would be a major security loophole and Bad Guys would be pwning servers left and right. So the spec forbids it.
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isaac Ferguson wrote:In this case as the file is very small for testing purposes it is in the same location when the .war is created


So, using a Tomcat server as an example, it's located:
webapps/yourapp/src/etc etc
?

Which is still outside of the classpath of the webapp anyway.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I just changed it to an external path, like this:



Obviously now the

doesnt makes sense anymore.

I havent tried it on the server because it doesnt compile so I cant generate the .dist

Any idea, please?
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THAT is not an "external path". It's an application-absolute path rooted at the top of the WAR. Re-read what I just said.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This would the external path isnt it?
 
Dave Tolls
Ranch Hand
Posts: 2110
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:THAT is not an "external path". It's an application-absolute path rooted at the top of the WAR. Re-read what I just said.


Hang on.
Am I getting confused here (or out of date possibly), which getResourceAsStream are we talking about?
There isn't one on HttpServletRequest is there?
I was (my fault) assuming this was the class/class loader one.
 
Isaac Ferguson
Ranch Hand
Posts: 1035
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is this one HttpServletRequest:



Any idea, to make it compile?
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:
Tim Holloway wrote:THAT is not an "external path". It's an application-absolute path rooted at the top of the WAR. Re-read what I just said.


Hang on.
Am I getting confused here (or out of date possibly), which getResourceAsStream are we talking about?
There isn't one on HttpServletRequest is there?
I was (my fault) assuming this was the class/class loader one.


No. It's not the classloader path. It's the path relative to the root of the WAR. Technically speaking, resources retrieved via classloader are done in the form "dir.dir.file", not "/dir/dir/file". Classloaders, in fact, can retrieve resources from places that never existed as files or directories at all.

Think of the WAR as being its own virtual filesystem completely distinct from the OS filesystem of the server that the webapp is deployed to.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the biggest mistake being made here is that the IDE is being considered as an integral part of the web application.

The IDE shouldn't even be installed on a production machine, much less part of the resource context.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic