dynamic class loading from web server

My program uses some classes which are not beloned its virtual machine,
in order to make it distributed, it think it is necessary broadcast some classes via web server.
URLClassLoader is useful for this purpose. but i don't know how can i deploy jar file of this classes in to tomcat server. is it is necessary writing a servlet or not. how can i?
ozan_aksitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
You can just use the standard applet classloader (so you shouldn't need to sign your applet), and write a servlet that does this:

--------------------

import java.io.BufferedInputStream ;
import java.io.BufferedOutputStream ;
import java.io.IOException ;
import javax.servlet.ServletException ;
import javax.servlet.http.HttpServlet ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

public class ClassLoaderServlet extends HttpServlet
{
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    String uri = request.getRequestURI().substring( request.getContextPath().length() ) ; ;
    String classname = uri.substring( 1 ) ;
    classname = classname.replace( '/', '.' ) ;
    classname = classname.substring( 0, classname.length() - 6 ) ;

    BufferedOutputStream bos = null ;
    BufferedInputStream bis = null ;
    try
    {
      Class c = Class.forName( classname ) ;
      if( c != null )
      {
        bos = new BufferedOutputStream( response.getOutputStream() ) ;
        bis = new BufferedInputStream( this.getClass().getResourceAsStream( uri ) ) ;
        byte[] buffer = new byte[ 64000 ] ;
        int cnt = 0 ;
        int tot = 0 ;
        while( ( cnt = bis.read( buffer, 0, 64000 ) ) != -1 )
        {
          bos.write( buffer, 0, cnt ) ;
          tot += cnt ;
        }
        System.out.println( "Loaded " + uri + " (" + tot + "bytes)" );
      }
      else
      {
        response.sendError( HttpServletResponse.SC_NOT_FOUND );
        System.err.println( "Trying to load " + classname + " which doesn't implement ServletFetchable" ) ;
      }
    }
    catch( Exception ex )
    {
      ex.printStackTrace() ;
      response.sendError( HttpServletResponse.SC_NOT_FOUND );
    }
    finally
    {
      try { if( bis != null ) bis.close() ; } catch( IOException ex ) { }
      try { if( bos != null ) bos.close() ; } catch( IOException ex ) { }
    }
  }
}

Then, assign this servlet (in web.xml) to match to all urls that match *.class

Hope this helps :-)

Tim
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimYatesCommented:
B grade?

Bah!  What was wrong with it?
0
ozan_aksitAuthor Commented:
thank you i will try to send jar file by this way.
because my classes import other clasess, and also sending them in one shot as jar file boost performance
0
TimYatesCommented:
Good luck with it :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.