0
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<jsp:useBean id="link" class="com.Sshexample">

<%= link.callconst()%>

</jsp:useBean>

</body>

</html>

我想从 jsp 调用一个 .java 文件。callconst 是一个调用类的构造函数的函数?我想在单击按钮时调用 .java 文件,所以我想将一个 jsp 转发给另一个,将另一个 jsp 调用转发给 .java

  package com;
  import com.jscape.inet.ssh.*; 
  import com.jscape.inet.ssh.util.SshParameters;   

  public class Sshexample implements SshListener {  

  // state of SSH connection  
  private boolean connected = false;  

  /** 
   * Creates a new SshExample instance. 
   * 
   */  
  public Sshexample() {  
     Ssh ssh = null;       

     try
       {  
        // create new Ssh instance  
        SshParameters params = new SshParameters("i210","ashishga","Linux_11");  
        System.out.print("Unix server is connected ");  
        ssh = new Ssh(params);  
        // register to capture events  
        ssh.addSshListener(this);  

        System.out.println("Connecting please wait...");  
        SshScript script = new SshScript(ssh);  
        SshTask task = new SshTask("$","./call.sh","$");  

        // connect  
        script.addTask(task);  
        ssh.connect();  
        while(!script.isComplete()) {  
             Thread.sleep(100);  
            }  

        } catch (Exception e) {  
        e.printStackTrace();              
        } finally {  
        try {  
            if(connected) {  
              ssh.disconnect();   
            }  
        } catch(Exception e) {  

         }             
      }  
  }  


  /** 
  * Captures SshConnectedEvent 
  */  
 public void connected(SshConnectedEvent ev) 
 {  
    System.out.println("Connected: " + ev.getHost());  
    connected = true;  
    System.out.println("New ashish is connected to server  "+ ev.getHost());   
}  

/** 
 * Captures SshDataReceivedEvent 
 */  
public void dataReceived(SshDataReceivedEvent ev) {  

    ev.getData();  

}  


/** 
 * Captures SshDisconnectedEvent 
 */  
public void disconnected(SshDisconnectedEvent ev) {  

    System.out.println("ashish server is Disconnecting: " + ev.getHost());  
    connected = false;  
}  

/* 
 * Main method for SshExample 
 */  
public void callconst()
{
   Sshexample t = new Sshexample();
}

}  

错误:

javax.servlet.ServletException:
java.lang.NoClassDefFoundError:                                                            com/jscape/inet/ssh/SshListener
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:268)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
4

1 回答 1

1

在类com.Sshexample.java中创建一个属性callconst及其 setter getter, 如下面的代码。

package com;

public class Sshexample {
    private String callconst = "DEAFULT DATA TO DISPLAY";

    public String getCallconst() {
        return callconst;
    }

    public void setCallconst(String callconst) {
        this.callconst = callconst;
    }


}



Access the property like below code.
 <jsp:useBean id="link" class="com.Sshexample" scope="request">          
        </jsp:useBean>

<html>  
<body>  
Data from java class :: <jsp:getProperty name="link" property="callconst"/>  

</body></html> 

访问您的 java 类的另一种方法是借助 scriptlet 让我编写代码

<%@ page import="com.Sshexample" %>

    <%

Sshexample obj = new Sshexample();

String dataTodisplay = obj .getCallconst();


%>


<html>  
    <body>  
    Data from java class :: <%=dataTodisplay%>

    </body></html> 
于 2013-02-13T05:44:52.247 回答