如何将JavaScript变量转换为Java变量?

时间:2011-03-28 06:54:30

标签: java javascript

我需要将JavaScript中的变量转换为Java变量。我通过测试框通过Block-UI弹出窗口向用户询问他们的电子邮件ID。如果该字段既不为空也不为空,则应将电子邮件ID发送到会话变量,以便可以在项目中的任何位置访问它。

这是我的代码:

function sbtEmail(){
    var email = document.getElementById("EmailId").value;
    if (email == null || email == ""){
        alert("Enter your contact Email address to continue...!");
        document.getElementById("EmailId").focus();
    } else {
        <%session.setAttribute("quoteEmail",email);%>
        window.location.href = "demo2.jsp";
    }
}

它不起作用。我做错了什么?

3 个答案:

答案 0 :(得分:2)

您无法直接从JavaScript在会话中设置变量(仅在服务器上驻留!)。您必须在某种请求中将它们传递给您的服务器,然后服务器可以在会话中设置它。

答案 1 :(得分:2)

解决:使用Ajax将解决问题。

  <script type="text/javascript" src="../ajax.js"></script>

     <script type="text/javascript">
    function sbtEmail(){
        var email = document.getElementById("anonymousEmailId").value;
        if (email==null||email==""){
            alert("Enter your contact Email address to continue...!");
            document.getElementById("anonymousEmailId").focus();
        }else{
            enqueue("demo1.jsp?Id="+email,ajaxReplay);
        }
    }
    function ajaxReplay(){
        window.location.href="demo.jsp";
    }
    </script>

在demo1.jsp中:设置会话变量的值

String Email = request.getParameter("Id");
session.setAttribute("sessEmail",Email);

答案 2 :(得分:1)

Use this Ajax file:----> eval(function(p, a, c, k, e, r) { e = function(c) { return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36)) }; if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function(e) { return r[e] } ]; e = function() { return '\\w+' }; c = 1 }; while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p } ('d 7;f n(a,b,c){6(!7)7=j s;6(!7.n(a,b,c))g l;6(!7.k)7.o();g h}f s(){3.5=j t();3.8=9;3.k;3.m=h;3.n=f(a,b,c){u(d i=0;i<3.5.p;i++)6(3.5[i][0]==a&&3.5[i][1]==b&&3.5[i][2]==c)g l;6(3.8==9){3.k=l;3.m=h;3.8=0}v{++3.8}3.5[3.8]=j t();3.5[3.8][0]=a;3.5[3.8][1]=b;3.5[3.8][2]=c;6(3.k&&c)w(c);g h};3.o=f(){6(3.8==9||3.5.p==0)g;3.k=h;6(3.m){d r=q();d a;6(r){u(d i=0;i<3.5.p;i++){6(3.5[i][2])w(3.5[i][2])}}3.m=l}3.x(3.5[0][0],3.5[0][1])};3.x=f(a,b){d r=q();6(r){r.E("F",a,h);r.G=f(){6(r.H==4){6(b)b(r.I);7.5.J(0,1);7.8>0?--7.8:7.8=9;6(7.8==9){7.k=l;7.m=h;g}v{7.o()}}};r.K(9)}}}f q(){d A=9;y{A=j z("L.B")}C(e){y{A=j z("M.B")}C(N){A=9}}6(!A&&O D!="P"){A=j D()}g A}', 52, 52, '|||this||queue|if|g_q|position|null||||var||function|return|true||new|isProcessing|false|callPreFunctions|enqueue|process|length|getXMLHTTP||ajaxQueue|Array|for|else|eval|getAjaxQueueResult|try|ActiveXObject||XMLHTTP|catch|XMLHttpRequest|open|GET|onreadystatechange|readyState|responseText|splice|send|Msxml2|Microsoft|oc|typeof|undefined'.split('|'), 0, {}))