admin管理员组

文章数量:1317572

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>  

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>  
Share Improve this question asked Feb 13, 2012 at 6:49 user_abhuser_abh 3673 gold badges7 silver badges20 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 4

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.

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 remend for you to use ExtJS. See Ext.Ajax Usage

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

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>  

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
}

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>

本文标签: javaHow can I call servlet from jsp without using formStack Overflow