# allg. Java Class mit JSP starten



## tu-besucher (14. Sep 2004)

Hallo,
aus meiner Webseite (.jsp) aus möchte ich eine allgemeine Java-Klasse, die keine Servlets und keine Beans ist, aufrufen. Ich hab den Code (.java und .class) in /WEB-INF/classes (Tomcat) gespeichert. diese Klasse soll ein Tool starten:

```
... main(...)
        { Connector con = new Connector();
          con.connect();
          con.start(); ..}
```

wie kann ich diese Klasse benutzen um das Tool online zu starten? gibt's Möglichkeiten diese Klasse in JSP einzubinden?

Ich Danke Euch


----------



## tu-besucher (14. Sep 2004)

kann jemand bitte bitte mir helfen !!

vielen Dank


----------



## foobar (14. Sep 2004)

Instanzier die Klasse doch einfach:

```
<% new MyClass().machWas(); %>
```


----------



## tu-besucher (14. Sep 2004)

Danke,
ich habe aber die folgende Fehhermeldung:


C:\jakarta-tomcat-4.1.30-LE-jdk14\work\Standalone\localhost\examples\Arbeit\BearbZeit_jsp.java:79: cannot resolve symbol
symbol  : class main 
location: class diplompack.Connector
 new Connector.main();
                     ^
1 error

weiss jemand woran das liegen könnte ?

Vielen Dank


----------



## foobar (14. Sep 2004)

Um eine statische Methode aufzurufen, brauchst du keine Instanz zu erzeugen. Beschreib doch mal etwas genauer was du machen willst, denn aus einer JSP-Seite die Main-Methode einer anderen Klasse aufzurufen ist etwas merkwürdig.


----------



## meez (14. Sep 2004)

Zudem solltest du KEINE Methode main nennen, die nicht wirklich eine main Methode ist...


----------



## Guest (16. Sep 2004)

Also, 
Meine Java Klasse sieht so aus: 

```
package diplompack;
import ...
public class EMPlantConnector extends Thread
{
   private static EMPlantConnector m_Instance;
   private ....
 
   /** native method declaraction  **/
   private static native int connect(String servername, String topic);
   private static native int disconnect(int channel);
   private static native String request(int channel, String objectpath);
   private static native int poke(int channel, String objectpath);
   private static native int execute(int channel, String instruction);   
   
   /** end of native method declaraction**/
   private final static int DDEEXECUTE = 0;
   private final static int DDEREQUEST = 1;
   private final static int DDEPOKE = 2;
   public final static int ERRMISSINGCONNECTION = -1;
   private PipedWriter out;
   private PipedReader in;
   boolean write_end_dead;
   
   static {
      System.loadLibrary("daisiytoemplant");
   }
   // Constructor.   
   public EMPlantConnector()
   {
      EMPlantConnector con = new EMPlantConnector();
      con.connect();
      String test = con.request(".Modelle.Netzwerk.Auftragsdaten");
      System.out.println("eM-Plant- Object::" + test);
      //con.execute(".Modelle.Netzwerk.Ereignisverwalter.start()");
      con.disconnect();
    }
   public EMPlantConnector(PipedWriter out)
   {
      in = null;
      write_end_dead = false;
      try
      {
         this.out = new PipedWriter();
         this.in = new PipedReader(out);
      }
      catch (IOException ioexception)
      {....
      }
   }

   private synchronized Object process(int type, String cmd)
   {...}
   /** Connect to eM-Plant. */ --
   public int connect() ---- > Hier ist das Problem, das ich nicht lösen kann !! :)
   {
      if (connected)
         return 0;
      int status = connect("eM-Plant", "");
      connected = true;
      return status;
   }
   /**  Disconnect the Connection to eM-Plant.  */
   public int disconnect()
   {
      if (connected)
      {
         connected = false;
         return disconnect(m_Channel);
      }
      return -1;
   }

   /** Send a instruction to eM-Plant. */   
   public int execute(String cmd)
   {
      return ((Integer) process(DDEEXECUTE, cmd)).intValue();
   }

   // Set field value for the simulation model. 
   public int poke(String cmd)
   {
      return ((Integer) process(DDEPOKE, cmd)).intValue();
   }

   // Request a field value from the model.
   public String request(String cmd)
   {
      return (String) process(DDEREQUEST, cmd);
   }
          ....
   public void run()
   {
      BufferedReader br = new BufferedReader(in);
      PrintWriter pw = new PrintWriter(out);
      boolean exit = false;
      connect();
      while (!exit); ....
      disconnect();
   }
public static void main(String args[])
   {
      new EMPlantConnector();
   }
}
```
Von einer Webseite aus (.jsp mit Tomcat) möchte ich diese Klasse aufrufen um die Verbindung (connect()) zu eM-Plant herzustellen.

```
+++++ meineKlasse.jsp ++++
<%@ page language="java" import="diplompack.*, java.util.*, java.io.*" %>
....
<% EMPlantConnector con = new EMPlantConnector(); %>
 /*** Oder so ???
<% String[] args = { };
  diplompack.EMPlantConnector.main(args);
%>
```
bin für jeden Tipp dankbar
mfg


----------



## Bleiglanz (16. Sep 2004)

lies den entsprechenden thread auf jsp-develop.de


----------



## foobar (16. Sep 2004)

```
<%@ page language="java" import="diplompack.*, java.util.*, java.io.*" %>
<% EMPlantConnector con = new EMPlantConnector(); %>
<% con.connect(); %>
```

BTW Die Main-Methode ist nur dazu da um das Programm von der Konsole aus zu starten. Wenn du die Klasse in einer JSp-Seite verwenden willst, mußt du dir einach eine Instanz der Klasse erstellen und dann die entsprechenden Methoden aufrufen.


----------



## Guest (16. Sep 2004)

vielen Dank für eure Tipps


----------



## Guest (27. Sep 2004)

Hi,
Tomcat zeigt jetzt eine Fehlermeldung wegen die Connection, die Methode connect() kann nicht aufgerufen werden.
weiss jemand woran das Problem liegen könnte?

Danke
mfg


----------



## foobar (27. Sep 2004)

Poste doch mal die Fehlermeldung.


----------



## Guest (27. Sep 2004)

Hier ist die Fehlermeldung:

```
type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException: connect
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:551)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:711)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:687)
	at java.lang.Thread.run(Thread.java:534)


root cause 

javax.servlet.ServletException: connect
	at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:536)
	at org.apache.jsp.Ergebnis1_jsp._jspService(Ergebnis1_jsp.java:97)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:137)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:210)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:551)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2422)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:163)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:641)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:174)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:643)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:480)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:995)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:199)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:711)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:584)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:687)
	at java.lang.Thread.run(Thread.java:534)
```

Vielen Dank


----------

