简体   繁体   English

如何正确执行JSP代码以将HttpSession中的值检索到JavaScript函数中?

[英]How can I correctly execute JSP code to retrieve a value from the HttpSession into a JavaScript function?

I am working on a very old legacy Java web application that is based on HttpServlet and I have the following problem trying to running JSP code into a JavaScript function. 我正在使用基于HttpServlet的非常旧的旧版Java Web应用程序,尝试将JSP代码运行到JavaScript函数中时遇到以下问题。

So, into a JSP page I found this JavaScript function that give me some problem: 因此,在JSP页面中,我发现了这个JavaScript函数,该函数给我带来了一些问题:

function rifiuta() {

    document.getElementById('notaRifiuto').value = document.getElementById('myRejectNote').value;

    my_pkcoda = '<%=((request.getSession().getAttribute("doc_num")!=null?((String)request.getSession().getAttribute("doc_num")).trim():""))%>';

    document.getElementById(my_pkcoda).checked = true;
    checkRifiuta();
}

This is very simple: 这很简单:

It is declared a ** my_pkcoda** that try to be initialized with a value obtained from the HttpSession that is neamed doc_num , this is done by: 它被宣布为** ** my_pkcoda那些试图从是neamed doc_num HttpSession中获得的值进行初始化,这是通过:

request.getSession().getAttribute("doc_num")

I am pretty sure that this value is into the HttpSession because I put it into the session by (into the class that implement my HttpServlet : 我非常确定该值已放入HttpSession中,因为我将其放入会话中(是通过(放入实现HttpServlet的类中:

req.getSession().setAttribute("doc_num", req.getParameter("pk"));

and after do this operation I check if it is correctly putted into the session using the debugger and it is ok, it is into the session. 然后执行此操作后,我将使用调试器检查它是否正确放入了会话中,并且可以,它已经进入了会话中。

The problem is that when into my JSP page the rifiuta() JavaScript function is performed, when it try to execute this statment: 问题在于,当进入我的JSP页面时,当尝试执行此语句时,将执行rifiuta() JavaScript函数:

my_pkcoda = '<%=((request.getSession().getAttribute("doc_num")!=null?((String)request.getSession().getAttribute("doc_num")).trim():""))%>';

I obtain that the my_pkcoda value is null. 我获得my_pkcoda值为null。

What could be the problem? 可能是什么问题呢? What am I missing? 我想念什么?

Tnx TNX

JSP scriptlet tags get evaluated only once when the web page is loaded from the server. 从服务器加载网页后,仅对JSP scriptlet标记进行一次评估。 The value of that scriptlet expression gets pasted in your JS function as a text the first time when the page is loaded from server. 第一次从服务器加载页面时,该scriptlet表达式的值将作为文本粘贴到JS函数中。

On each call of the JS function rifiuta that value will not get loaded from the session. 在JS函数rifiuta每次调用中,该值都不会从会话中加载。

So, in case you are calling rifiuta on a button click, you will not get the latest value of doc_num attribute from session. 因此,如果您在单击按钮时调用rifiuta,则不会从会话中获取doc_num属性的最新值。

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

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