1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import java.lang.reflect.*; 22 import java.util.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 27 34 35 public class Reflection01 extends HttpServlet { 36 37 public void service(HttpServletRequest request, 38 HttpServletResponse response) 39 throws IOException, ServletException { 40 41 response.setContentType("text/plain"); 42 PrintWriter writer = response.getWriter(); 43 StringBuffer results = new StringBuffer (); 44 45 try { 47 ServletConfig servletConfig = getServletConfig(); 48 Method method = servletConfig.getClass().getMethod 49 ("getParent", new Class [] {}); 50 Object parent = method.invoke(servletConfig, 51 new Object [] {}); 52 results.append(" Can reflect on ServletConfig/"); 53 } catch (Throwable t) { 54 StaticLogger.write("ServletConfig: " + t); 55 } 56 57 try { 59 ServletContext servletContext = getServletContext(); 60 Method method = servletContext.getClass().getMethod 61 ("getResources", new Class [] {}); 62 Object resources = method.invoke(servletContext, 63 new Object [] {}); 64 results.append(" Can reflect on ServletContext/"); 65 } catch (Throwable t) { 66 StaticLogger.write("ServletContext: " + t); 67 } 68 69 try { 71 Method method = request.getClass().getMethod 72 ("getInfo", new Class [] {}); 73 Object info = method.invoke(request, 74 new Object [] {}); 75 results.append(" Can reflect on HttpServletRequest/"); 76 } catch (Throwable t) { 77 StaticLogger.write("HttpServletRequest: " + t); 78 } 79 80 try { 82 Method method = request.getClass().getMethod 83 ("getInfo", new Class [] {}); 84 Object info = method.invoke(request, 85 new Object [] {}); 86 results.append(" Can reflect on HttpServletResponse/"); 87 } catch (Throwable t) { 88 StaticLogger.write("HttpServletResponse: " + t); 89 } 90 91 try { 93 HttpSession session = request.getSession(true); 94 Method method = session.getClass().getMethod 95 ("getInfo", new Class [] {}); 96 results.append(" Can reflect on HttpSession/"); 97 } catch (Throwable t) { 98 StaticLogger.write("HttpSession: " + t); 99 } 100 101 try { 103 RequestDispatcher rd = 104 getServletContext().getRequestDispatcher("/index.shtml"); 105 Method method = rd.getClass().getMethod 106 ("getInfo", new Class [] {}); 107 results.append(" Can reflect on RequestDispatcher/"); 108 } catch (Throwable t) { 109 StaticLogger.write("RequestDispatcher: " + t); 110 } 111 112 if (results.length() < 1) 114 writer.println("Reflection01 PASSED"); 115 else { 116 writer.print("Reflection01 FAILED -"); 117 writer.println(results.toString()); 118 } 119 120 while (true) { 121 String message = StaticLogger.read(); 122 if (message == null) 123 break; 124 writer.println(message); 125 } 126 StaticLogger.reset(); 127 128 } 129 130 } 131 | Popular Tags |