R7AF
asked on
Using URL path in jsp
Say I have a webapp (http://mydomain.com/test/) with three files in a subdirectory (123):
1) test/123/index.jsp
2) test/123/data.jsp
3) test/123/test.jsp
Index.jsp reads data from data.jsp and test.jsp. It has the following code in it:
String dataUrl = "http://localhost:8080/test/123/data.jsp";
String tUrl = "http://localhost:8080/test/123/test.jsp";
URL dataAuth = new URL(dataUrl);
URL tAuth = new URL(tUrl);
What happens here is that it reads data from these files in the same folder. What I want is to be able to read data.jsp from a relative path (like: String dataUrl = "data.jsp";) or get the path name (test/123) from the request URL. Now I have to change the url for each folder where I use this code. I want this to be done dynamically. How can I do this?
1) test/123/index.jsp
2) test/123/data.jsp
3) test/123/test.jsp
Index.jsp reads data from data.jsp and test.jsp. It has the following code in it:
String dataUrl = "http://localhost:8080/test/123/data.jsp";
String tUrl = "http://localhost:8080/test/123/test.jsp";
URL dataAuth = new URL(dataUrl);
URL tAuth = new URL(tUrl);
What happens here is that it reads data from these files in the same folder. What I want is to be able to read data.jsp from a relative path (like: String dataUrl = "data.jsp";) or get the path name (test/123) from the request URL. Now I have to change the url for each folder where I use this code. I want this to be done dynamically. How can I do this?
I think maybe CEHJ meant to post this.
String dataUrl = "./data.jsp";
Here is another way(just for fun).
String parent = request.getRequestURL().su bstring(0, request.ge tRequestUR L().lastIn dexOf("/") + 1);
String dataUrl = parent + "data.jsp";
String dataUrl = "./data.jsp";
Here is another way(just for fun).
String parent = request.getRequestURL().su
String dataUrl = parent + "data.jsp";
ASKER
Sorry, I should've given a complete example. Both solutions don't work. The fun-solution does what it says (I can print the url), but for some reason I can't use it with StreamSource.
<%@ page contentType="text/html"
import="net.sf.saxon.value .StringVal ue, net.sf.saxon.trans.Dynamic Error,
net.sf.saxon.functions.Sys temPropert y, net.sf.saxon.trans.Dynamic Error,
net.sf.saxon.FeatureKeys, net.sf.saxon.Configuration ,
net.sf.saxon.trace.XSLTTra ceListener , net.sf.saxon.TransformerFa ctoryImpl,
javax.xml.parsers.*, org.w3c.dom.*, javax.xml.transform.*,
javax.xml.transform.stream .*, java.io.*, java.net.URL;" %>
<%
System.setProperty("javax. xml.transf orm.Transf ormerFacto ry",
"net.sf.saxon.TransformerF actoryImpl ");
String urlpath = request.getRequestURL().su bstring(0, request.ge tRequestUR L().lastIn dexOf("/") + 1);
String XmlUrl = urlpath + "data.jsp";
String XslUrl = urlpath + "transform.xsl";
URL xmlAuth = new URL(XmlUrl);
URL xslAuth = new URL(XslUrl);
//out.print(urlpath);
//*
StreamSource xml = new StreamSource(xmlAuth.openS tream());
StreamSource xsl = new StreamSource(xslAuth.openS tream());
StreamResult result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInst ance();
Transformer transformer = tFactory.newTransformer(xs l);
transformer.transform(xml, result); //*/
%>
Stacktrace:
org.apache.jasper.servlet. JspServlet Wrapper.ha ndleJspExc eption(Jsp ServletWra pper.java: 524)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:4 23)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:320)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:266)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
root cause
java.net.ConnectException: Connection refused
java.net.PlainSocketImpl.s ocketConne ct(Native Method)
java.net.PlainSocketImpl.d oConnect(P lainSocket Impl.java: 333)
java.net.PlainSocketImpl.c onnectToAd dress(Plai nSocketImp l.java:195 )
java.net.PlainSocketImpl.c onnect(Pla inSocketIm pl.java:18 2)
java.net.Socket.connect(So cket.java: 516)
java.net.Socket.connect(So cket.java: 466)
sun.net.NetworkClient.doCo nnect(Netw orkClient. java:157)
sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
sun.net.www.http.HttpClient.<init>(HttpClient .java:214)
sun.net.www.http.HttpClient.New(HttpClient.java:287)
sun.net.www.http.HttpClient.New(HttpClient.java:299)
sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:795)
sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:747)
sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:672)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:916)
java.net.URL.openStream(UR L.java:100 7)
org.apache.jsp.q.qx.saxon_ jsp._jspSe rvice(saxo n_jsp.java :82)
org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:70)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 93)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:320)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:266)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
<%@ page contentType="text/html"
import="net.sf.saxon.value
net.sf.saxon.functions.Sys
net.sf.saxon.FeatureKeys, net.sf.saxon.Configuration
net.sf.saxon.trace.XSLTTra
javax.xml.parsers.*, org.w3c.dom.*, javax.xml.transform.*,
javax.xml.transform.stream
<%
System.setProperty("javax.
"net.sf.saxon.TransformerF
String urlpath = request.getRequestURL().su
String XmlUrl = urlpath + "data.jsp";
String XslUrl = urlpath + "transform.xsl";
URL xmlAuth = new URL(XmlUrl);
URL xslAuth = new URL(XslUrl);
//out.print(urlpath);
//*
StreamSource xml = new StreamSource(xmlAuth.openS
StreamSource xsl = new StreamSource(xslAuth.openS
StreamResult result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInst
Transformer transformer = tFactory.newTransformer(xs
transformer.transform(xml,
%>
Stacktrace:
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
root cause
java.net.ConnectException:
java.net.PlainSocketImpl.s
java.net.PlainSocketImpl.d
java.net.PlainSocketImpl.c
java.net.PlainSocketImpl.c
java.net.Socket.connect(So
java.net.Socket.connect(So
sun.net.NetworkClient.doCo
sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
sun.net.www.http.HttpClient.<init>(HttpClient
sun.net.www.http.HttpClient.New(HttpClient.java:287)
sun.net.www.http.HttpClient.New(HttpClient.java:299)
sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:795)
sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:747)
sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:672)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:916)
java.net.URL.openStream(UR
org.apache.jsp.q.qx.saxon_
org.apache.jasper.runtime.
javax.servlet.http.HttpSer
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
There's something odd going on here - you're trying to mix two protocols that don't mix. You should be connecting the StreamSource to a plain file - not a JSP
ASKER
Data.jsp queries data from a database and creates xml. When you say it should be plain, do you mean the jsp is not executed, but handled as plain text? However if I take a static xml file (data.xml), the problem stays.
OK, i see. What do you get when you do
out.println(xmlAuth);
?
out.println(xmlAuth);
?
ASKER
I see the URL as I expected: http://localhost:8080/test/123/data.xml (or .jsp)
ASKER
To be clear, I'm open to other ways to solve this. What I need is a way to read a local file (local to index.jsp) without having to know the full path from the root up, or needing a hard URL. I don't care about dropping StreamSource or anything else as long as the transformation works.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I posted the above for use with the StreamSource constructor that takes a File. Then you could use
StreamSource xsl = new StreamSource(new File(application.getRealPa th(request .getServle tPath())). getParent( ) + "transform.xsl");
StreamSource xml = new StreamSource(request.getRe questURL() .substring (0,request .getReques tURL().las tIndexOf(" /") + 1) + "data.jsp");
StreamSource xsl = new StreamSource(new File(application.getRealPa
StreamSource xml = new StreamSource(request.getRe
>StreamSource xml = new StreamSource(request.getRe questURL() .substring (0,request .getReques tURL().las tIndexOf(" /") + 1) + "data.jsp");
If that doesn't work, I guess you should try URI,
StreamSource xml = new StreamSource(request.getRe questURI() .substring (0,request .getReques tURL().las tIndexOf(" /") + 1) + "data.jsp");
If that doesn't work, I guess you should try URI,
StreamSource xml = new StreamSource(request.getRe
Damn this is harder than I thought.
StreamSource xsl = new StreamSource(new File(new File(application.getRealPa th(request .getServle tPath())). getParent( ) + "transform.xsl"));
StreamSource xsl = new StreamSource(new File(new File(application.getRealPa
ASKER
Hi rrz@871311, thanks for all the effort. If I use your code, I get an error message.
Using:
StreamSource xml = new StreamSource(request.getRe questURI() .substring (0,request .getReques tURL().las tIndexOf(" /") + 1) + "data.jsp");
Stacktrace:
org.apache.jasper.servlet. JspServlet Wrapper.ha ndleJspExc eption(Jsp ServletWra pper.java: 524)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:4 35)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:320)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:266)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
root cause
java.lang.StringIndexOutOf BoundsExce ption: String index out of range: 25
java.lang.String.substring (String.ja va:1765)
org.apache.jsp.q.qx.saxon_ jsp._jspSe rvice(saxo n_jsp.java :85)
org.apache.jasper.runtime. HttpJspBas e.service( HttpJspBas e.java:70)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
org.apache.jasper.servlet. JspServlet Wrapper.se rvice(JspS ervletWrap per.java:3 93)
org.apache.jasper.servlet. JspServlet .serviceJs pFile(JspS ervlet.jav a:320)
org.apache.jasper.servlet. JspServlet .service(J spServlet. java:266)
javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 803)
Using:
StreamSource xml = new StreamSource(request.getRe
Stacktrace:
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
root cause
java.lang.StringIndexOutOf
java.lang.String.substring
org.apache.jsp.q.qx.saxon_
org.apache.jasper.runtime.
javax.servlet.http.HttpSer
org.apache.jasper.servlet.
org.apache.jasper.servlet.
org.apache.jasper.servlet.
javax.servlet.http.HttpSer
ASKER
However, I got it working using
new File(application.getRealPa th(request .getServle tPath())). getParent( )
<%@ page contentType="text/html"
import="net.sf.saxon.value .StringVal ue, net.sf.saxon.trans.Dynamic Error,
net.sf.saxon.functions.Sys temPropert y, net.sf.saxon.trans.Dynamic Error,
net.sf.saxon.FeatureKeys, net.sf.saxon.Configuration ,
net.sf.saxon.trace.XSLTTra ceListener , net.sf.saxon.TransformerFa ctoryImpl,
javax.xml.parsers.*, org.w3c.dom.*, javax.xml.transform.*,
javax.xml.transform.stream .*, java.io.*, java.io.File.*, java.net.URL;" %>
<%
System.setProperty("javax. xml.transf orm.Transf ormerFacto ry",
"net.sf.saxon.TransformerF actoryImpl ");
String xmlpath = new File(application.getRealPa th(request .getServle tPath())). getParent( ).toString () + "/data.xml";
File xmlfile = new File(xmlpath);
String xslpath = new File(application.getRealPa th(request .getServle tPath())). getParent( ).toString () + "/transform.xsl";
File xslfile = new File(xslpath);
StreamSource xml = new StreamSource(xmlfile);
StreamSource xsl = new StreamSource(xslfile);
StreamResult result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInst ance();
Transformer transformer = tFactory.newTransformer(xs l);
transformer.transform(xml, result); //*/
%>
new File(application.getRealPa
<%@ page contentType="text/html"
import="net.sf.saxon.value
net.sf.saxon.functions.Sys
net.sf.saxon.FeatureKeys, net.sf.saxon.Configuration
net.sf.saxon.trace.XSLTTra
javax.xml.parsers.*, org.w3c.dom.*, javax.xml.transform.*,
javax.xml.transform.stream
<%
System.setProperty("javax.
"net.sf.saxon.TransformerF
String xmlpath = new File(application.getRealPa
File xmlfile = new File(xmlpath);
String xslpath = new File(application.getRealPa
File xslfile = new File(xslpath);
StreamSource xml = new StreamSource(xmlfile);
StreamSource xsl = new StreamSource(xslfile);
StreamResult result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInst
Transformer transformer = tFactory.newTransformer(xs
transformer.transform(xml,
%>
Did you try using something like this ?
String parent = request.getRequestURL().su bstring(0, request.ge tRequestUR L().lastIn dexOf("/") + 1);
URL url = new URL(parent + "data.jsp");
HttpURLConnection conn = (HttpURLConnection)url.ope nConnectio n();
conn.setRequestMethod("GET ");
conn.setDoInput(true);
conn.connect();
StreamSource xml = new StreamSource(conn.getInput Stream());
String parent = request.getRequestURL().su
URL url = new URL(parent + "data.jsp");
HttpURLConnection conn = (HttpURLConnection)url.ope
conn.setRequestMethod("GET
conn.setDoInput(true);
conn.connect();
StreamSource xml = new StreamSource(conn.getInput
ASKER
If I do that, I get: HttpURLConnection cannot be resolved to a type.
For me, this question is answered. Thanks for all your help!
For me, this question is answered. Thanks for all your help!
String dataUrl = "./123/data.jsp";