Servlet(一)

文章目录

1.Servlet整体框架

2.Servlet快速入门

1.创建项目配置基本环境

上个文件有详细介绍

2.添加jar包
1.在WEB-INF下创建目录lib,添加文件
2.添加到项目中
3.配置代码提示
3.src下创建文件
4.实现Servlet接口
java 复制代码
package HelloServlet;

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

/**
 * @author 孙显圣
 * @version 1.0
 */
public class HelloServlet implements Servlet {
    /**
     * 当创建HelloServlet会调用,且只会调用一次
     * @param servletConfig
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init()");
    }

    /**
     * 返回ServletConfig,也就是servlet的配置
     * @return
     */
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * 1.该方法处理浏览器的请求
     * 2.当浏览器每次请求一次servlet时,就会调用一次
     * 3.当tomcat调用该方法时,会把http请求的数据封装成实现ServletRequest接口的request对象
     * 4.通过servletRequest对象,可以得到用户提交的数据
     * 5.ServletResponsed对象可以用于返回数据给tomcat服务器
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("hi, helloServlet");
    }

    /**
     * 返回servlet的信息,使用较少
     * @return
     */
    @Override
    public String getServletInfo() {
        return null;
    }

    /**
     * 1.该方法是servlet销毁时调用的
     * 2.只会调用一次
     */
    @Override
    public void destroy() {

    }
}
5.在web.xml配置HelloServlet
6.通过浏览器访问HelloServlet

3.浏览器访问Servlet的流程分析

一共有三个容器
url-name
name-全类名
name-实例
Servlet的生命周期
初始化阶段
处理浏览器请求阶段
终止阶段

4.GET和POST的分发处理

代码示例
java 复制代码
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //GET和POST的分发处理
        //1.首先需要知道,ServletRequest的运行类型其实是它的子类HttpServletRequest的实现类,而getMethod方法是这个子接口的,所以需要将这个父接口转换为子接口的类型
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; //现在就是他的子接口,指向它的实现类,封装了请求的方法
        String method = httpServletRequest.getMethod();
        if (method.equals("GET")) {
            doGet();
        }
        else if (method.equals("POST")) {
            doPost();
        }
    }
    //编写doGet和doPost方法,用于分发处理
    public void doGet() {
        System.out.println("doGet被调用");
    }
    public void doPost() {
        System.out.println("doPost被调用");
    }
jsp 复制代码
<%--
  Created by IntelliJ IDEA.
  User: 8615941515990
  Date: 2024/1/24
  Time: 16:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form action="http://localhost:8080/servlet/helloServlet" method="post">
    username: <input type="text" name="username">
  </form>
  </body>
</html>

5.继承HttpServlet来开发Servlet

1.创建文件
2.配置web.xml
3.实现doGet和doPost方法并编写
java 复制代码
package HelloServlet;

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

/**
 * @author 孙显圣
 * @version 1.0
 */
public class HiServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("hi HiServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
4.浏览器访问
5.过程解析
1.http://localhost:8080/servlet/hiServlet发送请求到Tomcat服务
2.Tomcat服务根据资源路径/hiServlet,找到HelloServlet.HiServlet
3.创建HiServlet的实例,并调用它的init()方法和service()方法
4.init()方法就是个空的,重点看service()方法,是它的父类HttpServlet的
6.debug方法
1.打断点
2.debug
3.输入网址
4.此时由于是get方式,所以程序会停在断点处
5.查看运行类型的确是HiServlet

6.Servlet注意事项

最简化的浏览器访问Servlet过程
  1. 浏览器向Tomcat服务发送请求
  2. Tomcat服务根据浏览器地址最后的路由/xxx去web.xml中找到对应的类并创建一个对象
  3. 调用init()方法和service()方法
  4. 第二次则直接调用service()方法

7.Servlet注解方式

1.快速入门
1.代码实例
java 复制代码
package annotation;

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;

/**
 * @author 孙显圣
 * @version 1.0
 */
//使用注解配置路由,一个servlet可以有多个路由,访问网址为http://localhost:8080/servlet/ok1 或者http://localhost:8080/servlet/ok2
@WebServlet(urlPatterns = {"/ok1", "/ok2"})
public class OkServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("注解的doGet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("注解的doPost");
    }
}
2.结果
2.Servlet注解方式解密
1.与xml方式的区别
简要介绍
  1. 浏览器向Tomcat服务发送请求
  2. 服务扫描所有类的全路径并且通过反射获取注解对象,从而得到urlPatterns(路由)
  3. 将其与浏览器地址最后的/xxx比对,如果比对成功则调用init()方法和service()方法
  4. 第二次就直接调用service()方法
2.Servlet注解的参数说明
3.注解URL的4种匹配方式
1.精确匹配
2.目录匹配
3.扩展名匹配
4.任意匹配
4.URL配置注意事项

8.课后练习

1.练习一
login.html
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet/cat" method="post">
  username:<input name="username" type="text">
</form>
</body>
</html>
CatServlet.java
java 复制代码
package HomeWork;

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

/**
 * @author 孙显圣
 * @version 1.0
 */
public class CatServlet implements Servlet {
    private int num = 0; //统计次数
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

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

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("访问次数:" + (++num));
        //向下转型获取请求方式
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String method = httpServletRequest.getMethod();
        if (method.equals("GET")) {
            System.out.println("GET");
        }
        else if (method.equals("POST")) {
            System.out.println("POST");
        }
    }


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

    @Override
    public void destroy() {

    }
}
web.xml
结果
2.练习二
login.html
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/servlet/dog" method="post">
  username:<input name="username" type="text">
</form>
</body>
</html>
DogServlet.java
java 复制代码
package HomeWork;

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

/**
 * @author 孙显圣
 * @version 1.0
 */
public class DogServlet extends HttpServlet {
    //统计次数
    private int get = 0;
    private int post = 0;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("GET:" + (++get));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("POST:" + (++post));
    }
}
web.xml
结果
3.练习三
PigServlet.java
java 复制代码
package HomeWork;

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.net.InetAddress;

/**
 * @author 孙显圣
 * @version 1.0
 */
@WebServlet(urlPatterns = {"/pig1", "/pig2"})
public class PigServlet extends HttpServlet {
    //统计次数
    private int get = 0;
    private int post = 0;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("发出请求的浏览器的ip:" + req.getRemoteAddr());
        System.out.println("GET:" + (++get));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("发出请求的浏览器的ip:" + req.getRemoteAddr());
        System.out.println("POST:" + (++post));
    }
}
结果
相关推荐
U12Euphoria2 小时前
java项目-jenkins任务的创建和执行
java·servlet·jenkins
Wx-bishekaifayuan9 小时前
PHP动物收容所管理系统-计算机设计毕业源码94164
java·css·spring boot·spring·spring cloud·servlet·php
Blood_J18 小时前
Jenkins配置步骤
运维·servlet·jenkins
雷神乐乐1 天前
亲测有效:Maven3.8.1使用Tomcat8插件启动项目
maven·javaweb·tomcat8
乌啼霜满天2491 天前
tomcat与servlet版本对应关系
java·servlet·tomcat
一二小选手1 天前
【Java Web】EL表达式
java·servlet·el
曾经的三心草2 天前
Request和Response
javaweb·request·response·登录与注册实例
像污秽一样2 天前
基于Servlet实现MVC
servlet·mvc
永恒之月℡4 天前
JavaWeb合集23-文件上传
java·javaweb·文件上传
爬山算法4 天前
Tomcat(6) 什么是Servlet容器?
java·servlet·tomcat