0

I have an action class with a method serverstate where I am parsing few server details and weblogic command to get the current status of weblogic servers.The code below gives the required output in eclipse console and not on the GUI could someone please help regarding this, I want to display the output on the GUI(JSP) page coded by me by using sessions.

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);     
}

this method gives me the output in eclipse console and I want to display the same output in the Jsp page created by me using sessions.please help

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user1795999
  • 301
  • 2
  • 5
  • 15
  • Store the necessary data that you want to display as request attribute. Then, in your JSP, retrieve it using Expression Language `${}` and usage of some library like JSTL and print it accordingly. – Luiggi Mendoza Sep 03 '14 at 15:43
  • so I would use request.setattribute in my java code and getattribute in my JSP? – user1795999 Sep 03 '14 at 15:45
  • In servlet, you use `request.setAttribute`. In view (JSP, Facelets), you **NEVER** use scriptlets, instead use Expression Language that will call `request.getAttribute` for you. – Luiggi Mendoza Sep 03 '14 at 15:46
  • hmmm, could you please give me an example – user1795999 Sep 03 '14 at 15:49

1 Answers1

2

In your Servlet, you set the necessary data you want/need to display as request attributes:

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);
}

Then in your view (JSP), you retrieve it by Expression Language:

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

If the data you want/need to append comes from database and you want to prevent XSS attacks on your site, it would be better using a third party library to display it like JSTL:

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

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332