如何在java中使用会话显示方法的输出

时间:2014-09-03 15:41:30

标签: java jsp servlets

我有一个使用方法serverstate的动作类,我在其中解析一些服务器细节和weblogic命令以获取weblogic服务器的当前状态。下面的代码在eclipse控制台中提供所需的输出而不是在GUI上可能有人请帮助对此,我想通过使用会话在我编码的GUI(JSP)页面上显示输出。

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 help

创建的Jsp页面中显示相同的输出

1 个答案:

答案 0 :(得分:2)

在Servlet中,您可以设置所需/需要显示的必要数据作为请求属性:

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)中,通过表达式语言检索它:

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

如果您想要/需要追加的数据来自数据库并且您想要阻止对您网站的XSS攻击,那么最好使用第三方库来显示它,就像JSTL一样:

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

更多信息: