码迷,mamicode.com
首页 > Web开发 > 详细

SSH项目下,AJAX的实现

时间:2015-06-13 21:38:01      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

第一次尝试ajax,第一次尝试SSH。收货颇丰。

最主要还是发现javascript还是比较严谨的语言,一个字母的错误,一个大小写的错误,

都能让自己抓狂。比如:.readyState==4-------》.readystate==4。

XML、和json格式待续。。。。。

把自己的主要代码贴一下:

ajax:

<script type="text/javascript" language="javascript" charset="utf-8">
        var http_request;
        function sendRequest(){
            var u=document.getElementById("username").value;
            //var u=document.getElementsByTagName("username").value ;
            
            if(window.ActiveXObject){
                http_request=new ActiveXObject("Microsoft.XMLHTTP");
            
            }else{
                http_request=new XMLHttpRequest();
            }
            window.alert(http_request);
            if(http_request){
                var url="check.action";
                http_request.open("post",url,true);
                http_request.setRequestHeader(‘Content-type‘,‘application/x-www-form-urlencoded‘); 
                http_request.onreadystatechange=chuli;
                http_request.send("username="+u);
                window.alert(u);
                }
            }
            function chuli(){
            
                if(http_request.readyState==4){
                window.alert(http_request.readyState);
                    if(http_request.status==200){
                        var req=http_request.responseText;
                        window.alert(req);
                        if(req=="fail"){
                    //提示该用户名不可用
                    document.getElementById("myreq").value="该用户不存在";
                    }else{
                    document.getElementById("myreq").value="恭喜,用户名存在";
                    }
                }
            }
            }
    </script>

JSP页面:

<div>
    <form action="@@" method="post" id="myform">
                      <input type="text" name="username" id="username" />用户名??: 
                  <input type="text" name="password" />    密码?: 
                      
                      <input  type="button" value="检验" onclick="sendRequest();"/>
                      <input type="text" name="valid"  id="myreq"/>
                </form>
                
                                  <br/>
                  用户名??: <input type="text" name="username2" />
                    用户密码:<input type="password" name="password2"><br>
                    电子邮件:<input type="text" name="email"><br/>
                    <input type="submit" value="用户注册"/>
                    
                    </div>

struts.xml:

<action name="check" class="checkNameAction">
    
        </action>

applicationContext.xml:

<bean id="checkNameAction" class="com.action.user.CheckNameAction" scope="prototype">
        <property name="service">
            <ref bean="userService"></ref>
        </property>
    </bean>

CheckNameAction.java:

public String execute() throws Exception {
        username=ServletActionContext.getRequest().getParameter("username") ;

        if (this.service.checkUser(username)) {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print("success"); 
        }
        else {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.print("fail"); //将信息写入response
        }
        return null;
    }

 

 


 

SSH项目下,AJAX的实现

标签:

原文地址:http://www.cnblogs.com/skyball/p/4574103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!