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.

So, into a JSP page I found this JavaScript function that give me some problem:

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:

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:

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:

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

I obtain that the my_pkcoda value is null.

What could be the problem? What am I missing?

Tnx


Source: jsp

Leave a Reply