1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 27 41 42 public class Resources03 extends HttpServlet { 43 44 public void doGet(HttpServletRequest request, HttpServletResponse response) 45 throws IOException, ServletException { 46 47 String mode = request.getParameter("mode"); 49 if (mode == null) 50 mode = "context"; 51 String path = request.getParameter("path"); 52 if (path == null) 53 path = "/WEB-INF/web.xml"; 54 55 response.setContentType("text/plain"); 57 PrintWriter writer = response.getWriter(); 58 InputStream is = null; 59 URL url = null; 60 try { 61 if ("context".equals(mode)) { 62 is = getServletContext().getResourceAsStream(path); 63 url = getServletContext().getResource(path); 64 } else { 65 is = this.getClass().getResourceAsStream(path); 66 url = this.getClass().getResource(path); 67 } 68 if (url == null) { 69 if (is == null) 70 writer.println("Resources03 FAILED - No IS or URL was returned"); 71 else 72 writer.println("Resources03 FAILED - Returned IS but no URL"); 73 } else { 74 if (is == null) 75 writer.println("Resources03 FAILED - Returned URL but no IS"); 76 else { 77 InputStreamReader isr = new InputStreamReader(is); 78 while (true) { 79 int c = isr.read(); 80 if (c < 0) 81 break; 82 char ch = (char) c; 83 if (ch < ' ') 84 break; 85 writer.print(ch); 86 } 87 isr.close(); 88 } 89 writer.println(); 90 writer.println("url = " + url.toString()); 91 } 92 } catch (MalformedURLException e) { 93 writer.println("Resources03 FAILED - MalformedURLException: " 94 + e); 95 } catch (IOException e) { 96 writer.println("Resources03 FAILED - IOException: " + e); 97 } 98 99 while (true) { 101 String message = StaticLogger.read(); 102 if (message == null) 103 break; 104 writer.println(message); 105 } 106 StaticLogger.reset(); 107 108 } 109 110 } 111 | Popular Tags |