移动到除表单操作之外的其他servlet

时间:2014-04-06 21:30:57

标签: java jsp servlets

我有一张表格:

<form action="sendaddnotification" method="post">
        <%String namee=rs.getString(2);%>
<input name="IndUserName"  type="hidden" value="<%=namee%>"/>
User Name : <%=namee%>
<br>
First Name : <%=rs.getString(4)%> 
    <br>

Last Name : <%=rs.getString(5)%>
    <br>

Email Id : <%=rs.getString(6)%>
    <br>

Contact : <%=rs.getString(7)%>
   <br>
   <%
   String groupidd = request.getSession().getAttribute("groupid").toString();
   s=null;
   rs=null;
   int flag=0;
   String sql="select * from TBGROUPUSERS where I_ID=? and GU_GROUPID=?";

    s = con.prepareStatement(sql);
    s.setString(1,idperson);
    s.setString(2,groupidd);
    rs=s.executeQuery();
    if(rs.next())
    flag=1;
    request.setAttribute("flag", flag);
   %>

<c:choose>
<c:when test="${requestScope.flag == 1}">
    <!-- flag is 1 -->
    <input type="submit" value="REQUEST SENT" disabled="disabled"></input>
    <a href="CancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="return confirm('Are you sure you want to cancel the request?');">
    <input type="submit" value="CANCEL REQUEST"></input>
    </a>

</c:when>
<c:otherwise>
    <!-- flag isn't 1 -->
    <input type="submit" value="ADD"></input>
</c:otherwise>

</c:choose>


<a href="searchdatabasetoadd.jsp"><input type="button" value="BACK"></input></a>

</form>

现在,在这部分代码中:

<a href="CancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="return confirm('Are you sure you want to cancel the request?');">
    <input type="submit" value="CANCEL REQUEST"></input>
    </a>

我想用这个给定的参数转移到servlet CancelRequest.java。但是当表单动作转到sendaddnotification时,所以这个servlet永远不会运行。

如何点击此按钮运行它。请帮助

1 个答案:

答案 0 :(得分:0)

假设您的CancelRequest servlet在web.xml中定义为:

    <servlet>
         <servlet-name>cancelRequest</servlet-name>
         <servlet-path>packageName.CancelRequest</servlet-path>
    </servlet>
    <servlet-mapping>
         <servlet-name>cancelRequest</servlet-name>
         <url-pattern>/cancelRequest</url-pattern>
    </servlet-mapping>

将javascript函数写为:

    function cancelRequest(){
        var exit = confirm('Are you sure you want to cancel the request?');
        if(exit == true){
            document.getElementById('sendaddnotificationId').action = 'cancelRequest';
        }else{
            return;
        }
    }

在jsp中包含表单的id为:

    <form action="sendaddnotification" id="sendaddnotificationId" method="post">

将锚元素更改为:

    <a href="cancelRequest?userid=<%=idperson%>&userrnamee=<%=namee%>" onclick="cancelRequest();">
        <input type="submit" value="CANCEL REQUEST"></input>
    </a>