Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»


So we have to eliminate the HTML tags from the file, but, of course, deleting them would defeat the purpose of this project, so that won’t work. What we do then is bend to the browser and display everything in HTML, but convert the angle brackets of HTML tags (and the ampersands of existing entity tags) into their HTML entity forms: &lt;, &gt;, and &amp;. Wrapping the file contents between <pre> tags and converting the angle brackets like this gives us the source code.


JSP by example



Listing 17.9 viewsource.jsp


<%@ page import=»java.io.*» %>


<%


String url = request.getParameter(«url»); if (url.indexOf(«..») > -1)


throw new java.io.IOException(«Relative paths are not allowed»); File realPath = new File(application.getRealPath(url));


%>


<html><head><title>Source: <%= url %></title></head><body><pre>


<%


FileInputStream fis = null;


try {


fis = new FileInputStream(realPath);


BufferedReader reader;


reader = new BufferedReader(new InputStreamReader(fis));


String line;


while ((line = reader.readLine()) != null) { line = replace(line, «&», «&amp;»);


line = replace(line, «<«,


line = replace(line, «>», «&gt;H);


out.println(line);


}


}


catch (IOException e) {


out.println(«IOException: n + e.getMessage());


}


finally { if (fis != null) fis.close(); }


%>


</pre></body></html>


<%!


public String replace(String s, String old, String replacement) { int i = s.indexOf(old);

Скачать в pdf «Web Development with JavaServer Pages»