简体   繁体   English

如何将数据从 Java 类传递到 JSP 页面

[英]How to pass data from Java class to a JSP page

I am a newbie in JSP and Dynamic web projects.我是 JSP 和动态 Web 项目的新手。 My project structure is shown as below.我的项目结构如下所示。 Basically, I am having a variable temperature in my SparkStreaming class which is generated in every 2 seconds and I wish to pass this value to the A.JSP page .基本上,我的 SparkStreaming 类中有一个可变温度,它每 2 秒生成一次,我希望将此值传递给 A.JSP 页面。 I shall be generating few charts based on the JSP page.我将根据 JSP 页面生成一些图表。

How can I achieve this in Java?我如何在 Java 中实现这一点? 在此处输入图片说明

Simply you can use sessions.只需使用会话即可。

think in java class you have string variable call temperature .想想在java类中你有字符串变量调用temperature

string temperature = "11.11";
request.removeAttribute("temp");
request.setAttribute("temp", temperature);

in removeAttribute you clear the session stored value.then you set value to session.removeAttribute您清除会话存储的值。然后将值设置为会话。

In JSP u can access session value like this.在 JSP 中,您可以像这样访问会话值。

request.getAttribute("temp");
  • If you want to pass two dimensional array you can convert the session and pass the value to session如果你想传递二维数组,你可以转换会话并将值传递给会话

您应该有一个 servlet,然后在 jsp 中调用该 servlet 以获取最新的温度值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM