Servlet可以接收客户端发送过来的请求,并响应数据给客户端。

一. 通过实现Servlet接口的方式来实现Servlet程序

通常不需要实现Servlet接口,而是继承HTTPServlet类即可。

ServletRequestAttributes 怎么创建set header 创建servlet程序_ide

1. 创建Servlet程序步骤:

①编写一个类去实现Servlet接口(使用快捷键ALT+INSERT或ALT+SHIFT+0实现接口中的所有方法)

②实现Servlet接口中的service方法,处理请求并响应数据

import javax.servlet.*;
import java.io.IOException;

public class HelloServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    //service方法是专门用来处理请求和响应的
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Hello Servlet");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}

③在web.xml中去配置servlet程序的访问地址

<?xml version="1.0" encoding="UTF-8"?>
<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标签给Tomcat配置Servlet程序 -->
    <servlet>
        <!-- servlet-name标签给Servlet程序起一个别名(一般是类名) -->
        <servlet-name>HelloServlet</servlet-name>
        <!-- servlet-class标签是Servlet程序的全类名 -->
        <servlet-class>TestServlet.HelloServlet</servlet-class>
    </servlet>

    <!-- servlet-mapping标签给Servlet程序配置访问地址 -->
    <servlet-mapping>
        <!-- servlet-name标签的作用是告诉服务器,当前配置的地址是给哪个Servlet程序使用 -->
        <servlet-name>HelloServlet</servlet-name>
        <!-- url-pattern标签配置访问地址
             / 斜杠表示地址:http://ip:port/工程路径
             /hello表示地址为:http://ip:port/工程路径/hello,该地址为自己任意设置的地址名
        -->
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

④运行时默认运行的是index.xml程序,需要在原路径后添加访问HelloServlet程序的地址hello,即http://ip:port/工程路径/hello,则运行结果会出现重写的service方法的内容。

2.Servlet的生命周期:①执行Servlet构造器方法;②执行init方法;③执行service方法(每次访问都会执行);④执行destroy方法(在web工程停止时调用)

3. GET和POST请求的分发处理

①在web目录下写一个test.html如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="http://localhost:8080/WebProject_war_exploded/hello" method="get">
      <input type="submit">
    </form>
</body>
</html>

②在service方法下创建doGet和doPost方法来分别实现GET请求和POST请求

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class HelloServlet implements Servlet {
    public HelloServlet() {
        System.out.println("1构造器");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("2初始化");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    //service方法是专门用来处理请求和响应的
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("Hello Servlet");

        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String method = httpServletRequest.getMethod();   //获取请求的方式
        if("GET".equals(method)){
            doGet();
        }else if("POST".equals(method)){
            doPost();
        }
    }

    public void doGet(){
        System.out.println("该请求是GET请求");
    }

    public void doPost(){
        System.out.println("该请求是POST请求");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("4销毁");
    }
}

③通过执行test.html来测试是GET请求还是POST请求

二.通过继承HTTPServlet类的方式来实现Servlet程序

1.编写一个类去继承HTTPServlet类

2.根据开发的实际需求去重写doGet和doPost方法

3.在web.xml中配置Servlet程序的访问地址

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 HelloHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("GET请求");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("POST请求");
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<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标签给Tomcat配置Servlet程序 -->
    <servlet>
        <!-- servlet-name标签给Servlet程序起一个别名(一般是类名) -->
        <servlet-name>HelloServlet</servlet-name>
        <!-- servlet-class标签是Servlet程序的全类名 -->
        <servlet-class>TestServlet.HelloServlet</servlet-class>
    </servlet>

    <!-- servlet-mapping标签给Servlet程序配置访问地址 -->
    <servlet-mapping>
        <!-- servlet-name标签的作用是告诉服务器,当前配置的地址是给哪个Servlet程序使用 -->
        <servlet-name>HelloServlet</servlet-name>
        <!-- url-pattern标签配置访问地址
             / 斜杠表示地址:http://ip:port/工程路径
             /hello表示地址为:http://ip:port/工程路径/hello,该地址为自己任意设置的地址名
        -->
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>HelloHttpServlet</servlet-name>
        <servlet-class>TestServlet.HelloHttpServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloHttpServlet</servlet-name>
        <url-pattern>/hellohttp</url-pattern>
    </servlet-mapping>
</web-app>