Http用GET方式提交的请求,普通的查询就会进入到此方法。
http用POST方式提交的请求就会进入到此方法
作用:
还有其他的方法,像doHead、doDelete等,一样都是根据http提交Method来区分
url-pattern:以 “/” 开头,可以用/xxx/* 来区分模块, * 是通配符,最好用模块区分,方式通配符都映射成功,但会导致不同优先级的问题。
doGet和doPost的小案例
TestServlet.java
package com.cuikaiyang.web;
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;
import java.io.PrintWriter;
/**
* @author cuikaiyang
* @version 1.0
* @data 8/26/22 9:55 PM
*/
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("Hello doGet 崔凯洋的网络日志");
}
}
使用postman的GET方法测试结果如下:
使用postman的POST方法测试结果如下:
有关状态码为405,可以参考这篇文章:https://blog.csdn.net/Attsky/article/details/126546161
TestServlet.java
package com.cuikaiyang.web;
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;
import java.io.PrintWriter;
/**
* @author cuikaiyang
* @version 1.0
* @data 8/26/22 9:55 PM
*/
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("Hello doGet 崔凯洋的网络日志");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应内容
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("Hello doPost 崔凯洋的网络日志");
}
}
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>testServletservlet-name>
<servlet-class>com.cuikaiyang.web.TestServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>testServletservlet-name>
<url-pattern>/test/findMeurl-pattern>
servlet-mapping>
web-app>
PostMan分别测试GET 和 POST请求:
摘要:本篇文章主要介绍了重写doGet和doPost请求,然后在PostMan里做测试。最重要的是项目要导入Tomcat9安装包lib目录下的servlet-api.jar,然后在自己写的类里面继承HttpServlet并重写doGet和doPost请求。
有关于乱码的问题要设置响应内容类型:
resp.setContentType("text/html;charset=utf-8");
向页面中写入一段字符串需要借助以下代码:
PrintWriter printWriter = resp.getWriter();
printWriter.write("Hello doGet 崔凯洋的网络日志");
熟练的使用快捷键重写-Mac版:control + Enter
还有要了解web.xml 如何绑定一个请求
<servlet>
<servlet-name>testServletservlet-name>
<servlet-class>com.cuikaiyang.web.TestServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>testServletservlet-name>
<url-pattern>/test/findMeurl-pattern>
servlet-mapping>
例如上面这段xml,testServlet是servlet的名字可以随便起名字,但是最好要与类相关,绑定该servlet的类路径。
servlet-mapping里写的是浏览器访问/test/findMe时候,将要访问testServlet。
整体的流程就是: