mapserver java servlets

Umberto Nicoletti unicoletti at PROMETEO.IT
Thu Nov 25 04:58:31 EST 2004


Jose,
the point is that in tomcat (as in every servlet container) you have more THAN
ONE class loader so making a class a singleton does not guarantee that it will
be loaded once (since a different class loader could load it again)!

The following doc will be an interesting reading on this issue:
http://jakarta.apache.org/tomcat/tomcat-4.1-doc/class-loader-howto.html

(if you use tomcat 5, then go to the relative section on the web site).
The point is that you have to put the class in a directory that is handled by
the COMMON classloader. This will guarantee that you class will be loaded once
and only once during the lifecycle of tomcat.

If you put it in WEB-INF/lib or classes your
web-application-specific-class-loader will reload it if it thinks it has to or
maybe because you webadmin decides it should be so (that it also possibile), no
matter what you do to prevent it (web.xml tricks and *immutable* servlets included).

Happy coding,
Umberto

Quoting Jose Luis Gonzalez <joseluismapserver at YAHOO.ES>:

> Hi Oliver,
> I have just done what you recomended,  leaving the JavaMapscript-Class so the
> only thing he does is to load the library.
> Then in another class, the one where I have my servlet, I wote in the: public
> void init (ServletConfig cfg){
>
> JavaMapscript.getInstance();
>
> and later in the:
> public void service (HttpServletRequest req, HttpServletResponse res){
>
> I began writing my mapscript code, and it still gives me the same error:
>
> javax.servlet.ServletException: Native Library
> /opt/SUNappserver/domains/domain1/lib/ext/libmapscript.so already loaded in
> another classloader
>
> Do you know why?
>
> Thank you
>
> Jose Luis
>
> Oliver Wesp <wesp at gdv.com> wrote:
> Hi,
>
> you're on the right way to avoid the "Native library cannot be loaded
> twice" error. Your JavaMapscript-Class implements a singleton so it's
> loaded only once in Tomcat. But you should not add your mapscript code
> to this class. Whenever you change the class Tomcat tries to reload and
> complains about the library already loaded.
> Write a second class and load the library with
>
> JavaMapscript.getInstance();
>
> Add your mapscript code to that class and you should be fine.
>
> best regards
> Oliver
>
> Jose Luis Gonzalez wrote:
> > Hi to all
> > I would implement mapserver in tomcat as servlet but I have found a lot of
> > problems in start with it!
> >
> > So:
> > 1) is there sites that implement mapserver as servlet in tomcat that I can
> > view?
> > 2) Some one have a piece of basic code that I can compile as servlet in
> > tomcat for view how it run? I have load the java Howto.
> >
> > Do you have examples so I can see how can I work
> > with mapserver functions and servlets?
> > I have been seeing the wiki, but I can not really find what I want.
> >
> > I have done the example of loading a library that apears in:
> >
> > http://www.unicolet.org/mapserver/tomcat.html
> >
> >
> > and it works, but when I want to complete the JavaMapscript.java code
> > with other
> > mapserver comands, it doesn't work:
> >
> > import edu.umn.gis.mapscript.*;
> >
> > public class JavaMapscript {
> > private static JavaMapscript instance=null;
> >
> > private JavaMapscript() {
> > try {
> > System.loadLibrary("mapscript");
> > } catch (Exception e) {
> > e.printStackTrace();
> > System.err.println("* error loading native library *");
> > System.err.println("Error is: "+e);
> > }
> > System.out.println(" * mapscript native library loaded *");
> > mapObj map = new mapObj("test.map");
> > imageObj img = map.draw();
> > //img.save("result", map);*/
> > }
> >
> > public static JavaMapscript getInstance() {
> > System.out.println("Try to instantiate Mapscript");
> > System.out.flush();
> > if ( instance == null ) {
> > instance = new JavaMapscript();
> > }
> > return instance;
> > }
> > }
> >
> > And even worse, when I do any modifications and I put the new .class
> > file in the
> > server, I have an error because it says that the library was already
> > loaded, and
> > my only solution now to solve it is to turn off the server and turn it
> > on again.
> >
> > Hope someone can help me
> >
> > Thank you
> >
> > Jose Luis
> >
> > ------------------------------------------------------------------------
> >
> > Nuevo Correo Yahoo!
> >
> >
>
>
> --
> Dipl.-Geogr. Oliver Wesp
> Gesellschaft fuer geografische Datenverarbeitung
> Binger Strasse 49-51
> D-55218 Ingelheim
> fon: +49 6132 714818
> fax: +49 6132 714828
> http: www.gdv.com
>
>
> ---------------------------------
>
>



More information about the mapserver-users mailing list