Link to home
Start Free TrialLog in
Avatar of R7AF
R7AFFlag for Netherlands

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?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try

String dataUrl = "./123/data.jsp";
I think maybe CEHJ meant to post this.
String dataUrl = "./data.jsp";

Here is another way(just for fun).
  String parent = request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/") + 1);
  String dataUrl =  parent + "data.jsp";
Avatar of R7AF

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.StringValue, net.sf.saxon.trans.DynamicError,
net.sf.saxon.functions.SystemProperty, net.sf.saxon.trans.DynamicError,
net.sf.saxon.FeatureKeys, net.sf.saxon.Configuration,
net.sf.saxon.trace.XSLTTraceListener, net.sf.saxon.TransformerFactoryImpl,
javax.xml.parsers.*, org.w3c.dom.*, javax.xml.transform.*,
javax.xml.transform.stream.*, java.io.*, java.net.URL;" %>

<%
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");

String urlpath = request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/") + 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.openStream());
StreamSource xsl = new StreamSource(xslAuth.openStream());
StreamResult result = new StreamResult(out);

TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(xml, result); //*/
%>

Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:423)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.net.ConnectException: Connection refused
      java.net.PlainSocketImpl.socketConnect(Native Method)
      java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
      java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
      java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
      java.net.Socket.connect(Socket.java:516)
      java.net.Socket.connect(Socket.java:466)
      sun.net.NetworkClient.doConnect(NetworkClient.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(URL.java:1007)
      org.apache.jsp.q.qx.saxon_jsp._jspService(saxon_jsp.java:82)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
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
Avatar of R7AF

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);

?
Avatar of R7AF

ASKER

I see the URL as I expected: http://localhost:8080/test/123/data.xml (or .jsp)
Avatar of R7AF

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
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.getRealPath(request.getServletPath())).getParent() + "transform.xsl");  
StreamSource xml = new StreamSource(request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/") + 1) + "data.jsp");
>StreamSource xml = new StreamSource(request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/") + 1) + "data.jsp");
If that doesn't work, I guess you should try URI,
StreamSource xml = new StreamSource(request.getRequestURI().substring(0,request.getRequestURL().lastIndexOf("/") + 1) + "data.jsp");
Damn this is harder than I thought.
StreamSource xsl = new StreamSource(new File(new File(application.getRealPath(request.getServletPath())).getParent() + "transform.xsl"));  
Avatar of R7AF

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.getRequestURI().substring(0,request.getRequestURL().lastIndexOf("/") + 1) + "data.jsp");

Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:435)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

root cause

java.lang.StringIndexOutOfBoundsException: String index out of range: 25
      java.lang.String.substring(String.java:1765)
      org.apache.jsp.q.qx.saxon_jsp._jspService(saxon_jsp.java:85)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
Avatar of R7AF

ASKER

However, I got it working using
new File(application.getRealPath(request.getServletPath())).getParent()

<%@ page contentType="text/html"
import="net.sf.saxon.value.StringValue, net.sf.saxon.trans.DynamicError,
net.sf.saxon.functions.SystemProperty, net.sf.saxon.trans.DynamicError,
net.sf.saxon.FeatureKeys, net.sf.saxon.Configuration,
net.sf.saxon.trace.XSLTTraceListener, net.sf.saxon.TransformerFactoryImpl,
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.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");

String xmlpath = new File(application.getRealPath(request.getServletPath())).getParent().toString() + "/data.xml";
File xmlfile = new File(xmlpath);

String xslpath = new File(application.getRealPath(request.getServletPath())).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.newInstance();
Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(xml, result); //*/
%>
Did you try using something like this ?
    String parent = request.getRequestURL().substring(0,request.getRequestURL().lastIndexOf("/") + 1);
    URL url = new URL(parent + "data.jsp");
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    conn.connect();
    StreamSource xml = new StreamSource(conn.getInputStream());
Avatar of R7AF

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!