Servlet

1、介绍

  • 什么是Servlet?

是运行在服务器段的程序,用来接受和响应来自客户端基于HTTP协议的请求
image

2、执行流程

2.1、基于配置文件的开发()

  • 需要在web.xml中配置好Servlet
  • 每刷新一次浏览器都会执行一次service方法
    image
  • 常用的是继承HttpServlet
    image
    image
    image

2.2、注解式开发(基本上都是用这种方式)

在servlet的java文件里面添加下面的注解即可,就可以代替上面的配置

@WebServlet("/虚拟路径名")

3、只用写doGet和doPost其中之一的写法

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class Servlet01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hello world");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//调用doGet即可!
        super.doGet(req, resp);
    }
}

其它

  • Application context
    虚拟目录
  • servlet入口的url
    写在地址栏Application context后面
    image

html的表单属性怎么写

form action = "/虚拟目录/servlet访问目录"
method = "get或者post"

servlet获取数据代码

req.getParameter("属性名")

posted @ 2021-07-22 17:51  猪猪猪猪侠  阅读(35)  评论(0编辑  收藏  举报