L
LB
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
web...
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 java.io.FileNotFoundException:
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:
"java.io.FileNotFoundException: 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 java.net.URL
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
advance,
LB.
Problem is the naming conventions of the file address. I'm trying to
develop application on localhost using netbeans, then deploy to live
web...
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 java.io.FileNotFoundException:
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:
"java.io.FileNotFoundException: 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 java.net.URL
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
advance,
LB.