Html-structure

<html><head></head><body>
<!-- START XOXO -->
"lots of html and jsp stuff"
<!-- STOP XOXO -->
"file reading"
</body>
</html>

The file reading code

<%@ page import="java.io.*" %>
<%
String line = "";
String file = request.getRequestURL().toString();
int print = 0;
BufferedReader input = new BufferedReader(new FileReader(file));



while ((line = input.readLine()) != null) {
	if (print == 1) { 

		out.println(line); 
	}
	
	if (line.equals("<!-- START XOXO -->")) { print = 1; }
	else if (line.equals("<!--STOP XOXO -->")) { print = 0; }
}
input.close();
%>

Output

java.io.FileNotFoundException: http:/jsp-stud.idi.ntnu.no:8080/nicklau/oving4test.jsp
(No such file or directory)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:106)
	at java.io.FileInputStream.<init>(FileInputStream.java:66)
	at java.io.FileReader.<init>(FileReader.java:41)
	at _nicklau._oving4test__jsp._jspService(/locals/web/folk/nicklau/oving4test.jsp:106)
	at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
	at com.caucho.jsp.Page.subservice(Page.java:486)
	at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
	at com.caucho.http.security.SecurityFilter.doFilter(SecurityFilter.java:115)
	at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
	at com.caucho.server.http.Invocation.service(Invocation.java:311)
	at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
	at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:218)
	at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:160)
	at com.caucho.server.TcpConnection.run(TcpConnection.java:137)
	at java.lang.Thread.run(Thread.java:534)

I just cant figure out why it cant find itself?

Recommended Answers

All 7 Replies

the returned String is a URL so use this instead

URL url = new URL(request.getRequestURL());

BufferedReader input = new BufferedReader(new InputStreamReader(url.openStream()));

I importet java.net.*.. And tried what you posted.
This is what I got:

Note: sun.tools.javac.Main has been deprecated.
/locals/web/folk/nicklau/oving4test.jsp:110: Incompatible type for constructor.
Can't convert java.lang.StringBuffer to java.lang.String.
URL url = new URL(request.getRequestURL());

So I add altered the second line, adding ".toString()"

URL url = new URL(request.getRequestURL().toString());

Then I got this again:

java.io.IOException: Server returned HTTP response code: 403 for URL: http://jsp-stud.idi.ntnu.no:8080/nicklau/oving4test.jsp
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:789)
	at java.net.URL.openStream(URL.java:913)
	at _nicklau._oving4test__jsp._jspService(/locals/web/folk/nicklau/oving4test.jsp:111)
	at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
	at com.caucho.jsp.Page.subservice(Page.java:486)
	at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
	at com.caucho.http.security.SecurityFilter.doFilter(SecurityFilter.java:115)
	at com.caucho.server.http.FilterChainFilter.doFilter(FilterChainFilter.java:88)
	at com.caucho.server.http.Invocation.service(Invocation.java:311)
	at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
	at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:218)
	at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:160)
	at com.caucho.server.TcpConnection.run(TcpConnection.java:137)
	at java.lang.Thread.run(Thread.java:534)

can you access this file directly from the browser?

got no problems at all with that if I comment out the jsp-part that throws that exception. :p The read part, that is. It shows just fine then.

I've tested it with other filenames as well now, relativ to the jsp file. Same deal there, then tryed it with som external files. Also the same. I'm starting to wonder if it could be the server rights, and not the jsp it self. Since it is on a server at my school.

Could that be the case?

it finds the file but returns a 403 error which means you are forbidden from accessing the file.

it must be a server configuration then, that forbids jsp from accessing other files. Else it should work with external sites.

You dont know a workaround for this?? :P

Run your code from a main application and see if it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.