2014-09-03 78 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> 

更多信息:

相关问题