Velocity Reviews

Velocity Reviews (
-   Java (
-   -   Resource/File URL Problems, relative links, servlet context and (

LB 06-02-2008 04:34 AM

Resource/File URL Problems, relative links, servlet context and
I'm trying to write to a file from within a <jsp:useBean java class.
Problem is the naming conventions of the file address. I'm trying to
develop application on localhost using netbeans, then deploy to live

I can't figure out how to pass the servlet context info into the
useBean java class.

(attempt 1)
I found a posting that recommended adding this code into the JavaBean:
import javax.servlet.jsp.JspPage;

private JspPage jspPage;

public JspPage getJspPage() { return jspPage; }
public void setJspPage (JspPage jspPage) { this.jspPage =
jspPage; }

and then, from within the JSP page, invoke the following:
<jsp:useBean id="yourBean" class="your.package.YourBean" />
<% yourBean.setJspPage(this); %>

Then the bean can access the implicit objects and methods of the
calling JSP page. Reportedly, you can use the ServletConfig using
JspPage.getServletConfig() and in turn use that
with .getServletContext() to return the ServletContext related to the
original JSP page.

No go for me: I keep getting "Non-static method getServletConfig()
cannot be referenced from a static context." errors. Ugh.

(attempt 1.5)
I tried same technique with a <%
yourBean.setServContext(this.getServletContext) but I get immediate
errors on the .jsp page... No go there.

(attempt 2)
I was able to successfully transmit String contextPath and String
hostName from the JSP using an already existing form:
<INPUT TYPE="hidden" name="contextPath" value= $
{pageContext.request.contextPath} >
<INPUT TYPE="hidden" name="hostName" value=<%=
request.getHeader("Host")%> >
Getters & Setters are defined within my bean, and the next .jsp page
includes <jsp:setProperty name="yourBean" property="*" />

Problem is, I am unable to resolve those into an address that works
for the file system. I was hoping to use
ServletContext.getRealPath(string) function, but I don't think that's
in the cards.

I do have the following:
hostName = "localhost:8080"
contextPath = "/ProjectName"
filePath = "/data/file.csv"

with netbeans local server running I can manually paste
http://localhost:8080/ProjectName/data/file.csv and see the correct
file. What I have been unable to do is joint those three strings
together and avoid a

PrintWriter userout = new PrintWriter(new FileWriter(new
File( this.realCSVFilePath),true));

When realCSVFilePath = "http://localhost:8080/ProjectName/data/
file.csv", the error that I get on the server is:
" http:\localhost:
8080\ContactUs_Feedback\data\VisitLog.csv (The filename, directory
name, or volume label syntax is incorrect)"

One slash after http:// disappears and all the slashes are reversed.
Anybody been here before? I've been playing with the
without much luck.

Any clues as to what I am doing wrong? I'm embarrassed to tell you
how long I've been struggling on this problem. Many thanks in

LB 06-10-2008 08:24 PM

Re: Resource/File URL Problems, relative links, servlet context and
I got some of the file stuff cleaned up using class URLConnection and

URL tempURL = new URI(PROTOCOL + hostName + contextPath +
URLConnection CSVconnect = tempURL.openConnection();
CSVconnect.setRequestProperty("Content-type", "application/

DataOutputStream DataOut = new
BufferedReader DataIn = new BufferedReader(new
String inputLine;
while ((inputLine = DataIn.readLine()) != null) {

DataOut.writeBytes("writeBytes " + getCSVText());
DataOut.writeChars("writeChars " + getCSVText());
DataOut.writeUTF("writeUTF " + getCSVText());

I can now see the text file that I have stored within my Netbeans
project. I can access the file using: "http://localhost:8084/
WebApplication1/data/textfile.csv" I can see the system.out.println
listing on my local host server per above and it correctly matches the
file of interest...)

What will not work is any DataOut operation. How do you write to a
simple text file from a 'network / served' application? I've tried
both DataOutputStream and PrintWriter. Obviously with those two
different classes, the write / append methods vary slightly...) I'm
not getting any exception errors, and yes, my code should catch
something. The file write operation simply doesn't seem to be
working. I'm using Netbeans IDE 6.1 on a Windows XP machine.

Anybody been here before? I can read the file, but can't write to
it. Is there some permission issue on a windows localhost machine?
Did you all give up and just simply attach all your data to a SQL
database instead? How do you send data to a simple text file at the
server from a POJO bean?

thanks in advance for your assistance,

All times are GMT. The time now is 01:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.