英文:
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中定义的变量?
英文:
I have two jsp file, one is NewFile.jsp and another is index.jsp
I want to use variables(defined in index.jsp) in NewFile.jsp.
So I put this line in "NewFile.jsp":
<%@ include file = "index.jsp" %>
But page loaded HTTP Status 500 Internal Server Error and it said, :
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:
It said : row [13] is wrong.(It is wrote in Korean but I think you can understand.)
and I have these lines in 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();
}
I think it has an error while files interact in request.
And when I erase <%@ include file = "index.jsp" %>
, it worked well (but not use index.jsp variables in NewFile.jsp).
But I don't know how to modify it.
How can I do to use variables defined in index.jsp in NewFile.jsp?
答案1
得分: 1
从描述中不清楚你尝试做什么。似乎在第13行出现了空引用。如果你将年龄作为请求参数传递,那么"共享变量"根本没有意义。
以下是演示如何共享变量的示例:
<!-- hello.jsp -->
<%
String hi = "Hello from hello.jsp";
request.setAttribute("hi", hi);
%>
<br>
<!-- index.jsp -->
<jsp:include page="hello.jsp"/>
<%=request.getAttribute("hi") %>
请记住,强烈不建议使用JSP脚本。查看此链接以获取更详细的信息。
英文:
It's not clear from the description what you are trying to do. It seems that you got a null reference at line 13. If you pass the age as a request parameter than it does not make sense at all "sharing the variables".<br>Here's a working example which demonstrates how to share variables:
<!-- hello.jsp -->
<%
String hi = "Hello from hello.jsp";
request.setAttribute("hi", hi);
%>
<br>
<!-- index.jsp -->
<jsp:include page="hello.jsp"/>
<%=request.getAttribute("hi") %>
Keep in mind that the usage of JSP scriptlets is highly discoureged. Check this post for more in-depth details.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论