0

I have a JSP which has a form with a submit button (with value "Start Server"). When I click on this button it calls a method (java code) and starts up a server. Now the button value is changed to "Stop Server" based on a boolean method "getServerStatus()"

My jsp is as follows

<form class="form" id="hubForm" method="post" action="/ServManager/servmgr">
    <c:if test="${requestScope.servStatus == 'true'}">
        <input type="submit" name="action" id="stopServer" value="Stop Server" />
    </c:if>
    <c:if test="${requestScope.servStatus != 'true'}">
        <input type="submit" name="action" id="startServer" value="Start Server" />
    </c:if>
</form>

My servlet is as follows

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getRequestDispatcher("/servmanager.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ServManager servmgr = new ServManager();
    String action = request.getParameter("action");
    if(action.equalsIgnoreCase("start server")) {
        servmgr.startServer();
    }
    else {
        servmgr.stopServer();
    }
    boolean servStatus = servmgr.getServerStatus();
    request.setAttribute("servStatus", servStatus);
    request.getRequestDispatcher("/servmanager.jsp").forward(request, response);
}

getServerStatus() is as follows

public boolean getServerStatus()
{
    return blStatus;
}

My questions is

  1. When I start the webpage it always has the button displayed as "Start Server" even though the server is running. How do I check the status of the server using getServerStatus() method and display "Stop Server" button when the page is loaded and if the server is already running when the page is loaded
  2. Did I do the other stuff right in this page?

Pls help. Thanks and Happy new year!

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Damien-Amen
  • 5,544
  • 10
  • 36
  • 64
  • @mplungjan : yes it's a boolean. It seems to be working though. I'll change it to true instead of 'true' – Damien-Amen Jan 01 '15 at 16:39
  • what is ServManager, Does status defaulted to false? – kamoor Jan 01 '15 at 16:39
  • ServManager is the class that has the start and stop server methods including the getServerStatus() method. – Damien-Amen Jan 01 '15 at 16:42
  • Could you please post that class. I am wondering how do you stop. If stopped, How do you start? – kamoor Jan 01 '15 at 16:43
  • ` id="stopServer" value="Stop Server" /> id="startServer" value="Start Server" /> ` To get the status later you will need to either ajax or load an image that if successful lets the script know the server is running in the onload event – mplungjan Jan 01 '15 at 16:46
  • @kamoor : if you see my servlet code. The server is started and stopped based on the displayed value on the button. If the button's value is "start server" the startServer() method is executed. If the button's value is "stop server" the stopServer() method is executed. – Damien-Amen Jan 01 '15 at 16:46
  • @mplungjan : My start server and stop server is working fine based on the button value. But my question is when I load the page I need to check if the server is already running and display the value on the button accordingly. – Damien-Amen Jan 01 '15 at 16:48
  • You need to split this question. 1) if you use the correct code in the correct manner the button will show the correct value 2) you need a different technique to see if the server is running. If the duplicate answer works for you, delete the question and ask a new one about how to get the status from the server in an already loaded page – mplungjan Jan 01 '15 at 16:50
  • @mplungjan : yes that's my question. What's that technique to see if the server is running. – Damien-Amen Jan 01 '15 at 16:59
  • If someone able to hit /servmanager.jsp and see the screen then server is running. – kamoor Jan 02 '15 at 00:51
  • @kamoor : seriously? and you wanted to post this as an answer? – Damien-Amen Jan 03 '15 at 01:51

0 Answers0