[英]How can I use jsp variables in another jsp file?
我有兩個 jsp 文件,一個是 NewFile.jsp,另一個是 index.jsp
我想在 NewFile.jsp 中使用變量(在 index.jsp 中定義)。
所以我把這一行放在“NewFile.jsp”中:
<%@ include file = "index.jsp" %>
但是頁面加載了 HTTP 狀態 500 內部服務器錯誤,它說:
org.apache.jasper.JasperException: 행 [13]에서 [/index.jsp]을(를) 처리하는 중 예외 발생
10: out.println("can't access");
11: }
12: else {
13: int age = Integer.parseInt(request.getParameter("age"));
14: double height = Double.parseDouble(request.getParameter("height"));
15: boolean sex = Boolean.parseBoolean(request.getParameter("female"));
16:
它說:第[13]行是錯誤的。(它是用韓文寫的,但我想你可以理解。)
我在 NewFile.jsp 中有這些行:
function doAction(){
var req = createRequest();
if (req == null){
alert("실행이 되지 않는다!");
return ;
}
var hei = document.getElementById("height").value;
var ag = document.getElementById("age").value;
var fem = document.getElementById("female").checked;
req.open("GET", "index.jsp?female=" + encodeURI(fem) + "&age=" + encodeURI(ag) + "&height=" + encodeURI(hei));
req.setRequestHeader("User-Agent", "XMLHttpRequest");
req.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200){
var msg = document.getElementById('msg');
msg.innerHTML = this.responseText;
}
}
req.send();
}
我認為文件在請求中交互時出現錯誤。
當我擦除<%@ include file = "index.jsp" %>
時,它運行良好(但不在 NewFile.jsp 中使用 index.jsp 變量)。
但我不知道如何修改它。
如何在 NewFile.jsp 中使用 index.jsp 中定義的變量?
從描述中不清楚您要做什么。 似乎您在第 13 行獲得了 null 參考。如果您將年齡作為請求參數傳遞,那么“共享變量”根本沒有意義。
這是一個演示如何共享變量的工作示例:
<!-- hello.jsp -->
<%
String hi = "Hello from hello.jsp";
request.setAttribute("hi", hi);
%>
<!-- index.jsp -->
<jsp:include page="hello.jsp"/>
<%=request.getAttribute("hi") %>
請記住,非常不建議使用 JSP 腳本。 檢查這篇文章以獲得更深入的細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.