Skip to content
Advertisement

How can I call servlet from jsp without using form

This could be a repeat question, I apologize. I have a jsp page in which I have some buttons. Each button has its own servlet to call. I want to know if there is any way I can call these servlets without using form because the user may choose any of the 3 functionalities given. I also need to pass a value from the jsp page to the servlets I call.

<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
<title>Configurations</title>  
<script type="text/javascript">  
    function runConfiguration(){  
        var config=${dataValues.get(0)};  
        //call servlet  
    }  
    function editConfiguration(){  
        var config=${dataValues.get(0)};  
        //call servlet  
    }  
    function deleteConfiguration(){  
        var config=${dataValues.get(0)};  
        //call servlet  
    }  
</script>  
</head>  
<body>  
<%  
String[] label={"Master Port","Baud Rate","Char Size","Stop Bits","Parity","RTU Port","Baud Rate","Char Size","Stop Bits", "Parity"};  
int i=0;  
%>  
<br>  
<br>  
<br>  
<table align="center" border="1">  
   <td><div align="center" style="background-color: goldenrod;"><b> ${dataValues.get(0)}</b></div>   
        <table width="210" align="left" border="1">  
            <td bgcolor="goldenrod"><b> Header1 </b></td>  
            <c:forEach var="data" begin="1" end="5" items="${dataValues}" varStatus="status">  
            <tr>  
                <td><%=label[i++]%>: ${data}</td>  
            </tr>  
            </c:forEach>  
        </table>      
        <table width="210" align="left" border="1">  
            <td bgcolor="goldenrod"><b> Header2 </b></td>  
            <c:forEach var="data" begin="6" end="10" items="${dataValues}" varStatus="status">  
            <tr>  
                <td><%=label[i++]%>: ${data}</td>  
            </tr>  
        </c:forEach>  
        </table>  
   </td>  
</table>  
        <c:choose>  
        <c:when test="${dataValues.get(11)==1}">  
                <p align="center"><b><i>This configuration is already running</i></b></p>  
                <p align="center">  
            <input type="button" value="stop" onclick="StopConfiguration"/>  
                </p>  
        </c:when>  
        <c:otherwise>  
                <p align="center"><b><i>This configuration is currently NOT running</i></b></p>  
                <p align="center">                     
                    <button type="button" onclick="runConfiguration()">Run</button>  
                    <button type="button" onclick="editConfiguration()">Edit</button>  
                    <button type="button" onclick="deleteConfiguration()">Delete</button>  
                </p>  
        </c:otherwise>  
    </c:choose>  
</body>  
</html>  

Advertisement

Answer

I see 2 options here:

  1. Changing a form’s URL accordingly to each button using JavaScript, right before sendiing a POST request;

  2. Use a form and the same servlet for all 3 cases. In the servlet you should determine what button has been pressed (their values are passed as a request parameter) and then go forward accordingly.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement