• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving the path

 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get the context path so I can access a file using the File class. If I add this:

<%= getServletContext().getResource("/").getPath() %>

It prints /localhost/ which am I right to assume is the start of the path to my file, which is in ROOT/img/random/? So would be /localhost/img/random/?

If I try to assign the String to a reference I get a MalformedURL exception.

<%! String path = getServletContext().getResource("/").getPath(); %>

Why would it do this?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The context path and the physical file path on your system have nothing to do with each other. To obtain the context path, use getContextPath() on the request.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. So which path do I need to access files in the classpath? The book I have isn't very clear on this point.

I added a line in the jsp page to print out what request.getContextPath() returns but it printed nothing.

Why does this line in the jsp generates a compiler error(request cannot be resolved): <%! String s = request.getContextPath(); %> but this line compiles fine: <%=request.getContextPath() %>

[ December 30, 2006: Message edited by: David McCombs ]
[ December 30, 2006: Message edited by: David McCombs ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand the implications of using <%! over the <% notation?

Therein lies the answer to your question.

And neither of the discussed paths are really appropriate for classpath issues. What exactly are you trying to do?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David McCombs:
Why does this line in the jsp generates a compiler error(request cannot be resolved): <%! String s = request.getContextPath(); %>
You put an "!" in there, as you'll see, and that means you're declaring an instance variable for the class that the JSP represents. And when you declare an instance variable, you don't have access to the parameters of the method where your JSP code lives, and "request" is one of those parameters.

Of course all this requires you to understand how JSP code is converted into a Java class, which is really more than JSP coders should be made to understand. So I'd advise putting such code into a servlet, where it's clear what goes where.

And I'd also advise you not to try accessing files in your web context. In some servlet containers you don't even have files to access, the whole application is run out of a jar file. If you want to read from something in your web context, then get an InputStream something like this:
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Do you understand the implications of using <%! over the <% notation?

Therein lies the answer to your question.

And neither of the discussed paths are really appropriate for classpath issues. What exactly are you trying to do?


<%! is used for declaring variables, which is exactly what I am trying to do. How is what I posted different from <%! int val=0; %>?

What I am trying to do is read a folder full of gif files into a File, and randomly pick one. I got it to work by putting this code in the jsp, but only on my windows box, it doesn't work on my BSD server, so I need a platform independent way to do it.

//note that <%! works just fine
<%! File file = new File("webapps/ROOT/img/random/"); %>//this only works on my machine
<%! Random rand = new Random(System.currentTimeMillis()); %>
<%! String[] pics = file.list(); %>

...

<img src="img/random/<%= pics[rand.nextInt(pics.length)] %>" alt="fail" />

What I would like to do is move this to a .class file under classes, but have run into a path issue where no matter where img/random/ is the class can't open a File.
[ December 30, 2006: Message edited by: David McCombs ]
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Clapham:
You put an "!" in there, as you'll see, and that means you're declaring an instance variable for the class that the JSP represents. And when you declare an instance variable, you don't have access to the parameters of the method where your JSP code lives, and "request" is one of those parameters.

Of course all this requires you to understand how JSP code is converted into a Java class, which is really more than JSP coders should be made to understand. So I'd advise putting such code into a servlet, where it's clear what goes where.

And I'd also advise you not to try accessing files in your web context. In some servlet containers you don't even have files to access, the whole application is run out of a jar file. If you want to read from something in your web context, then get an InputStream something like this:


Thank you, that clears it up a bit. How would an InputStream work since the name of the file read isn't known until someone requests my page, and still needs to read the entire file contents?

So I should also run a servlet file to get the name of the image to display? It still has the problem of being able to access the file, which will be several levels above the servlet file.

I tried to implement Ben Southers idea of a StreamServlet, but couldn't get it to work on a random image. It seems like there should be a simple solution, but the book I have is a pretty poor tutorial(HF Servlets & JSP), although I suppose it really isn't meant to be one.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David McCombs:

<%! is used for declaring variables,


Not quite. Did you read Pauls' reply? <%! creates class-level declarations. Implicit variables such as request are only available in the service method.

I always recommend that JSPs be written without scriptlets of any type, but if you are going to use scriptlets avoid <%! unless you really understand its implications.

With regards to files, as long as you are running the web application out of the file system (and not from an unexpanded war file), you can use the getRealPath() of ServletContext to get the absolute file path of any file relative to the app context root
[ December 30, 2006: Message edited by: Bear Bibeault ]
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it working both on my machine and my server. Thank you to both for your explanations and patience. I think I finally understand the declaration element. I haven't struggled this much since I first started learning this crazy discipline. Looking over the compiled servlet also helped me figure out what was going on.

This is what I did. I realize that it is extremely poor practice to have this much java code in a jsp, but I will be moving the code to its own .class file.


<%! String path; %>
<% path=getServletContext().getRealPath("img/random/"); %>
<%! File file; %>
<% file = new File(path); %>
<%= file.exists() %>
<%! Random rand; %>
<% rand = new Random(System.currentTimeMillis()); %>
<%! String[] pics; %>
<% pics = file.list(); %>

...

<img src="img/random/<%= pics[rand.nextInt(pics.length)] %>" alt="fail" />

Thanks again to both of you.
[ December 31, 2006: Message edited by: David McCombs ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd highly recommend not using the <%! constructs at all. It's not necessary and makes your JSP pages non-thread safe. If two people access your JSP at the same time, you're going to be very very unhappy.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good comments on the meaning of "<%!".

Bear (or anybody else), do you know of a previosu discussion or article with a good description of what "<%!" IS appropriate for?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said before, in the era of JSP 2.0, any scriplets, be they declarations or otherwise, are contra-indicated (fancy-speak for "don't!").

In JSP 1.x, the decalration sytnax should only be used for class-level declarations; defining internal methods for example, constant declarations, or for the 0.00000000000000000001% of the time that an instance variable would have been appropriate.

But now, for JSP 2.0 the answer is never.
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gotcha ;)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!