ajax实现异步注册验证(stru…

//action类

import java.io.PrintWriter;   
import java.text.SimpleDateFormat;   
import java.util.Date;   
import javax.servlet.http.HttpServletRequest;   
import javax.servlet.http.HttpServletResponse;   
import org.apache.struts2.ServletActionContext;   
import com.opensymphony.xwork2.ActionSupport;   
  
public class AjaxAction extends ActionSupport {   
     
   public String getServerTime(){   
       try {   
           SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");   
           //获取原始的PrintWriter对象,以便输出响应结果,而不用跳转到某个试图   
           HttpServletResponse response = ServletActionContext.getResponse();   
           //设置字符集   
           response.setCharacterEncoding("UTF-8");   
           PrintWriter out = response.getWriter();   
           //直接输入响应的内容   
           out.println(sd.format(new Date()));   
             
           out.flush();   
           out.close();   
       } catch (Exception e) {   
           // TODO: handle exception   
           e.printStackTrace();   
        
       return null;//不需要跳转某个视图 因为上面已经有了直接输出的响应结果   
    
     
   public String ajaxLogin(){   
       try {   
           String responseText="";   
           //读取请求的参数   
           HttpServletRequest request = ServletActionContext.getRequest();   
           String loginname=   request.getParameter("loginname");   
           String loginpwd= request.getParameter("loginpwd");   
           //进行登录验证
            
               if(loginname.equalsIgnoreCase("1")){   
                   responseText="该用户名已经被注册!!";   
               }else{   
                   responseText="该用户名字还可以注册!!";   
                
              
           //获取原始的PrintWriter对象,以便输出响应结果,而不用跳转到某个试图   
           HttpServletResponse response = ServletActionContext.getResponse();   
           //设置字符集   
           response.setContentType("text/plain");//设置输出为文字流  
           response.setCharacterEncoding("UTF-8");   
           PrintWriter out = response.getWriter();   
           //直接输出响应的内容   
           out.println(responseText);   
           out.flush();   
           out.close();   
       } catch (Exception e) {   
           // TODO: handle exception   
           e.printStackTrace();   
        
       return null;//不需要跳转某个视图 因为上面已经有了直接输出的响应结果   
       //http://www. dukai168.cn/blog/157.html
    
 

 

//ajax 请求服务器返回结果

<script language="javascript">  
   //定义一个变量用于存放XMLHttpRequest对象   
   var xmlHttp;   
   //改函数用于创建一个XMLHttpRequest对象   
   function createXMLHttpRequest(){   
       if(window.ActiveXObject){   
           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
       }else if(window.XMLHttpRequest){   
           xmlHttp = new XMLHttpRequest();   
        
    
   //这是一个启动AJAX异步通信的方法   
   function getServerTime(){   
       alert("我走过了时间方法");   
       var now = new Date();//获取系统当前的时间   
       //创建一个XMLHttpRequest对象   
       createXMLHttpRequest();   
       //将状态触发器绑定到一个函数   
       xmlHttp.onreadystatechange= processServerTime;   
       //通过GET方法向指定的URL建立服务器的调用,加个临时的参数,以便表示一个全新的请求   
       var url="ajax_getServerTime.action?tmp="+now.getTime();//等写完回来看这一句话   
       xmlHttp.open("GET",url,true);   
       //发送请求   
       xmlHttp.send(null);   
    
   //这是一个用来处理状态改变的函数   
   function processServerTime(){   
       //定义一个变量用于存放从服务器返回的响应结果   
       var responseContext;   
       if(xmlHttp.readyState==4){   
           //如果响应成功   
           if(xmlHttp.status==200){   
               //取出服务器的响应内容   
               responseContext=xmlHttp.responseText;   
               document.getElementByIdx_x("servertime").innerHTML=responseContext;   
            
        
    
      
   //这是一个启动AJAX异步通信的方法   
   function ajaxLogin(){   
       var ln = document.getElementByIdx_x("loginname").value;   
       var lp = document.getElementByIdx_x("loginpwd").value;   
       //创建一个XMLHttpRequest对象   
       createXMLHttpRequest();   
       //将状态绑定到一个函数   
       xmlHttp.onreadystatechange=processAjaxLogin;   
       //通过GET方法向指定的URL建立服务器的调用   
       var url="ajax_ajaxLogin.action?loginname="+ln;   
       xmlHttp.open("GET",url,true);   
       //发送请求   
       xmlHttp.send(null);   
    
   //这是一个用来处理状态改变的函数   
   function processAjaxLogin(){   
       //定义一个变量用于存放 从服务器返回的响应结果   
       var responseContext="";   
       if(xmlHttp.readyState==4){   
           if(xmlHttp.status==200){   
               responseContext = xmlHttp.responseText;   
               alert(responseContext);   
            
        
    
</script>   

 

//jsp页面

  <body>
    <span id="servertime"></span>  

<input type="button" value="获取服务器端时间" onclick = "getServerTime()"> <BR>
  用户名: <input type="text" id="loginname"><BR>
   <P></P>密码: &nbsp; <input type="password" id="loginpwd" οnfοcus="ajaxLogin()" >    <BR>
  </body>

 

 

//xml配置

  <package name="test1" extends="struts-default">   
       <action name="ajax_*" class="com.action.AjaxAction" method="{1}">   
           <result>/show.jsp</result>   
       </action>   
   </package>    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值