We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Using URL path in jsp

R7AF
R7AF asked
on
Medium Priority
483 Views
Last Modified: 2012-08-13
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?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Try

String dataUrl = "./123/data.jsp";
rrzstudent
CERTIFIED EXPERT

Commented:
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";
Top Expert 2007

Author

Commented:
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)
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
Top Expert 2007

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK, i see. What do you get when you do

out.println(xmlAuth);

?
Top Expert 2007

Author

Commented:
I see the URL as I expected: http://localhost:8080/test/123/data.xml (or .jsp)
Top Expert 2007

Author

Commented:
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.
student
CERTIFIED EXPERT
Commented:
I guess you want
<%=new File(application.getRealPath(request.getServletPath())).getParent()%>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
rrzstudent
CERTIFIED EXPERT

Commented:
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");
rrzstudent
CERTIFIED EXPERT

Commented:
>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");
rrzstudent
CERTIFIED EXPERT

Commented:
Damn this is harder than I thought.
StreamSource xsl = new StreamSource(new File(new File(application.getRealPath(request.getServletPath())).getParent() + "transform.xsl"));  
Top Expert 2007

Author

Commented:
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)
Top Expert 2007

Author

Commented:
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); //*/
%>
rrzstudent
CERTIFIED EXPERT

Commented:
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());
Top Expert 2007

Author

Commented:
If I do that, I get: HttpURLConnection cannot be resolved to a type.

For me, this question is answered. Thanks for all your help!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.