IllegalAccessException using reflection on HttpSession

Discussion in 'Java' started by Ranger, Jul 30, 2003.

  1. Ranger

    Ranger Guest

    I have a scenario where I need to invoke methods on the object
    implementing HttpSession retrieved by getSession(). When I use
    ServletExec and run my code, it works fine. When I deploy this same
    code on WebLogic, I get an IllegalAccessException with no detailed
    information (the message is null).

    See the following code:

    HttpSession session = request.getSession();
    Method method = session.getClass().getMethod("getId", new Class[] {
    });
    String id = (String)method.invoke(session, new Object[] { });
    out.println("Session id is: "+id);

    The error when executing this on a WebLogic server:

    java.lang.IllegalAccessException
    at java.lang.reflect.Method.invoke(Native Method)
    at TestServlet.doGet(TestServlet.java:41)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:265)
    at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:200)
    at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:2495)
    at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2204)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:139)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:120)
     
    Ranger, Jul 30, 2003
    #1
    1. Advertising

  2. Ranger wrote:
    > I have a scenario where I need to invoke methods on the object
    > implementing HttpSession retrieved by getSession(). When I use
    > ServletExec and run my code, it works fine. When I deploy this same
    > code on WebLogic, I get an IllegalAccessException with no detailed
    > information (the message is null).
    >
    > See the following code:
    >
    > HttpSession session = request.getSession();
    > Method method = session.getClass().getMethod("getId", new Class[] {
    > });
    > String id = (String)method.invoke(session, new Object[] { });
    > out.println("Session id is: "+id);


    Why in the world are you trying to do it that way? How about

    out.println("Session id is: " + request.getSession().getId());

    instead? (Or cache intermediate references as desired.) You do not
    need to use reflection to invoke the methods defined by an interface
    through a compatibly-typed reference. That would make interfaces pretty
    useless.


    John Bollinger
     
    John C. Bollinger, Aug 4, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cHJhZGVlcF9UUA==?=

    HttpSession Class

    =?Utf-8?B?cHJhZGVlcF9UUA==?=, Oct 16, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    7,139
    Joerg Jooss
    Oct 16, 2005
  2. mike
    Replies:
    4
    Views:
    13,826
  3. mike
    Replies:
    2
    Views:
    551
  4. Andreas Solsbach

    Classloader IllegalAccessException

    Andreas Solsbach, Oct 7, 2004, in forum: Java
    Replies:
    0
    Views:
    429
    Andreas Solsbach
    Oct 7, 2004
  5. Josh
    Replies:
    0
    Views:
    1,682
Loading...

Share This Page