Hi experts, I got problem in servlet. I have a web page that has two buttons (submit button and update button) , both buttons call the servlet. my question is how I let my servlet program distinguish the two buttons when one of the two buttons is pressed. the servlet contain two different contents , each content will run once the user click one button.

to be more specified, is there something like handling events between servlet and a web page like what we see in java GUI

Recommended Answers

All 2 Replies

There is no such event for html button action. You have to use getParameter() method of HttpServletRequest class.
sample.html

<html>
   .....
   <body>
     <form method="post" action="FirstServlet">
       ....
       <input type="submit" name="cmd" value="Update"/>
       <input type="submit" name="cmd" value="Delete"/>
     </form>
   </body>
 </html>

FirstServlet.java

....
public class FirstServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
         String cmd=request.getParameter("cmd");
         if(cmd!=null) {
            if(cmd.equals("Update")) {
                  ... statements...
            }
            else
            if(cmd.equals("Delete")) {
                  ... statements...
            }
 
         }
    } 
protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 
    
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
}

thank you I got the point

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.