Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Read in text file from JSP page  RSS feed

 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey All -

The project that I am currently working on requires that I load some values from a few textfiles. I've gotten it working with scanners, using request.getRealPath("/"), but it is deprecated and from this thread http://www.coderanch.com/t/288744/JSP/java/getRealPath-Depricated it doesn't appear as if that is the correct way to go about it, since when I deploy in a .war, the request.getRealPath() method will return null.

Does anyone have any guidance on how I might be able to do it correctly, in a way that will work fine with a .war? For refrerence, the "parent" JSP page is in a folder called "Survey_1" and the text files that need to be read in are in a child folder of "Survey_1" called "Practice", so the relative path would be "practice/filename". Using the normal File constructor to pass in that doesn't work for some reason.

Thanks for any help!
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.getResourceAsStream() should be what you are looking for...
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote:request.getResourceAsStream() should be what you are looking for...


Alright, Eclipse is claiming that getResourceAsStream() is not defined for the request object. Some googling revealed that it is a method of ServletContext objects though.

Somehow I've managed to avoid this in my JSP career, so does anyone mind telling me how to get the current ServletContext so I can use this getResourceAsStream method?

Thanks!
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your JSP, the ServletContext is available via the 'application' implicit object.

However, it's considered bad practice to do that kind of stuff in a JSP.
 
Sam Gardner
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sebastian Janisch wrote: However, it's considered bad practice to do that kind of stuff in a JSP.


What would a better practice be?

I need to read the words specified in the files into a Vector or array for display, so I need to specify the location of those files somewhere.

Wouldn't that necessitate getting the context path at a minimum? From my understanding, I can't use the request implicit object since it will return null if deployed in a .war.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best practice is to put your business logic in a servlet, that is where you read your file, parse, translate or do whatever you might find is helpful with it.

Then forward the results to the corresponding JSP. JSPs are strictly for presentation. You strive to separate logic from presentation.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66188
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy to recognize the separation. If you have the urge to put Java code in your JSP, it's wrong.

Modern JSPs should contain no Java code.

Dealing with legacy JPSs means that you need to balance what belongs in a controller, and what belongs on a page already chock full of Java goop.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!