jquery调后台服务

来源:互联网 发布:颐和园结局 知乎 编辑:程序博客网 时间:2024/06/04 22:46
private void processRequest(final HttpServletRequest req, final HttpServletResponse resp) {
    try {
    final Map<String, Object[]> params = new HashMap<String, Object[]>();
            params.putAll(req.getParameterMap());
            String command=params.get("command")[0].toString();
            String username=params.get("username")[0].toString();
            String password=params.get("password")[0].toString();
            String result = null;
            ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
            PropertyPlaceholderConfigurer propertyConfigurer =  (PropertyPlaceholderConfigurer)ctx.getBean("propertyConfigurer");
            BasicDataSource ds = (BasicDataSource)ctx.getBean("dataSource");
            String dbdriver = ds.getDriverClassName();
            String dburl = ds.getUrl();
            String dbuser = ds.getUsername();
            String dbpass = ds.getPassword();
            userService = (UserService)ctx.getBean("userService");
            //userDao = (UserDao)ctx.getBean("userDao");
            //WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());  
            //UserService userService = (UserService)ctx.getBean("userService");
    User user = userService.getUserById(1); 
            if("login".compareTo(command)==0) {
            result = userService.login(username, password);
            }
    resp.setContentType("application/json; charset=UTF-8");
        resp.setStatus(HttpServletResponse.SC_OK);
        resp.getWriter().print("{\"result\":\"" + result + "\"}");
} catch (IOException e) {

e.printStackTrace();
}
        return;

    }


<body>

登录名:<input type="text" id="username">
          密码:<input type="password" id="password">
    <button id="login">登陆</button>

</body>


/**
 * 
 */
/*jquery函数*/

(function($, window, document,undefined) {
    
function fun1() {
$("div").css("color", "red");
};

function login(name, password){
var data = {
                username: name,
                password: password
            };

$.ajax( {  
   type : "POST",  
   url :  "/cids/api?command=login&response=json",
   data: data,
   dataType: "json",
            async: false,  
   success : function(data) {  
       if(data.result == "true"){  
           alert("登录成功!"); 
       }  
       else{  
           alert("登录失败!");  
       }  
       if(data.session =="1234567890"){  
           alert("1234567890");  
       }  
   },  
   error :function(){  
       alert("网络连接出错!");  
   }  
});  
}  

$(document).ready(function() {
$("#login").click(function() {
login($("#username").val(), $("#password").val());
});
})
    
})(jQuery, window, document);

原创粉丝点击