如何在不使用表单的情况下从jsp调用servlet

发布于 2025-01-04 14:03:54 字数 3059 浏览 0 评论 0原文

这可能是一个重复的问题,我很抱歉。我有一个 jsp 页面,其中有一些按钮。每个按钮都有自己的 servlet 来调用。我想知道是否有任何方法可以在不使用表单的情况下调用这些 servlet,因为用户可以选择给定的 3 个功能中的任何一个。我还需要将一个值从 jsp 页面传递到我调用的 servlet。

<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>  

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>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

書生途 2025-01-11 14:03:54

我在这里看到 2 个选项:

  1. 在发送 POST 请求之前,使用 JavaScript 根据每个按钮更改表单的 URL;

  2. 对所有 3 种情况使用表单和相同的 servlet。在 servlet 中,您应该确定按下了哪个按钮(它们的值作为请求参数传递),然后相应地继续。

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.

好倦 2025-01-11 14:03:54

您可以使用 Ajax 来执行此操作,或者使用更糟糕但可运行的方式来执行此操作,您可以更改当前窗口 url 并重新加载页面。

对于 Ajax 调用,我建议您使用 ExtJS。 请参见 Ext.Ajax 用法

其他方法 :更改 href, 重新加载窗口

You can use Ajax to do this or a worse but runnable way, you can change the current window url and reload the page.

For Ajax call I recommend for you to use ExtJS. See Ext.Ajax Usage

For the other ways: change the href, reload the window

伴我老 2025-01-11 14:03:54

您还可以使用 Javascript,当用户点击按钮时调用脚本并动态更改 url 并提交该表单。
这是一个示例......

    <form action = "something".....>
    your stuff here....
    <button type="button" onclick="runConfiguration(actionName)">Run</button>  
    <button type="button" onclick="editConfiguration(actionName)">Edit</button>
    <button type = "button" onclick = "deleteConfiguration(actionName)">Delete</button> 

并且在您的脚本中

    <script type="text/javascript">  
        function runConfiguration(actionName){  
            var config=${dataValues.get(0)};
            goToPage(actionName);
            //call servlet  
        }  
        function editConfiguration(actionName){  
            var config=${dataValues.get(0)};
             goToPage(actionName)  
            //call servlet  
        }  
        function deleteConfiguration(actionName){  
            var config=${dataValues.get(0)};
             goToPage(actionName)  
            //call servlet  
        } 

function gotopage(actionname)
{   
        document.formName=actionname;
        document.formName.submit();
} 
    </script>  

You can also use Javascript and at the time when user hit a button call the script and dynamically change the url and submit that form.
Here is one Example.....

    <form action = "something".....>
    your stuff here....
    <button type="button" onclick="runConfiguration(actionName)">Run</button>  
    <button type="button" onclick="editConfiguration(actionName)">Edit</button>
    <button type = "button" onclick = "deleteConfiguration(actionName)">Delete</button> 

And in Your Script

    <script type="text/javascript">  
        function runConfiguration(actionName){  
            var config=${dataValues.get(0)};
            goToPage(actionName);
            //call servlet  
        }  
        function editConfiguration(actionName){  
            var config=${dataValues.get(0)};
             goToPage(actionName)  
            //call servlet  
        }  
        function deleteConfiguration(actionName){  
            var config=${dataValues.get(0)};
             goToPage(actionName)  
            //call servlet  
        } 

function gotopage(actionname)
{   
        document.formName=actionname;
        document.formName.submit();
} 
    </script>  
梦屿孤独相伴 2025-01-11 14:03:54

请在 JSP 和 JSP 中指定操作字符串。在您的 servlet 中检查它。

根据该字符串操作,您可以在 servlet 代码中设置条件,以确定您希望该特定操作执行什么操作。
请参阅下面,这是我在代码中所做的。

例如

<A href="<%=request.getContextPath()%>/JobAction?action=runConfig">RUN</a>

<a href="<%=request.getContextPath()%>/JobAction?action=editConfig">EDIT</a>

,您可以只调用一个 servlet,而不是调用许多 servlet。
在您的 servlet 中使用这样的代码。

String action = request.getParameter("action");
if (action.equalsIgnoreCase("runConfig")) {
    // Specify what you want to do
} else if (action.equalsIgnoreCase("editConfig")) {
    // Specify what you want to do
} else if (action.equalsIgnoreCase("deleteConfig")) {
    // Specify what you want to do
}

Please specify the action string in your JSP & check for it in your servlet.

Depending on that string action you can set the condition in your servlet code, as to what action you want that particular action to perform.
Please see below this is what i have done in my code.

FOR EXAMPLE

<A href="<%=request.getContextPath()%>/JobAction?action=runConfig">RUN</a>

<a href="<%=request.getContextPath()%>/JobAction?action=editConfig">EDIT</a>

Instead of calling many servlets, you can just call one servlet.
Inside your servlet use code like this.

String action = request.getParameter("action");
if (action.equalsIgnoreCase("runConfig")) {
    // Specify what you want to do
} else if (action.equalsIgnoreCase("editConfig")) {
    // Specify what you want to do
} else if (action.equalsIgnoreCase("deleteConfig")) {
    // Specify what you want to do
}
爱冒险 2025-01-11 14:03:54

你可以使用这个:

<a href="<%=request.getContextPath()%>/logout"> Logout </a>

如果你想添加一些引导程序,你所要做的就是

<a href="<%=request.getContextPath()%>/logOut"> 
    <button class="btn btn-outline-secondary" type="button">log out </button>
</a>

You can use this :

<a href="<%=request.getContextPath()%>/logout"> Logout </a>

If you wanna add some bootstrap to it, all you have to do is

<a href="<%=request.getContextPath()%>/logOut"> 
    <button class="btn btn-outline-secondary" type="button">log out </button>
</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文