2014-09-03 79 views
0

我有一個操作類,其中有一個方法serverstate,我正在解析幾個服務器細節和weblogic命令以獲取weblogic服務器的當前狀態。下面的代碼給出了所需的輸出Eclipse的控制檯,而不是在圖形用戶界面可能有人請幫助這一點,我想顯示在我編寫的GUI(JSP)頁面使用會話的輸出。如何在java中使用會話顯示方法的輸出

public void serverstate(HttpServletRequest request,HttpServletResponse response) throws IOException, Exception 

{ 
String appname; 
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); 
String selapp = " "; 
System.out.println("enter appname:"); 
selapp = dataIn.readLine(); 
System.out.println("selected app is:" + selapp); 
{ 
try 
{ 

     File apps = new File("/WEB-INF/appdetails.xml"); 
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
     Document doc = dBuilder.parse(apps); 
     doc.getDocumentElement().normalize(); 

     System.out.println("server status of " + selapp); 
     NodeList nodes = doc.getElementsByTagName("app"); 
     System.out.println("=========================="); 

     for (int i = 0; i < nodes.getLength(); i++) 
     { 
      Node node = nodes.item(i); 

     if (node.getNodeType() == Node.ELEMENT_NODE); 
     { 
     Element element = (Element) node; 
     appname = getValue("name", element); 
     System.out.println(appname); 
     String user = getValue("uname", element); 
     System.out.println(user); 
     String server = getValue("server", element); 
     System.out.println(server); 
     String command = getValue("cmd", element); 
     System.out.println(command); 
     String passwd = getValue("passwd",element); 
     System.out.println(passwd); 
     String cmd= getValue("cmd",element); 
     System.out.println(cmd); 
     String port=getValue("port",element); 
     String clu=getValue("cluster",element); 
     String admin=getValue("admstate",element); 
     System.out.println(admin); 
     if (selapp.equalsIgnoreCase(appname)) 
    { 
    try { 

      Runtime rt = Runtime.getRuntime(); 
       //Process pr = rt.exec("cmd /c dir"); 
      { 
       Process pr = rt.exec(""+ command +" && "+ admin +" && java weblogic.Admin -url "+ server +":"+ port +" -username "+ user +" -password "+ passwd +" CLUSTERSTATE -clusterName "+ clu +""); 

       BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream())); 

       String line=null; 

       while((line=input.readLine()) != null) { 
        System.out.println(line); 
       // int exitVal = pr.waitFor(); 
       //System.out.println("Exited with error code "+exitVal); 
       }}}finally{ 
       }}else { 
        System.out.println("no app selected"); 
       }}}}catch(Exception e) { 


        System.out.println(e.toString()); 
        e.printStackTrace(); 
        }} 
RequestDispatcher dispatcher = request.getRequestDispatcher("status.jsp"); 
dispatcher.forward(request,response);  
} 

這種方法讓我在Eclipse控制檯輸出和我想用sessions.please幫助

+0

存儲您希望顯示爲請求屬性的必要數據。然後,在您的JSP中,使用表達式語言'$ {}'檢索它並使用像JSTL這樣的庫並相應地打印它。 – 2014-09-03 15:43:06

+0

所以我會在我的java代碼中使用request.setattribute並在我的JSP中獲取屬性? – user1795999 2014-09-03 15:45:12

+0

在servlet中,使用'request.setAttribute'。在視圖(JSP,Facelets)中,您絕不**使用scriptlet,而是使用表達式語言來爲您調用'request.getAttribute'。 – 2014-09-03 15:46:09

回答

2

在servlet來顯示我創建JSP頁面相同的輸出,你設置你所需要的數據想/需要顯示爲請求屬性:

<!DOCTYPE html> 
<html> 
    <body> 
     Name: ${name} 
    </body> 
</html> 

public void serverstate(HttpServletRequest request,HttpServletResponse response) 
    throws IOException, Exception { 
    /* 
     ... 
    */ 
    //just a small example: 
    request.setAttribute("name", "Luiggi Mendoza"); 
    RequestDispatcher dispatcher = request.getRequestDispatcher("status.jsp"); 
    dispatcher.forward(request,response); 
} 
在視圖(JSP)

然後,通過表達式語言檢索

如果你想/需要追加來自數據庫,並要避免網站上的XSS攻擊,它會使用第三方庫,像JSTL顯示是更好的數據:

<!DOCTYPE html> 
<html> 
    <body> 
     Name: <c:out value="${name}" /> 
    </body> 
</html> 

更多信息:

相關問題