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?

Dani AI

Generated

The exception comes from using FileReader on an HTTP URL string. FileReader expects a local filesystem path, so giving it something like http://... makes Java try to open a file literally named that, which fails. 's URL/openStream approach is valid for fetching over HTTP, but the HTTP 403 you saw means the server refused the request (access control, authentication, or outbound/http-loop blocking on the host).

A more reliable server-side approach is to read the resource from the webapp context instead of issuing an HTTP request to yourself. Two common options:

/* safer when the webapp is exploded on disk */
String realPath = application.getRealPath(request.getServletPath());
/* check realPath != null and File.exists()/canRead() before opening */
/* works even if the webapp is not exploded (preferred) */
InputStream is = application.getResourceAsStream(request.getServletPath());
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));

Troubleshooting steps: log the path you try to open; test getResourceAsStream for null; if getRealPath is non‑null check new File(realPath).exists() and canRead(). If you must use HTTP and get 403, try curling the URL from the server itself (or inspect server access/error logs) to see whether the container blocks loopback requests or requires auth.

Design note: reading the JSP source while it executes is fragile. If the goal is to include the chunk between your markers, move that chunk into a separate include/fragment and use a server-side include or <jsp:include> (or getResourceAsStream) — that’s simpler, more portable, and avoids file/HTTP permission headaches. , your suspicion about server rights is reasonable; check container logs and try the context-based reads above before chasing an HTTP workaround.

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: 
	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.