如何在jsp中访问javascript变量

问题描述:

我想在jsp页面scriptlet中获取JavaScript变量我正在尝试获取空值,请帮助我。

I want to get JavaScript variable in jsp page scriptlet i am trying alot its getting null value please help me out.

mycode:

<script>
    function setacademicyear(ele){
        alert("ele"+ele);  
        request.setAttribute("ayear", ele);
    }

    <%
        if (request.getAttribute("ayear") != null) {
            String ayear1 = (String) request.getAttribute("ayear");
            System.out.println("ayear1ayear1ayear1" + ayear1);
            Academicyearmaster acy = (Academicyearmaster) ses.get(Academicyearmaster.class, Long.valueOf(ayear1));
            request.getSession().setAttribute("academicyear", acy);
        }%>  
</script>


你不能。 JSP正在服务器上进行处理;然后客户端接管并且JavaScript可以处理。除非在新请求中,否则您无法将值从JavaScript传递到JSP。

You cannot. JSP is processing on the server; then the client takes over and JavaScript can process. You cannot pass values from JavaScript to JSP except in a new request.

想象一下您前往莫斯科,当您到达那里时,您会要求您的母亲提醒您一种与她取得联系的方式。已经太晚了,她无法联系,你必须自己记住它。

Imagine you were traveling to Moscow, and you ask your mother to remind you when you get there to find a way to get in touch with her. It's too late, she can't make contact, you have to remember it by yourself.