This discussion is archived
6 Replies Latest reply: Aug 9, 2010 11:24 AM by DrClap RSS

Reflection in Servlets

843798 Newbie
Currently Being Moderated
Hello All,

I am trying to load a class which is a Servlet class inside a webapp using URL Class Loader from another webapp. I got exception while loading the class. I think Tomcat is imposing a security mechanism to that does not allows to load a class from one webapp to another webapp. I would like to confirm this point whether Tomcat is imposing such security mechanism or not.

Also, I read about cross context access in Tomcat. As I am very new to Tomcat 6, I would be very thankful if somebody gives me a clue on this.

Sample code to load a class in a servlet
try{
                       //Loading a class using URLClassLoader
               File file = new File("/home/comm/tomcat6/webapps/myWebApp/WEB-INF/classes/");
               URL url = file.toURI().toURL(); 
               URL[] urls = new URL[] { url };
               ClassLoader cl = new URLClassLoader(urls);
               Class cls = cl.loadClass("com.servletImpl.MyServlet");//getting error at this line
               Class TempClass = Class.forName("com.servletImpl.MyServlet", true, cl);
               Package pack = TempClass.getPackage();
               Method[] procs = TempClass.getDeclaredMethods();
               System.out.println("Name is  : " + pack.getName());
               for(int i =0; i < procs.length; i++)
               {
                    System.out.println("Name is  : " + procs.getName());
               }
               //invoking method in the loaded class
          Method tempMethod = TempClass.getMethod("sendMessage", new Class[]{String.class, String.class});
               tempMethod.invoke(null, new Object[]{ID, message});
          }catch(Exception e){
               System.out.println("Exception");
               e.printStackTrace();
          }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 1. Re: Reflection in Servlets
    EJP Guru
    Currently Being Moderated
    Why? That's not how you deploy a servlet. Even if you can load it, what then?
  • 2. Re: Reflection in Servlets
    DrClap Expert
    Currently Being Moderated
    I think the hypothesis that Tomcat isn't allowing you to load a class is much, much less likely than the hypothesis that you're doing it wrong.

    But you didn't bother to post the message, so I won't bother to speculate any more.
  • 3. Re: Reflection in Servlets
    EJP Guru
    Currently Being Moderated
    I think Tomcat is imposing a security mechanism to that does not allows to load a class from one webapp to another webapp.
    Each web-app has its own classpath.
    I would like to confirm this point whether Tomcat is imposing such security mechanism or not.
    Of course, but only via keeping their classpaths distinct, which in turn is done via using a separate classloader per web-app.
  • 4. Re: Reflection in Servlets
    843798 Newbie
    Currently Being Moderated
    Thank you very much for your replies. Can you please tell us how to access a class in one webapp from another webapp.

    Regards,
    Gaja
  • 5. Re: Reflection in Servlets
    EJP Guru
    Currently Being Moderated
    For what purpose? Access how? If you just want to access another servlet's services, the Servlet API already lets you do that in several ways, e.g. via the RequestDispatcher. Have a good look around the Servlet API. Or you can just send it HTTP requests directly from your own servlet.
  • 6. Re: Reflection in Servlets
    DrClap Expert
    Currently Being Moderated
    And what kind of "class" is it you need to use? Based on your example you're going to call a message named sendMessage, so that wouldn't normally be a servlet class. If it's a support class, the easiest thing to do might be to include copies of the support classes in both applications.

    But we really do need to know the requirements. Your solution is way down the wrong road.