Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

dynamic class loading from web server

Posted on 2004-10-22
4
Medium Priority
?
275 Views
Last Modified: 2013-11-24
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?
0
Comment
Question by:ozan_aksit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 375 total points
ID: 12378901
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12379200
B grade?

Bah!  What was wrong with it?
0
 

Author Comment

by:ozan_aksit
ID: 12379257
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
 
LVL 35

Expert Comment

by:TimYates
ID: 12379276
Good luck with it :-)
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question