jspblog : ajax test - json
This commit is contained in:
65
jspblog/src/main/java/com/example/jspblog/test/Ajax2.java
Normal file
65
jspblog/src/main/java/com/example/jspblog/test/Ajax2.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package com.example.jspblog.test;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
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.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
@WebServlet("/ajax2")
|
||||
public class Ajax2 extends HttpServlet {
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
BufferedReader br = request.getReader();
|
||||
String data = br.readLine();
|
||||
System.out.println("data : " + data);
|
||||
|
||||
Gson gson = new Gson();
|
||||
// gson.fromJson() : json -> java object
|
||||
// gson.toJson() : java object -> json
|
||||
TestDto dto = gson.fromJson(data, TestDto.class);
|
||||
System.out.println("dto : " + dto);
|
||||
|
||||
String userJson = gson.toJson(dto);
|
||||
System.out.println("userJson : " + userJson);
|
||||
PrintWriter out = response.getWriter();
|
||||
out.print(userJson);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
}
|
||||
}
|
||||
|
||||
class TestDto {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TestDto{" +
|
||||
"username='" + username + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
39
jspblog/src/main/webapp/test/jsonajax.jsp
Normal file
39
jspblog/src/main/webapp/test/jsonajax.jsp
Normal file
@@ -0,0 +1,39 @@
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
<script
|
||||
src="https://code.jquery.com/jquery-3.5.1.js"
|
||||
integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
|
||||
crossorigin="anonymous"></script>
|
||||
</head>
|
||||
<body>
|
||||
<button onclick="jsonAjax()">클릭</button>
|
||||
|
||||
<script>
|
||||
let data = {
|
||||
username: "kim",
|
||||
password: "1234"
|
||||
}
|
||||
|
||||
// JSON.stringify() : js object -> json
|
||||
// JSON.parse() : json -> js object
|
||||
function jsonAjax() {
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "http://localhost:8080/jspblog/ajax2",
|
||||
data: JSON.stringify(data),
|
||||
contentType: "application/json",
|
||||
dataType: "json"
|
||||
})
|
||||
.done(function (result){
|
||||
console.log(result);
|
||||
console.log(result.username);
|
||||
})
|
||||
.fail(function (error) {
|
||||
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user