0

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

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by alsoumhi
0

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);
    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.