方法介绍

public class ServletDemo1 implements Servlet {private ServletConfig thisConfig;//此处演示的是config在getServletConfig和init之间的传递@Overridepublic void init(ServletConfig servletConfig) throws ServletException {thisConfig=servletConfig;//此处赋值System.out.println("init");}@Overridepublic ServletConfig getServletConfig() {return thisConfig;//此处传递}
体系结构Servlet和HttpServlet
get测试和post测试
添加
代码如下,写法是先写extends HttpServlet 然后写dog补齐doget,dop补齐dopost
package top.wjysb.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;@WebServlet("/httpServletDemo1")public class HttpServletDemo1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// super.doGet(req, resp);System.out.println("get");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// super.doPost(req, resp);System.out.println("post");}}
进入http://localhost/httpServletDemo1
控制台输出get表示成功
建立一个表格http文件用于测试post
action中内容对应代码内的@WebServlet("/httpServletDemo1")
method选择post
进入
点击提交控制台输出post表示成功
1.get和post的请求消息不太一致
2.获取请求方式method然后根据get和post分别处理
tip @Override代表重写
要做的就是在httpServlet中重写doget和dopost方式
在这两个函数中写出不同的处理逻辑
