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

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

问题描述

我有一个使用重定向转发到HTML页面的servlet。因为我在html页面上使用ajax和php来执行其他功能。可以变成一个jsp。有没有一种方法可以获得名称 - 我在servlet中使用会话属性获取的poNumber。我是得到它并显示它的价值。

我是编程新手。



可以得到这个工作在jsp中。



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



我曾尝试过:

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

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



试过:

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

再次在页面上输出。

试过:

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

再一次在网页上没有输出?



想不到其他人尝试。重定向到此HTML页面的servlet会创建并设置会话属性pONumb。

解决方案

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



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




  • 一个隐藏的输入字段,将变量存储为其值并通过DOM API读取它

  • 您可以通过DOM读取的HTML5数据属性

  • 将其存储为cookie并通过JavaScript访问它
  • 直接在JS代码中注入它,如果你有它的内联



在JSP中,您可能会看到如下内容:

 < input type =hiddenname =pONumbvalue =$ {sessionScope.pONumb} /> 



 < div id =productdata -prodnumber =$ {sessionScope.pONumb}/> 

然后在JS中:

  //你可以找到更有效的方法来选择你想要的输入
var inputs = document.ge tElementsByTagName(input),len = inputs.length,i,pONumb;
for(i = 0; i< len; i ++){
if(inputs [i] .name ==pONumb){
pONumb = inputs [i] .value;
休息;


$ / code $ / pre
$ b $ p


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

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

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


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.

I am new to programming.

Can get this working in jsp.

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

I have tried:

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

This does not output any values on the HTML page.

Tried:

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

Again get no output on page.

Tried:

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

Again get no output on page?

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

解决方案

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

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

  • a hidden input field storing the variable as its value and reading it through the DOM API
  • an HTML5 data attribute which you can read through the DOM
  • storing it as a cookie and accessing it through JavaScript
  • injecting it directly in the JS code, if you have it inline

In JSP you'd have something like:

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

or:

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

Then in 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;
    }
}

or:

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

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天全站免登陆