IllegalAccessException using reflection on HttpSession

R

Ranger

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)
 
J

John C. Bollinger

Ranger said:
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
(e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top