使用onclick事件将两个参数提交到同一个servlet中

Using onclick event to submit 2 parameters into a the same servlet

本文关键字:提交 参数 同一个 servlet 两个 事件 onclick 使用      更新时间:2023-09-26

我正试图使用一个提交按钮,使用jsp将两个参数userIdfriendId传递到同一个servlet中。我能够获得friendId,但是,我不能获得userId

function anotherPageServlet(servletName) {
    document.forms[0].method = "post";
    document.forms[0].action = servletName;
    document.forms[0].submit();
}
function anotherPageServlet1(servletName) {
    document.forms[0].method = "post";
    document.forms[0].action = servletName;
    document.forms[0].submit();
}
<input style="color:#3333ff" 
       type="submit" 
       class="rsubmit" 
       value="<%= retrieving.getDisplayname()%> :" 
       onclick="anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute("userId") %>'),
anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>')"
>

您可能只需要转义引号:

onclick="anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute('"userId'") %>'),
anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>')"

我不习惯JSP,但在JSF中,您宁愿将值分配给请求范围的bean,然后调用与该bean一起工作的函数,而不是直接传递这些参数。

编辑:

首先定义onclick函数,然后只调用简单引用。

function servletFoo() { 
    anotherPageServlet('ViewingFriendServlet?stringParameter=<%= session.getAttribute("userId") %>');
    anotherPageServlet1('ViewingFriendServlet?stringParameter=<%=retrieving.getoID() %>');
}

然后在您的HTML:中

<input ... onclick="servletFoo()" />