美文网首页
Servlet学习笔记(一)

Servlet学习笔记(一)

作者: dev_winner | 来源:发表于2019-10-31 15:46 被阅读0次

    Servlet的定义:

    Servlet是在服务器上运行的小程序。一个Servlet就是一个java类,并且可以通过“请求-响应”编程模型来访问这个驻留在服务器中内存里的Servlet小程序。

    Tomcat的容器等级:

    Tomcat的容器分为四个等级,Servlet的容器管理Context容器,一个Context对应一个Web工程。

    Tomcat容器等级

    编写第一个Servlet小程序:

    • 继承HttpServlet
    • 重写doGet()或者doPost()方法
    • 在web.xml中注册Servlet
    自定义servlet
    • 创建测试类HelloServlet.java,其继承抽象类HttpServlet
    package com.zzw.servlet;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class HelloServlet extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("处理Get()请求...");
            PrintWriter out = response.getWriter();
            //指定输出的文件类型
            response.setContentType("text/html;charset=utf-8");
            //向浏览器输出信息
            out.println("<h1>Hello Servlet!</h1><br/>");
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("处理Post()请求...");
            PrintWriter out = response.getWriter();
            response.setContentType("text/html;charset=utf-8");
            out.println("<h1>Hi Welcome!</h1><br/>");
        }
    }
    
    • 编写初始化页面index.jsp
    <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8" %>
    <html>
    <body>
        <h1>第一个Servlet</h1>
        <h2>Hello World!</h2>
        <a href="servlet/HelloServlet">Get方式请求HelloServlet</a>
        <hr/>
        <form action="servlet/HelloServlet" method="post">
            <input type="submit" value="Post方式请求HelloServlet" />
        </form>
        <hr/>
    </body>
    </html>
    
    • 注册servlet实例,修改web.xml文件
    <!DOCTYPE web-app PUBLIC
            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
        <display-name>Archetype Created Web Application</display-name>
    
        <!--注册servlet,处理类-->
        <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <!--包名+类名-->
            <servlet-class>com.zzw.servlet.HelloServlet</servlet-class>
        </servlet>
        
        <!--请求地址映射到上面的实体类,两者的ServletName要保持一致-->
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
        <!--先写一个斜杆,表示当前项目的根目录,然后对应jsp页面中某个链接请求-->
            <url-pattern>/servlet/HelloServlet</url-pattern>
        </servlet-mapping>
    </web-app>
    
    Servlet执行流程

    Servlet的生命周期

    • 该阶段包括初始化加载实例化服务销毁

    1、初始化阶段,调用init()方法;
    2、响应客户请求阶段,调用service()方法。由service()方法根据提交方式选择执行doGeT()或者doPost()方法;
    3、终止阶段,调用destroy()方法。

    servlet的生命周期
    • 在下列时刻Servlet容器装载Servlet:
    1. Servlet容器启动时自动装载某些Servlet,实现它只需要在web.xml文件中的<Servlet></Servlet>之间添加如下代码:<loadon-startup>1</loadon-startup>,数字越小表示优先级别越高;
    2. 在Servlet容器启动后,客户端首次向Servlet发送请求;
    3. Servlet类文件被更新后,Servlet容器将会重新装载Servlet;(tomcat服务器一启动,Servlet实例长期驻留在内存中)
    4. Servlet被装载后(类的构造函数先执行),Servlet容器创建一个Servlet实例时首先调用Servlet的init()方法进行初始化。在Servlet的整个生命周期内,init()方法只被调用一次。
    • 修改HelloServlet.java文件
    package com.zzw.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class HelloServlet extends HttpServlet{
    
        public HelloServlet() {
            System.out.println("HelloServlet的构造方法被执行......");
        }
    
        @Override
        public void init() throws ServletException {
            super.init();
            System.out.println("HelloServlet的init方法被执行......");
        }
    
        @Override
        public void destroy() {
            System.out.println("HelloServlet的destroy方法被执行......");
        }
    
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("HelloServlet中的doGet方法被执行...");
            PrintWriter out = response.getWriter();
            //指定输出的文件类型
            response.setContentType("text/html;charset=utf-8");
            //向浏览器输出信息
            out.println("<h1>Hello Servlet!</h1><br/>");
        }
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            System.out.println("HelloServlet中的doPost方法被执行...");
            PrintWriter out = response.getWriter();
            response.setContentType("text/html;charset=utf-8");
            out.println("<h1>Hi Welcome!</h1><br/>");
        }
    }
    
    • 创建测试类TestServlet1.java,其继承抽象类HttpServlet
    package com.zzw.servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class TestServlet1 extends HttpServlet {
    
        public TestServlet1() {
            System.out.println("TestServlet1的构造方法被执行......");
        }
    
        @Override
        public void init() throws ServletException {
            super.init();
            System.out.println("TestServlet1的init方法被执行......");
        }
    
        @Override
        public void destroy() {
            System.out.println("TestServlet1的destroy方法被执行......");
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("TestServlet1的doGet方法被执行......");
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.println("<h1>Hello!我是TestServlet1</h1><br/>");
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println("TestServlet1的doPost方法被执行......");
            //设置响应文本类型
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            out.println("<h1>Hi!Welcome!</h1><br/>");
        }
    }
    
    • 修改初始化页面index.jsp
    <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8" %>
    <html>
    <body>
        <h1>第一个Servlet</h1>
        <h2>Hello World!</h2>
        <a href="servlet/HelloServlet">Get方式请求HelloServlet</a>
        <hr/>
        <form action="servlet/HelloServlet" method="post">
            <input type="submit" value="Post方式请求HelloServlet" />
        </form>
        <hr/>
        <h1>Servlet生命周期</h1>
        <a href="servlet/TestServlet1">以Get方式请求TestServlet1</a>
        <form action="servlet/TestServlet1" method="post">
            <input type="submit" value="Post方式请求TestServlet1" />
        </form>
        <hr/>
    </body>
    </html>
    
    • 注册servlet实例,修改web.xml文件
    <!DOCTYPE web-app PUBLIC
            "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
            "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
        <display-name>Archetype Created Web Application</display-name>
    
        <!--注册servlet,处理类-->
        <servlet>
            <servlet-name>HelloServlet</servlet-name>
            <!--包名+类名-->
            <servlet-class>com.zzw.servlet.HelloServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet>
            <servlet-name>TestServlet1</servlet-name>
            <servlet-class>com.zzw.servlet.TestServlet1</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
    
        <!--请求地址映射到上面的实体类,两者的ServletName要保持一致-->
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
        <!--先写一个斜杆,表示项目的根目录,然后对应jsp页面中某个链接请求-->
            <url-pattern>/servlet/HelloServlet</url-pattern>
        </servlet-mapping>
    
        <servlet-mapping>
            <servlet-name>TestServlet1</servlet-name>
            <url-pattern>/servlet/TestServlet1</url-pattern>
        </servlet-mapping>
    
    </web-app>
    
    servlet容器自动装载servlet的执行结果 servlet与九大内置对象

    相关文章

      网友评论

          本文标题:Servlet学习笔记(一)

          本文链接:https://www.haomeiwen.com/subject/lbrzvctx.html