如何在另一个 JSP 文件中使用 JSP 变量?

huangapple go评论69阅读模式
英文:

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) + "&amp;age=" + encodeURI(ag) + "&amp;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":

&lt;%@ include file = &quot;index.jsp&quot; %&gt;

But page loaded HTTP Status 500 Internal Server Error and it said, :

org.apache.jasper.JasperException: 행 [13]에서 [/index.jsp]을(를) 처리하는 중 예외 발생

10:  		out.println(&quot;can&#39;t access&quot;);
11:  	}
12:  	else {
13:  		int age = Integer.parseInt(request.getParameter(&quot;age&quot;));
14:  		double height = Double.parseDouble(request.getParameter(&quot;height&quot;));
15:  		boolean sex = Boolean.parseBoolean(request.getParameter(&quot;female&quot;));
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(&quot;실행이 되지 않는다!&quot;);
                  		return ;
                  	}
                  	var hei = document.getElementById(&quot;height&quot;).value;
                  	var ag = document.getElementById(&quot;age&quot;).value;
                  	var fem = document.getElementById(&quot;female&quot;).checked;
                  	req.open(&quot;GET&quot;, &quot;index.jsp?female=&quot; + encodeURI(fem) + &quot;&amp;age=&quot; + encodeURI(ag) + &quot;&amp;height=&quot; + encodeURI(hei));
                  	req.setRequestHeader(&quot;User-Agent&quot;, &quot;XMLHttpRequest&quot;);
                  	req.onreadystatechange = function() {
                  		if (this.readyState == 4 &amp;&amp; this.status == 200){
                  			var msg = document.getElementById(&#39;msg&#39;);
                  			msg.innerHTML = this.responseText;
                  		}
                  	}
                  	req.send();
                  }

I think it has an error while files interact in request.

And when I erase &lt;%@ include file = &quot;index.jsp&quot; %&gt;, 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:

&lt;!-- hello.jsp --&gt;
&lt;%
	String hi = &quot;Hello from hello.jsp&quot;;
	request.setAttribute(&quot;hi&quot;, hi);
%&gt;

<br>

&lt;!-- index.jsp --&gt;
&lt;jsp:include page=&quot;hello.jsp&quot;/&gt;
&lt;%=request.getAttribute(&quot;hi&quot;) %&gt;

Keep in mind that the usage of JSP scriptlets is highly discoureged. Check this post for more in-depth details.

huangapple
  • 本文由 发表于 2020年8月8日 18:26:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63314339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定