打開另外一個窗口,我想我已經解決了一個問題,我發現這個解決方案稍早公佈:Javascript Post on Form Submit open a new window。但是,它並不完全符合我的要求,我無法弄清楚缺少的是什麼。需要有條件地從servlet的
這裏的問題...... 我有它一個表單的jsp。當表單被提交時(通過JavaScript),我希望父窗口總是重定向到另一個頁面。但是,根據用戶的輸入值,我還想CONDITIONALLY打開另一個窗口並將THAT窗口重定向到其他頁面。所以,總是重定向父,有時打開和重定向的孩子。
我在JSP代碼:
<script>
function handleFormSubmit() {
var form = document.getElementById("myForm");
...
if (checkbox.checked) {
// this code creates the child window, but ONLY if the checkbox is checked
myForm.setAttribute("target", "newResultPage");
window.open("childpage.jsp", "newResultPage", "width=850,height=550,...");
}
form.submit();
}
</script>
而這裏的HTML表單:
<form id='myForm' method='post' action='someAction' accept-charset='UTF-8'>
// some hidden input elements here
</form>
並且servlet邏輯:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String redirectedPage = "/parentPage";
if (someCondition) {
redirectedPage = "/childpage.jsp";
}
RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher(redirectedPage);
reqDispatcher.forward(request,response);
}
}
當條件不滿足,一切工作正常。調用servlet並將調用頁面重定向到parentPage。但是,如果條件滿足,servlet會在新的子窗口中正確打開childpage.jsp,但它不會將調用者頁面重定向到parentPage。
我試過添加一個明確的重定向(「parentPage.jsp」);後form.submit();,但這會產生競爭條件,並不是一個好的解決方案,因爲在提交表單之後不應調用任何內容。
我也嘗試修改servlet的內部邏輯,且僅當檢測到該條件重定向。但是,如果條件不存在,則會導致問題,因爲servlet不知道下一位向何處發送用戶。
有我的方式,以多種形式傳遞給servlet和通過Java處理呢?任何幫助將不勝感激。
-Bob