Multiple method calling using single servlet [duplicate]

谁说胖子不能爱 提交于 2019-12-02 10:03:11

You can just give the submit button a specific name.

<input type="submit" name="action1" value="Invoke action 1" />
<input type="submit" name="action2" value="Invoke action 2" />
<input type="submit" name="action3" value="Invoke action 3" />

The name-value pair of the pressed button is available as request parameter the usual way.

if (request.getParameter("action1") != null) {
    // Invoke action 1.
}
else if (request.getParameter("action2") != null) {
    // Invoke action 2.
}
else if (request.getParameter("action3") != null) {
    // Invoke action 3.
}

Hidden fields in a JSP are the same as in HTML:

<input type="hidden" name="name" value="value">

Then in your servlet you can do

if (request.getParameter("name").equals("value")) { /* do something */ }

Depends on which method you want to call. Assuming that the URL Pattern declared for your servlet in web.xml is /myservlet*,

For doGet, just use a URL

http://localhost:8080/myservlet?name=value

For doPost, use a form.

<form action="/myservlet" method="post">
    <input type="text" value="value" name="name" />
</form>

i dont think having 3 buttons is the best solution,basis on the logic and parameters method asked,
i believe this can be an accurate solution-

On the basis of parameters passed ,we can have 2 methods to access different actions via javascript-
1)getting values from user,checking them in javascript.
2)getting values from user,checking them in javascript,assigning values to hidden variables accordingly,calling servlets and using those hidden values.i have elaborated method1.

    <html>
<head>
    <script type="text/javascript">
    function nawab() {
    param = document.getElementById('param1').value;
    alert('in nawab');
    if (param != "") {
    if (param === 'abc') {
          alert('abc');
          document.forms[0].action = "nawabServlet";
          document.forms[0].submit();
    }
    if (param === 'def') {
     alert('def');
     document.forms[0].action = "nawabServlet2";
     document.forms[0].submit();
     }          
}           
else{
      alert('empty');
      document.forms[0].action = "nawabServlet";
      document.forms[0].submit();
     }
 }
 </script>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <title>nawab has come</title>
 </head>
 <body>
  <form>
        param1:<input type="text" name="param1" id="param1"></select> 
        <input type="submit" onclick="nawab()">
   </form>
  </body>
</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!