JavaWeb之Servlet基础(更新中)

    技术2025-04-22  30

    一、概念

    Servlet是JavaEE规范之一,规范就是接口;Servlet是JavaWeb三大组件之一,分别是Servlet程序、Filter过滤器、Listener监听器;Servlet是运行在服务器上的一个Java小程序,它可以接收客户端发送过来的请求,并响应数据给客户端。

    二、具体实现

    1、编写一个类去实现Servlet接口; 2、实现Servlet方法,处理请求,并响应数据; 3、到web.xml中去配置Servlet程序的访问地址。

    /:斜杆在服务器解析的时候,表示地址为http://ip:port/工程路径 工程路径:06_servlet /hello就表示:http://localhost:8080/06_servlet/hello

    在浏览器(客户端)中输入:http://localhost:8080/06_servlet/hello,服务器就会接收到请求,并支持。Java的class程序 对访问做出响应:

    URL地址如何定位到Servlet程序去访问的?

    三、Servlet的生命周期

    1、执行Servlet构造器方法 2、执行init初始化方法 3、执行service方法 4、执行destory方法 点击停止:

    四、请求的分发处理

    package com.atguigu.servlet; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import java.io.IOException; //实际开发中较少用实现的方式,多是使用继承HTTPServlet类的方式去实现Servlet程序 public class HelloServlet implements Servlet { public HelloServlet() { System.out.println("1.构造器方法"); } @Override public void init(ServletConfig servletConfig) throws ServletException { System.out.println("2.init初始化方法"); } @Override public ServletConfig getServletConfig() { return null; } /** * service方法:专门用于处理请求和响应 * @param servletRequest * @param servletResponse * @throws ServletException * @throws IOException */ @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("3.service ===Hello servlet 被访问了"); //类型转换(因为它有getMethod()方法 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请求"); System.out.println("get请求"); } public void doPost(){ System.out.println("post请求"); System.out.println("post请求"); } @Override public String getServletInfo() { return null; } @Override public void destroy() { System.out.println("4.destory销毁方法"); } }

    实际开发中较少用实现的方式,多是使用继承HTTPServlet类 的方式去实现Servlet程序: 1、编写一个类去继承 HttpServlet 类 2、根据业务需要重写 doGet 或 doPost 方法 3、到 web.xml 中的配置 Servlet 程序的访问地址

    package com.atguigu.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; //实际开发中较少用实现的方式,多是使用 继承HTTPServlet类的方式去实现Servlet程序 public class HelloServlet2 extends HttpServlet { /** * doGet()在get请求的时候调用 * @param req * @param resp * @throws ServletException * @throws IOException */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("HelloServlet2的doGET方法"); } /** * doPost()方法,在post请求的时候调用 * @param req * @param resp * @throws ServletException * @throws IOException */ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("HelloServlet2的doPOST方法"); } }

    IDEA菜单生成Servlet程序

    更简便的生成方法: 如下如果不取消掉的话,它会使用3.0以下的注解来做配置: 出现继承的方式实现的Servlet程序:

    package com.atguigu.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 HelloServlet3 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }

    Servlet类的继承体系

    ServletConfig 类 :

    ServletConfig 类从类名上来看,就知道是 Servlet 程序的配置信息类。 Servlet 程序和 ServletConfig 对象都是由 Tomcat 负责创建,我们负责使用。 Servlet 程序默认是第一次访问的时候创建,ServletConfig 是每个 Servlet 程序创建时,就创建一个对应的 ServletConfig 对象。

    Processed: 0.012, SQL: 9