【jsp】局部变量和全局变量

1.

jsp三种脚本元素

<%! 声明 这里定义的变量是全局变量%>


<% 脚本 这里定义的是局部变量%>


<%=对象% 

全局变量到处可以用,例如<%!%>声明的,局部变量就是只有函数内部可以用<%%>


2.

记录一下我用使用jsp页面中<%! %>全局变量声明遇到的问题,我们用全局变量一般保存的是在这个页面中要多次用到的变量,如用户ID等等,但是在<%! %>给变量定义赋值后,关闭页面再此打开后值是不变的,这使得想用全局变量接收提交的参数的时候出现一些问题:比如用户ID依然是上次的ID等等。
   原因我查了一下,大致是因为下列原因:
   1.使用<%! %>定义的变量编译成servlet后成为servlet的私有成员变量,在servlet环境中,对于一个JSP页只允许有一个结果servlet的实例在运行和响应所有的页面请求。因此,在结果servlet中所有的成员变量可以被所有的请求所共享,所以只要成员变量没有重新赋值,下次访问该页面时仍然不变。
   2.而在<% %>中定义的变量编译成servlet后成为service()方法的一个本地变量,而service()方法中的本地变量只能每响应一次请求就重建一次。
   所以解决办法是变量定义在<%! %>之中,但是在<%%>里面用接收到的参数进行赋值,这样该全局变量就可以在每次请求的时候重新赋值。比如:
   <%!int i=0;%>
   <%i++;%>
另外引用<%!int i=0;%>定义的变量用<%=this.i;>;引用<%int i=0;%>用<%=i;%>

发布了27 篇原创文章 · 获赞 5 · 访问量 3万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

js里只有一类关于name的全局变量取不到值,其他的都可以

06-13

这是js代码部分,功能是表单验证 只有变量name、inputName取不到值,其他都可以,查了一天了都没答案,求助 ``` var name = document.getElementById("name"); var psw = document.getElementById("psw"); var confirmation = document.getElementById("confirmation"); var email = document.getElementById("email"); var phone = document.getElementById("phone"); var submit = document.getElementById("submit"); var nameHint = document.getElementById("name-hint"); var pswHint = document.getElementById("psw-hint"); var conHint = document.getElementById("con-hint"); var emailHint = document.getElementById("email-hint"); var phoneHint = document.getElementById("phone-hint"); var inputName,inputPsw,inputConfirmation,inputEmail,inputPhone; function checkNull (text) { var name = document.getElementById("name"); if (text == "") { nameHint.innerHTML = "名称不能为空"; nameHint.style.color = "#de000f"; name.style.border = "solid 1px #de000f"; } else { checkLen(text); } } function checkLen (text) { //\x00-\xff以内的是英文(单字节),以外的是双字节 //匹配不在\x00-\xff范围内的字符 var nchar = /[^\x00-\xff]{1,}/g; var char = /[\w+\S]{1,}/g; //转换成数组 var str = char.exec(text); var nstr = nchar.exec(text); if(str==null){ str=""; } if(nstr==null){ nstr=""; } //转换成字符串 var len = nstr.toString().length*2+str.toString().length; if (3<len && len<17) { nameHint.innerHTML = "名称格式正确"; nameHint.style.color = "#56b73c"; name.style.border = "solid 1px #56b73c"; inputName = true; } else { nameHint.innerHTML = "长度应为4~16个字符"; nameHint.style.color = "#de000f"; name.style.border = "solid 1px #de000f"; } } function checkPsw (text) { if (text == "") { pswHint.innerHTML = "密码不能为空"; pswHint.style.color = "#de000f"; psw.style.border = "solid 1px #de000f"; } else { checkPswLen(psw.value); } } function checkPswLen (text) { //\x00-\xff以内的是英文(单字节),以外的是双字节 //匹配不在\x00-\xff范围内的字符 var nchar = /[^\x00-\xff]{1,}/g; var char = /[\w+\S]{1,}/g; //转换成数组 var str = char.exec(text); var nstr = nchar.exec(text); if(str==null){ str=""; } if(nstr==null){ nstr=""; } //转换成字符串 var len = nstr.toString().length*2+str.toString().length; if (7<len && len<17) { pswHint.innerHTML = "密码可用"; pswHint.style.color = "#56b73c"; psw.style.border = "solid 1px #56b73c"; inputPsw = true; } else { pswHint.innerHTML = "长度应为8~16个字符"; pswHint.style.color = "#de000f"; psw.style.border = "solid 1px #de000f"; } } function checkConfirmation (text) { if (text == "") { conHint.innerHTML = "密码不能为空"; conHint.style.color = "#de000f"; confirmation.style.border = "solid 1px #de000f"; } else if (text == psw.value) { conHint.innerHTML = "密码输入一致"; conHint.style.color = "#56b73c"; confirmation.style.border = "solid 1px #56b73c"; inputConfirmation = true; } else { conHint.innerHTML = "两次密码不一致"; conHint.style.color = "#de000f"; confirmation.style.border = "solid 1px #de000f"; } } function checkEmail (text) { var at = text.indexOf("@"); var dot = text.indexOf("."); if (at < 1 || dot - at < 2) { emailHint.innerHTML = "邮箱格式错误"; emailHint.style.color = "#de000f"; email.style.border = "solid 1px #de000f"; } else { emailHint.innerHTML = "邮箱格式正确"; emailHint.style.color = "#56b73c"; email.style.border = "solid 1px #56b73c"; inputEmail = true; } } function checkPhone (text) { if (!/1[34578]\d{9}/.test(text)) { phoneHint.innerHTML = "手机格式错误"; phoneHint.style.color = "#de000f"; phone.style.border = "solid 1px #de000f"; } else { phoneHint.innerHTML = "手机格式正确"; phoneHint.style.color = "#56b73c"; phone.style.border = "solid 1px #56b73c"; inputPhone = true; } } window.onload = function init () { var name = document.getElementById("name"); name.onfocus = function () { nameHint.innerHTML = "必填,长度为4~16个字符"; nameHint.style.color = "#a9a9a9"; name.style.border = "solid 1px #a9a9a9"; }; name.onblur = function () { checkNull(name.value); }; psw.onfocus = function () { pswHint.innerHTML = "必填,长度为8~16个字符"; pswHint.style.color = "#a9a9a9"; psw.style.border = "solid 1px #a9a9a9"; }; psw.onblur = function () { checkPsw(psw.value); }; confirmation.onfocus = function () { conHint.innerHTML = "再次输入相同密码"; conHint.style.color = "#a9a9a9"; confirmation.style.border = "solid 1px #a9a9a9"; }; confirmation.onblur = function () { checkConfirmation(confirmation.value); }; email.onfocus = function () { emailHint.innerHTML = "输入邮箱"; emailHint.style.color = "#a9a9a9"; email.style.border = "solid 1px #a9a9a9"; }; email.onblur = function () { checkEmail(email.value); }; phone.onfocus = function () { phoneHint.innerHTML = "输入手机"; phoneHint.style.color = "#a9a9a9"; phone.style.border = "solid 1px #a9a9a9"; }; phone.onblur = function () { checkPhone(phone.value); }; submit.onclick = function () { if (inputName&&inputPsw&&inputConfirmation&&inputEmail&&inputEmail) { alert("提交成功!"); } else { if (!inputName) { alert("名称格式错误"); } else if (!inputPsw) { alert("密码格式错误") } else if (!inputConfirmation) { alert("密码输入不一致") } else if (!inputEmail) { alert("email格式错误") } else if (!inputPhone) { alert("手机格式错误") } alert("提交失败!"); } }; }; ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览