登陆页面当中调用session取值: <%
String ccode=(String)request.getSession().getAttribute("ccode");
System.out.println("Jsp-->"+ccode);
%>form表单:
<html:form action="jsp/user.do" method="post">
用户ID:<html:text property="userid" onblur="checkUserName();"></html:text>
<span id="userIdMessage" style="width:200px ; height:10px;"></span>
<br>
用户密码:<html:password property="userpwd" onblur="checkPassWord();"></html:password>
<span id="pwdMessage" style="width:200px ; height:10px;"></span>
<br>
确认密码:<html:password property="confirmpwd" onblur="checkConfpwd();"></html:password>
<span id="confpwdMessage" style="width:200px ; height:10px;"></span>
<br>
丢失密码提示问题:<html:text property="userques"></html:text>
<br>
丢失密码问题答案:<html:text property="userans"></html:text>
<br>
验证码:<html:text property="checkcode" onblur="checkCode();"></html:text>
<img src="image.jsp">
<input type="hidden" name="ccode" value="<%=ccode%>">
<span id="checkcodeMessage" style="width:200px ; height:10px;"></span>
<br>
<input type="hidden" name="status" value="register">
<input type="hidden" name="type" value="1">
<html:submit value="注册"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
验证码生成代码主要就是:// 将认证码存入SESSION
session.setAttribute("ccode",sRand);问题:第一次到达页面,从session里面没有取到验证码值,以后刷新都是取到上次显示的值。
(注:我这个是struts+dwr做的,望高人指点迷津)
String ccode=(String)request.getSession().getAttribute("ccode");
System.out.println("Jsp-->"+ccode);
%>form表单:
<html:form action="jsp/user.do" method="post">
用户ID:<html:text property="userid" onblur="checkUserName();"></html:text>
<span id="userIdMessage" style="width:200px ; height:10px;"></span>
<br>
用户密码:<html:password property="userpwd" onblur="checkPassWord();"></html:password>
<span id="pwdMessage" style="width:200px ; height:10px;"></span>
<br>
确认密码:<html:password property="confirmpwd" onblur="checkConfpwd();"></html:password>
<span id="confpwdMessage" style="width:200px ; height:10px;"></span>
<br>
丢失密码提示问题:<html:text property="userques"></html:text>
<br>
丢失密码问题答案:<html:text property="userans"></html:text>
<br>
验证码:<html:text property="checkcode" onblur="checkCode();"></html:text>
<img src="image.jsp">
<input type="hidden" name="ccode" value="<%=ccode%>">
<span id="checkcodeMessage" style="width:200px ; height:10px;"></span>
<br>
<input type="hidden" name="status" value="register">
<input type="hidden" name="type" value="1">
<html:submit value="注册"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
验证码生成代码主要就是:// 将认证码存入SESSION
session.setAttribute("ccode",sRand);问题:第一次到达页面,从session里面没有取到验证码值,以后刷新都是取到上次显示的值。
(注:我这个是struts+dwr做的,望高人指点迷津)
解决方案 »
- java GUI 开发 标题栏图片
- 如何提取主板的时间呢???
- 关于取${ XXXXXX } 取得${ }中间的值,使用java的正则表达式regx
- 快来看一下,帮助一下新手吧谢谢
- 调查:请问各位Java Developer中有多少人在经常使用Java泛型
- 各位我编的一个JAVA文件在运行的时候出现"javaapplication2.NewClass@35ce36"请问如何解释?????
- 接口和抽象类,我晕,该如何应用,高手帮忙。
- 所有的JFC对象都是线程不安全的,大家遇到过这方面的问题吗?
- 用户权限的具体实现不可不看!!高手请进!!!
- 高手请进,求解代码片段.
- 请解释一下正则的相关内容
- 被调用对象的方法内部,如何"动态"获得调用者(调用对象)的引用。
楼主【yudylaw】截止到2008-06-30 10:33:30的历史汇总数据(不包括此帖):
发帖数:3 发帖分:200
结贴数:2 结贴分:160
未结数:1 未结分:40
结贴率:66.67 % 结分率:80.00 %
楼主加油
你把两个页面分开用就可以了,就是把提交页和接收页面分开,
如果用一个页面的话,就在页面里判断一下是否是提交状态!
if( request.getMethod().equalsIgnoreCase("POST") == true )
<img src="image.jsp?<%=Long.toString(System.currentTimeMillis(), 36)%>">
另外有了这句
<input type="hidden" name="ccode" value="<%=ccode%>">
你的验证码就跟没有一样,起不到作用。
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/util.js'> </script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/ajaxValidateDAO.js'> </script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/user.js'> </script>
<%
String ccode=(String)request.getSession().getAttribute("ccode");
System.out.println("Jsp-->"+ccode);
%>
<script type="text/javascript">
function checkUserName()
{
var userName=dwr.util.getValue("userid");
//var passWord = dwr.util.getValue("userpwd");
//User ={userid:userid,userpwd:userpwd};
ajaxValidateDAO.checkUserName(userName,
function(data)
{
if(data!="用户名可用")
{
document.userForm.userid.value="";
document.userForm.userid.focus();
}
dwr.util.setValue("userIdMessage",data);
}
);
}
function checkPassWord()
{
var passWord = dwr.util.getValue("userpwd");
//var userName=dwr.util.getValue("userid");
ajaxValidateDAO.checkPassword(passWord,
function(data)
{
if(data!="密码输入正确")
{
document.userForm.userpwd.value="";
document.userForm.userpwd.focus();
}
dwr.util.setValue("pwdMessage",data);
}
);
}
function checkConfpwd()
{
var confpwd = dwr.util.getValue("confirmpwd");
var passWord = dwr.util.getValue("userpwd");
ajaxValidateDAO.confPassword(passWord,confpwd,
function(data)
{
if(data!="密码输入正确")
{
document.userForm.userpwd.value="";
document.userForm.confirmpwd.value="";
document.userForm.userpwd.focus();
}
dwr.util.setValue("confpwdMessage",data);
}
);
}
function checkCode()
{
var ccode =dwr.util.getValue("ccode");
// alert(ccode);
var checkcode=dwr.util.getValue("checkcode");
ajaxValidateDAO.checkCode(checkcode,ccode,
function(data)
{
if(data=!"验证码输入正确")
{
document.userForm.checkcode.value="";
document.userFrom.checkcode.focus();
}
}
dwr.util.setValue("checkcodeMessage",data);
);
}
</script>
</head>
<body>
<center>
<jsp:include flush="true" page="../inc/template.jsp">
<jsp:param name="url" value="../"/>
</jsp:include>
<h1>用户注册</h1>
<html:form action="jsp/user.do" method="post">
用户ID:<html:text property="userid" onblur="checkUserName();"></html:text>
<span id="userIdMessage" style="width:200px ; height:10px;"></span>
<br>
用户密码:<html:password property="userpwd" onblur="checkPassWord();"></html:password>
<span id="pwdMessage" style="width:200px ; height:10px;"></span>
<br>
确认密码:<html:password property="confirmpwd" onblur="checkConfpwd();"></html:password>
<span id="confpwdMessage" style="width:200px ; height:10px;"></span>
<br>
丢失密码提示问题:<html:text property="userques"></html:text>
<br>
丢失密码问题答案:<html:text property="userans"></html:text>
<br>
验证码:<html:text property="checkcode" onblur="checkCode();"></html:text>
<img src="image.jsp?<%=Long.toString(System.currentTimeMillis(), 36)%>">
<input type="hidden" name="ccode" value="<%=ccode%>">
<span id="checkcodeMessage" style="width:200px ; height:10px;"></span>
<br>
<input type="hidden" name="status" value="register">
<input type="hidden" name="type" value="1">
<html:submit value="注册"></html:submit>
<html:reset value="重置"></html:reset>
</html:form>
你可另建一个servlet去session当中拿值试试。
首先,页面上的session会先于图片加载过来。所以在随机数还没生成的时候,session就被读到jsp上了.这就解释了为什么第一次session里null,而后面每次都是上一次的值。上面的其实都是屁话;真正解决问题的方法是:
比如:你的验证码是在A.jsp上的文本框里输入的,触发一个onblur事件。这个事件对应一个 ajax 动态的在servlet里验证,session里的值和你输入值是否向相等。在用PrintWriter写会页面。会ajax的这点都能做到。因为,其实当图片加载过来时,session里的值已经被更新,只是页面上无法及时的更新罢了。这时ajax的异步刷新就发挥作用了。下面贴源代码,希望对楼主有所帮助:
页面代码jsp:
<font size="2">验证码 :</font>
<img id="checkCodeImage" src="servlet/ValidateCodeServllet" style="cursor:pointer" onclick="javascript:ajaxMethod('checkCodeImage');"/>
<a style="cursor:pointer" onclick="javascript:ajaxMethod('checkCodeImage');">
<font id="fontId" size="2">
<u onmouseover="colorblue('fontId');" onmouseout="colorblack('fontId');">看不清换一个</u></font>
</a>
<BR>
<input type="text" size=30 onchange="validate(this.value)"> <span id="va"></span>脚本代码:
<script type="text/javascript">
function ajaxMethod(cid){
document.getElementById(cid).setAttribute("src",null);
//加上随机数的目的是为了每次都改变url 让浏览器不要加载缓存
var svalue = "servlet/ValidateCodeServllet?id=" + Math.random();
document.getElementById(cid).setAttribute("src",svalue);
}
function colorblue(id){
document.getElementById(id).color="blue";
}
function colorblack(id){
document.getElementById(id).color="black";
}
function validate(e){
if(window.ActiveXObject){
xmlhttp = new ActiveXObject("Microsoft.XMLHttp")
}else{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("get","servlet/CheckVa?str="+e);
xmlhttp.onreadystatechange = handlerequest;
xmlhttp.send(null);
}
function handlerequest(){
if(xmlhttp.readyState==4){
var sp = document.getElementById("va");
var rr = xmlhttp.responseText;
sp.innerHTML =rr;
}
}
</script> servlet代码:
public class CheckVa extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String string = request.getParameter("str");
System.out.println(string+"!@#$%^&*");
HttpSession session = request.getSession();
System.out.println(session.getAttribute("check_code")+"这是验证框架里的session");
PrintWriter pw = response.getWriter();
if(string .equals(session.getAttribute("check_code"))){
pw.print("对@@@@@@@@@@@@@@");
}else{
pw.print("错@@@@@@@@@@@@@@");
}
pw.flush();
pw.close();
}}