Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8410855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:17:16+00:00 2026-06-10T00:17:16+00:00

I have a Servlet class which do some dynamic class initialization and method invocation

  • 0

I have a Servlet class which do some dynamic class initialization and method invocation using reflection. It has the following code to do that.

@Override
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
    try {
        String reqActionContext = PageContextHelper.getFunctionName( "login" );
         // Trace reqActionContext
        System.out .println("reqActionContext : " + reqActionContext );
        // Get the page context related class object
        Object contextClass = PageContextHelper.getContextBoundedClass( "authentication");
        Class < ? >[ ] paramTypes = new Class < ? >[2];

        paramTypes[0] = HttpServletRequest.class ;
        paramTypes[1] = HttpServletResponse.class ;

        // Is there a class associated with the current context
        if ( contextClass != null ) {
            // Trace contextClassSystem. out .println("Contextclass : " + contextClass);
            // get the Class
            Class < ? > thisClass = contextClass.getClass();
            // Trace thisClass
            System.out .println("thisClass : " + thisClass );
            // get the method
            Method contextMethod = thisClass.getDeclaredMethod( reqActionContext, paramTypes );

            if ( contextMethod != null ) {
                contextMethod.invoke ( contextClass, request, response );
            }
        }
    }  catch ( IllegalArgumentException e ) {
        e.printStackTrace();
    } catch ( IllegalAccessException e ) {
        e.printStackTrace();
    } catch ( InvocationTargetException e ) {
        e.printStackTrace();
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

The class which get the context bounded class has the following code :
PageContextHelper.java

public class PageContextHelper {

    private static final String CONTEXT_CLASS_SUFFIX = "ContextHelper" ;

    private static final String CONTEXT_CLASS_PACKAGE = "com.proj.context." ;

    /**
    * Get the base path related context class object.
    *
    *@param basePath
    *@return
    */
    public static Object getContextBoundedClass ( String basePath ) {
        Class < ? > classOBJ = null ;

        try{

            if(basePath != null && !basePath.isEmpty() && basePath.length() > 1 ) {
                basePath = basePath .substring( 0,1 ).toUpperCase() + basePath.substring( 1 ).toLowerCase();
            }
            // Get the class object
            classOBJ = Class.forName( CONTEXT_CLASS_PACKAGE + basePath + CONTEXT_CLASS_SUFFIX);
        } catch ( ClassNotFoundException e ) {
            // Do nothing and return null object
        }
        return classOBJ;
    }

    .....

}

The class which is looking for is com.proj.context.AuthenticationContextHelper and it has the following content :
com.proj.context.AuthenticationContextHelper

package com.proj.context;

    public class AuthenticationContextHelper{

    public void loginProcess (HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            // Do some processing here
        } catch ( Exception e ) {
            // TODO : Remove this trace line and do something meaningful
            e.printStackTrace();
        }
    }
}

When I run the code get the following trace :
Trace

reqActionContext :loginProcess 
Contextclass : class com.proj.context.AuthenticationContextHelper
thisClass : class java.lang.Class
java.lang.NoSuchMethodException: java.lang.Class.loginProcess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
        at java.lang.Class.getDeclaredMethod(Class.java:1937)
        at com.proj.servlet.ContentServlet.doGet(ContentServlet.java:81)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
...

It says NoSuchMethod but the method is there in the class com.proj.context.AuthenticationContextHelper.

Am I missing any basic here or something wrong in the code?

The environment I am using is Java 1.6.0_32 and Tomcat 7

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:17:18+00:00Added an answer on June 10, 2026 at 12:17 am

    You are returning a Class object from getContextBoundedClass() and getting that into an Object here:

    Object contextClass = PageContextHelper.getContextBoundedClass( "authentication");
    

    Instead say this:

     Class contextClass = PageContextHelper.getContextBoundedClass( "authentication");
    

    You dont call contextClass.getClass() again. Instead to get an instance of a class you call contextClass.newInstance() to create an instance of that object and use that while invoking the method. Read this tutorial:

    http://docs.oracle.com/javase/tutorial/reflect/index.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an application. I have a servlet (writeDataBase.class) that writes some information
I have a servlet that does some business login and then redirects to a
I have deployed some jax-ws webservices in a tomcat: web.xml: ... <servlet> <servlet-name>WebServiceJaxWs</servlet-name> <servlet-class>...a
I have a servlet code which calls a ejb stateful session bean code as
I have an object that implements ServletContextListener which, according to the Java EE servlet
I'm new to learning JSPs and Servlets. I have a Java class which calls
I have a servlet running on GAE and I have a cron job that
We have a servlet which occupies more virtual memory on the server due to
We have a servlet which occupies more virtual memory on the server as it
I have a servlet which takes us to an existing jsp, say home.jsp. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.