在前后端的交互中,前端通过以下三种方式来与后端进行交互🌟
✅query string
✅form表单
✅JSON字符串
下面我们将书写这三种方式的后端代码并进行讲解
1、Query String
QueryString即在url中写入键值对,一般用doGet方法进行交互
代码如下
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("/getParameter")
public class GetParameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
resp.getWriter().write("ok");
}
}
用postman验证
2、form表单
form表单一般用doPost方法进行交互
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("/postParameter")
public class postParameterServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
System.out.println(username);
System.out.println(password);
resp.getWriter().write("ok");
}
}
3、Jason字符串
(1)关于Jason
Jason是什么
JSON 是存储和交换文本信息的语法,类似 XML。JSON采用完全独立于任何程序语言的文本格式
要在Servlet中实现Jason相关操作,我们使用Jackson解析器
(2)jackson的配置
怎样配置Jackson呢?🧐
在中央仓库搜索Jackson,选中Jackson Databind👆
点击进去之后,选择2.15.0版本👆
(Jackson2.15.0对应Tomcat8、对应Servlet3.1.0)
进去后,点击复制下面这段代码📃
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.0</version>
</dependency>
粘贴到pom.xml文件中
之后会出现Maven的按钮,点击进行下载
下载完成后,原本出现的红字报错消失
(3)使用json字符串进行前后端交互
在配置好json的解析器后,我们书写代码进行前后端交互
import com.fasterxml.jackson.databind.ObjectMapper;
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;
//定一个Request类
class Request{
public String username;
public String password;
}
class Response{
public Boolean ok;
}
@WebServlet("/json")
public class JsonParameterSerlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ObjectMapper mapper = new ObjectMapper();
Request request = mapper.readValue(req.getInputStream(),Request.class);
//把json字符串映射成Request类的java对象
System.out.println("username:" + request.username);
System.out.println("password:" + request.password);
Response response = new Response();
response.ok = true;
//将Response类的java对象映射成json字符串
String respjson = mapper.writeValueAsString(response);
resp.setContentType("applicaton/json;charset=utf8");
resp.getWriter().write(respjson);
}
}
使用postman进行验证