我今天要做的是直接從JSP Servlet響應中調用一個Javascript函數。那是什麼意思?下面的代碼:從JSP Servlet中調用Javascript函數forward
的Servlet,這是包含在myServlet.java
// Takes an XML already previously parsed as a string as input
CharSequence confirm= "something";
if (xml.contains(confirm)) {
// Carry on
// If it is contained we needn't go further
} else {
// "couldn't find confirms content
errorMessage = "Does not contain confirm";
// "If^quit this servlet"
request.setAttribute("rol", this.rol);
request.setAttribute("user", this.user);
request.setAttribute("errorMessage", errorMessage);
forwardToJSP(request, response, "/myJSP.jsp");
}
現在在Javascript中,我已經定義了我想要一個函數被調用。它是沿着這些路線的東西:
JavaScript中,這是包含在myJSP.jsp
<script language="JavaScript" type="text/JavaScript">
// Assume all document.getElementById calls are properly implemented,
// they call real variables that exist elsewhere, but not shown
// here due to irrelevance.
window.errorExists = function() {
var errorExists = document.getElementById("terror");
errorExists.setAtribute("terror", "Does not contain confirm");
if (errorExists.attributes == "Does not contain confirm"){
if(confirm("Cannot find content, do you wish to add it?")){
anotherFunctionCall();
} else {
// Don't do anything
}
}
}
</script>
現在,簡單來說就是我想要做的是從上述JSP調用我的JavaScript函數。我覺得不能變通,如這樣做:
PrintWriter out = response.getWriter();
out.println("<tr><td><input type='button' name='Button' value='Search' onclick=\"searchRecord('"+ argument + "');\"></td></tr>");
因爲其他功能就不叫,也有一個更新的Javascript變量。
如何解決這個問題?
任何和所有的幫助,非常感謝。
此javascript方法已定義或可從myJSP.jsp文件中獲取? –
如果您轉發到JSP,則可以將JSP屬性直接放置到JavaScript。 –