[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using URL path in jsp

Posted on 2007-07-20
16
Medium Priority
?
439 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?
0
Comment
Question by:R7AF
  • 7
  • 6
  • 3
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19530830
Try

String dataUrl = "./123/data.jsp";
0
 
LVL 28

Expert Comment

by:rrz
ID: 19531395
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";
0
 
LVL 13

Author Comment

by:R7AF
ID: 19532150
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)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 19532236
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
0
 
LVL 13

Author Comment

by:R7AF
ID: 19532476
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19532635
OK, i see. What do you get when you do

out.println(xmlAuth);

?
0
 
LVL 13

Author Comment

by:R7AF
ID: 19532775
I see the URL as I expected: http://localhost:8080/test/123/data.xml (or .jsp)
0
 
LVL 13

Author Comment

by:R7AF
ID: 19532838
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.
0
 
LVL 28

Accepted Solution

by:
rrz earned 2000 total points
ID: 19533404
I guess you want
<%=new File(application.getRealPath(request.getServletPath())).getParent()%>
0
 
LVL 28

Expert Comment

by:rrz
ID: 19533517
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");
0
 
LVL 28

Expert Comment

by:rrz
ID: 19533565
>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");
0
 
LVL 28

Expert Comment

by:rrz
ID: 19533601
Damn this is harder than I thought.
StreamSource xsl = new StreamSource(new File(new File(application.getRealPath(request.getServletPath())).getParent() + "transform.xsl"));  
0
 
LVL 13

Author Comment

by:R7AF
ID: 19535975
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)
0
 
LVL 13

Author Comment

by:R7AF
ID: 19535986
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); //*/
%>
0
 
LVL 28

Expert Comment

by:rrz
ID: 19537179
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());
0
 
LVL 13

Author Comment

by:R7AF
ID: 19538035
If I do that, I get: HttpURLConnection cannot be resolved to a type.

For me, this question is answered. Thanks for all your help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question