欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

笔记-JavaWeb学习之旅18

程序员文章站 2022-12-24 16:08:08
AJAX:ASynchronous JavaScript And XML 异步的JavaScript 和XML 异步和同步:客户端和服务器端相互通信的基础上 同步:客户端操作后必须等待服务器端的响应,在等待的期间客户端不能做其他操作 异步:客户端操作后,不需要等待服务器端的响应,在服务器处理请求的过 ......
ajax:asynchronous javascript and xml 异步的javascript 和xml

异步和同步:客户端和服务器端相互通信的基础上

同步:客户端操作后必须等待服务器端的响应,在等待的期间客户端不能做其他操作

异步:客户端操作后,不需要等待服务器端的响应,在服务器处理请求的过程中,客户端可以进行其他的操作

jquery对ajax实现

实现异步的方式

使用$.ajax()发送异步请求

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script>
        //定义方法
        function fun(){
            //使用jquery的方式实现异步请求
            $.ajax({
                url:"ajaxservlet1",//必选参数
                type:"post",//请求方式,可选参数
                data:{"username":"jack","age":23},//请求参数,可选
                success:function(data){
                    alert(data);//data变量是服务器响应给会的结果值
                },//响应成功后的回调函数,可选
                error:function(){
                    alert("出错啦...");
                },//请求响应出现错误,会执行的回调函数
               datatype:"text"//设置接受到的响应数据的格式
            });
        }
    </script>
</head>
<body>
    <input type="button" value="发送异步请求" onclick="fun();">
    <input>
</body>
</html>
package com.data.ajax;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;

@webservlet("/ajaxservlet1")
public class ajaxservlet1 extends httpservlet {
    protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        //获取请求参数
       string username =  request.getparameter("username");
       string age =  request.getparameter("age");
        system.out.println(username);
       //响应请求请求
        response.getwriter().write("hello :" + username+age);
    }

    protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
        this.dopost(request,response);
    }
}

使用$.get()和$.post()发送异步请求

$.get(url,[data],[callback],[type])

url:请求路径

data:请求参数

callback:回调函数,载入成功后执行的函数

type:响应结果的类型

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script>
        function fun() {
            $.get("ajaxservlet1",{"username":"zhangsan"},function (data) {
                alert(data)
            })
        }
    </script>
</head>
<body>
    <input type="button" value="发送异步请求" onclick="fun();">
    <input>
</body>
</html>

$.post(url,[data],[callback],[type])

json:

1.概念:javascript object notation javascript对象表示法

json现在多用于存储和交换文本信息的语法

进行数据的传输

json比xml更小,更快,更易解析

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <script>
        var person1={"name":"张三",age:23,'gender':"true"};
        //遍历对象
        for(key in person1){
            alert(key+":"+person1[key]);
        }
        //获取name的值
        var name1 = person1.name;
        alert(name1)
        //嵌套格式{}->[]
        var person2 = {
            "person":[
            {"name":"张三","age":23,"gender":true},
            {"name":"李四","age":24,"gender":true},
            {"name":"王五","age":25,"gender":false},
        ]
        };
        //嵌套格式  []-{}
        var person3=[
            {"name":"张三","age":23,"gender":true},
            {"name":"李四","age":24,"gender":true},
            {"name":"王五","age":25,"gender":false},
        ]
        alert("===============")
        //遍历数组person3
        for(var i=0;i<person3.length;i++){
            var l=person3[i];
            for(key in l ){
                 alert(key+l[key]);
            }
        }
    </script>
</head>
<body>
</body>
</html>

json解释器--jackson

常见json解释器:jsonlib,gson,fastjson,jackson

把java对象转换为json对象

注解:@jsonignore:排除不需要的属性,@jsonformat:属性值格式化

复杂java对象转换

1.list:转换出来的是数组

3.map:转换出来结果与对象格式一致

package com.data.domain;

import com.fasterxml.jackson.annotation.jsonformat;

import java.util.date;

public class person {
    private string name;
    private string gender;
    private int age;
    @jsonformat(pattern = "yyyy-mm-dd")//设置指定格式
    private date birthday;

    public person(string name, string gender, int age, date birthday) {
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.birthday = birthday;
    }
    public person(string name, string gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public person() {

    }

    @override
    public string tostring() {
        return "person{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                '}';
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public string getgender() {
        return gender;
    }

    public void setgender(string gender) {
        this.gender = gender;
    }

    public int getage() {
        return age;
    }

    public void setage(int age) {
        this.age = age;
    }

    public date getbirthday() {
        return birthday;
    }

    public void setbirthday(date birthday) {
        this.birthday = birthday;
    }
}
package com.data.test;

import com.data.domain.person;
import com.fasterxml.jackson.databind.objectmapper;
import org.junit.test;
import java.io.file;
import java.util.*;

public class testdemo1 {
    //把java对象转化为json对象
    @test
    public void test1() throws exception {
        person person = new person("张三","男",20);
        //创建jackson的核心对象,objectmapper
        objectmapper mapper = new objectmapper();
   /*
    writevalueasstring(obj):将对象转为json字符串
    转换方法:writevalue(参数1,obj):该方法有很多重载方法
    参数1:
        file:将obj对象转换为json字符串,并保存到指定的文件中
        writer:将obj对象转换为json字符串,并将json数据填充到字符输出流中
        outputstream:将obj对象转换为json字符串,并将json数据填充到字节输出流中

   */
        string s = mapper.writevalueasstring(person);
        system.out.println(s);//{"name":"张三","gender":"男","age":20}
        //将json写入文件
        mapper.writevalue(new file("e://a.txt"),person);
    }
    @test
    public void test2() throws exception {
        person person = new person("张三","男",20, new date());
        objectmapper mapper = new objectmapper();
        string s = mapper.writevalueasstring(person);
        system.out.println(s);
    }
        //list集合转json对象
    @test
    public void test3() throws exception {
        person person1 = new person("张三","男",20, new date());
        person person2 = new person("李四","女",22, new date());
        person person3 = new person("王五","男",23, new date());
        person person4 = new person("赵六","女",24, new date());
        //创建list集合
        list<person> list = new arraylist<>();
        list.add(person1);
        list.add(person2);
        list.add(person3);
        list.add(person4);
       //把list集合转换为json对象
        objectmapper mapper = new objectmapper();
        string s = mapper.writevalueasstring(list);
        system.out.println(s);
    }

    @test
    public void test5() throws exception {
        map<string,object> map = new hashmap<>();
        map.put("name","张三");
        map.put("age",22);
        map.put("gender","男");
        objectmapper mapper = new objectmapper();
        string s = mapper.writevalueasstring(map);
        system.out.println(s);
    }
    //把json对象转换为java对象
    @test
    public void test6() throws exception {
        //初始化json字符串
        string json = "{\"gender\":\"男\",\"name\":\"张三\",\"age\":22}";
        objectmapper mapper = new objectmapper();
        person s = mapper.readvalue(json,person.class);
        system.out.println(s);
    }

}