2013-02-13 43 views
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是調用類的構造函數的函數?我想打電話給我想一個轉發JSP到其它的按鈕被點擊時,這樣的.java文件,另一個JSP調用的.java如何在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) 
+0

我猜你的意思是'$ {link.callconst()}'。另外,正如其他問題答案中所述,您應該移動'link'代碼** outse **''標籤。 – 2013-02-13 05:38:10

+0

我們不知道,callconst是什麼;) – bsiamionau 2013-02-13 05:41:15

+1

通過反覆詢問同一個問題來阻止垃圾郵件。你的上一個[問題](http://stackoverflow.com/questions/14846849/how-to-call-file-in-to-jsp) – SudoRahul 2013-02-13 05:43:26

回答

1

callconst創建一個屬性和它的二傳手getter類com.Sshexample.java 像下面的代碼。

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類的幫助下小腳本,讓我寫代碼

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

    <% 

Sshexample obj = new Sshexample(); 

String dataTodisplay = obj .getCallconst(); 


%> 


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

    </body></html> 
+0

真正的我認爲這個代碼在Java類 – ricky 2013-02-13 05:53:20

+0

所以你會得到任何異常或沒有顯示 – 2013-02-13 05:55:12

+0

其實我是新的行業,並沒有太多的實踐知識只是主題的概述,所以你可以詳細說明setter getter? – ricky 2013-02-13 05:58:06