Monday 15 April 2013

Java: how to check in servlet which html button was pressed


We can define two buttons in the html page:

<input type="submit" value="Add" name="addbutton">
<input type="submit" value="Search" name="addbutton">



Then in the servlet in the get or post method get the values for the buttons. Button pressed will have a value and the one not pressed will be null:

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String add = request.getParameter("addbutton");
        String search = request.getParameter("searchbutton");

        if (add != null) {
            response.sendRedirect("addperson.html");
        } else if (search != null) {
            response.sendRedirect("searchperson.html");
        }
    }

No comments:

Post a Comment