使用Javascript可以从HTML页面中servlet设置的会话属性中获取值吗 [英] Using Javascript can you get the value from a session attribute set by servlet in the HTML page

查看:20
本文介绍了使用Javascript可以从HTML页面中servlet设置的会话属性中获取值吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用重定向转发到 HTML 页面的 servlet.因为我在html页面上使用ajax和php做其他功能.可以转成jsp.有什么方法可以让我在会话属性中的 servlet 中获得名称 -poNumber".我要得到它并展示它的价值.

I have a servlet that forwards to a HTML page, using redirect. Because I am using ajax and php on the html page to do other functions. Can turn into a jsp. Is there a way I can get the name -"poNumber" I get in servlet in the session attributes. I was to get it and display it's value.

我是编程新手.

可以在jsp中运行.

但是需要在 html 页面中使用它.我可以用 javascript 做吗?

However need to get this working in a html page. Can I do it with javascript?

我试过了:

      <script type="text/javascript">
      var purchaseOrderNo = session.getAttribrute("pONumb");
      document.write("pONumb");
      </script> [

这不会在 HTML 页面上输出任何值.

This does not output any values on the HTML page.

尝试过:

       <script type="text/javascript">
       var purchaseOrderNo = (String) session.getAttribrute("pONumb");
           document.write("pONumb");
           </script> 

再次在页面上没有输出.

Again get no output on page.

尝试过:

            <script type="text/javascript">
            String purchaseOrderNo = (String) session.getAttribrute("pONumb");
            document.write("pONumb");
            </script> 

页面上又没有输出?

想不出还有什么可以尝试的.重定向到此 HTML 页面的 servlet 创建并设置会话属性 pONumb.

Can not think of any thing else to try. The servlet that redirects to this HTML page creates and set session attribute pONumb.

推荐答案

不,你不能.JavaScript 在客户端(浏览器)执行,而会话数据存储在服务器上.

No, you can't. JavaScript is executed on the client side (browser), while the session data is stored on the server.

但是,您可以通过多种方式为 JavaScript 公开会话变量:

However, you can expose session variables for JavaScript in several ways:

  • 一个隐藏的输入字段,将变量存储为其值并通过 DOM API 读取它
  • 可以通过 DOM 读取的 HTML5 数据属性
  • 将其存储为 cookie 并通过 JavaScript 访问它
  • 直接在 JS 代码中注入它,如果你有内联

在 JSP 中,你会得到类似的东西:

In JSP you'd have something like:

<input type="hidden" name="pONumb" value="${sessionScope.pONumb} />

或:

<div id="product" data-prodnumber="${sessionScope.pONumb}" />

然后在 JS 中:

// you can find a more efficient way to select the input you want
var inputs = document.getElementsByTagName("input"), len = inputs.length, i, pONumb;
for (i = 0; i < len; i++) {
    if (inputs[i].name == "pONumb") {
        pONumb = inputs[i].value;
        break;
    }
}

或:

var product = document.getElementById("product"), pONumb;
pONumb = product.getAttribute("data-prodnumber");

内联示例是最直接的,但如果您想将 JavaScript 代码存储为外部资源 (推荐方式) 不可行.

The inline example is the most straightforward, but if you then want to store your JavaScript code as an external resource (the recommended way) it won't be feasible.

<script>
    var pONumb = ${sessionScope.pONumb};
    [...]
</script>

这篇关于使用Javascript可以从HTML页面中servlet设置的会话属性中获取值吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆