1. 什么是Servlet?
      ① Servlet就是JAVA 类
      ② Servlet是一个继承HttpServlet类的类
      ③ 这个在服务器端运行,用以处理客户端的请求
    2. Servlet相关包的介绍
      --javax.servlet.* :存放与HTTP 协议无关的一般性Servlet 类;
      --javax.servlet.http.* :除了继承javax.servlet.* 之外,并且还增加与HTTP协议有关的功能。
        (注意:大家有必要学习一下HTTP协议,因为WEB开发都会涉及到)
        所有的Servlet 都必须实现javax.servlet.Servlet 接口(Interface)。
        若Servlet程序和HTTP 协议无关,那么必须继承javax.servlet.GenericServlet类;
        若Servlet程序和HTTP 协议有关,那么必须继承javax.servlet.http.HttpServlet 类。
      --HttpServlet :提供了一个抽象类用来创建Http Servlet。
        public void doGet()方法:用来处理客户端发出的 GET 请求
        public void doPost()方法:用来处理 POST请求
        还有几个方法大家自己去查阅API帮助文件
      --javax.servlet包的接口:
        ServletConfig接口:
      在初始化的过程中由Servlet容器使用
        ServletContext接口:定义Servlet用于获取来自其容器的信息的方法
        ServletRequest接口:向服务器请求信息
        ServletResponse接口:响应客户端请求
        Filter接口:
      --javax.servlet包的类:
        ServletInputStream类
      :用于从客户端读取二进制数据
        ServletOutputStream类:用于将二进制数据发送到客户端
      --javax.servlet.http包的接口:
        HttpServletRequest接口:
      提供Http请求信息
        HttpServletResponse接口:提供Http响应
    3. Servlet生命周期
      --Servlet生命周期就是指创建Servlet实例后,存在的时间以及何时销毁的整个过程.
      --Servlet生命周期有三个方法
        init()方法
        service()方法:Dispatches client requests to the protected service method 
        destroy()方法:Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
      --Servlet生命周期的各个阶段
        ----实例化:Servlet容器创建Servlet实例
        ----初始化:调用init()方法
        ----服务:如果有请求,调用service()方法
        ----销毁:销毁实例前调用destroy()方法
        ----垃圾收集:销毁实例
    4. Servlet的基本结构
      package web01;
      
      import java.io.IOException;
      import java.io.PrintWriter; import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse; public class FristServlet extends HttpServlet { /**
      * Constructor of the object.
      */
      public FristServlet() {
      super();
      } /**
      * Destruction of the servlet. <br>
      */
      public void destroy() {
      System.out.println("销毁了servlet!");
      } /**
      * The doGet method of the servlet. <br>
      *
      * This method is called when a form has its tag value method equals to get.
      *
      * @param request the request send by the client to the server
      * @param response the response send by the server to the client
      * @throws ServletException if an error occurred
      * @throws IOException if an error occurred
      */
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
      doPost(request, response);//调用doPost
      } /**
      * The doPost method of the servlet. <br>
      *
      * This method is called when a form has its tag value method equals to post.
      *
      * @param request the request send by the client to the server
      * @param response the response send by the server to the client
      * @throws ServletException if an error occurred
      * @throws IOException if an error occurred
      */
      public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException { response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
      out.println("<HTML>");
      out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
      out.println(" <BODY>");
      out.print(" This is ");
      out.print(this.getClass());
      out.println(", using the POST method");
      out.println("<br/>");
      out.println("<hr/>");
      out.println("<h1>Hello world!</h1>");
      out.println(" </BODY>");
      out.println("</HTML>");
      out.flush();
      out.close();
      } /**
      * Initialization of the servlet. <br>
      *
      * @throws ServletException if an error occurs
      */
      public void init() throws ServletException {
      // Put your code here
      System.out.println("我是做初始化工作的!");
      } }

      5. Servlet的部署

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="3.0"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>
      <servlet>
      <servlet-name>FristServlet</servlet-name>
      <servlet-class>web01.FristServlet</servlet-class>
      </servlet>
      <servlet-mapping>
      <servlet-name>FristServlet</servlet-name>
      <url-pattern>/servlet/FristServlet</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      </web-app>

      【注意】

        ① 上面的两个<servlet-name>必须相同
        ② <servlet-class>后面指在对应的类上面.  技巧:你可以直接在你的servlet类中复制过来,这样可以避免出错!
        ③ <url-pattern> 必须是/servlet 再加servlet名字.大家现在就这么记.

      6.Servlet实例演示

      package mybao;
      
      import java.io.IOException;
      import java.io.PrintWriter; import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse; import bean.User; public class OrderServlet extends HttpServlet { /**
      * Constructor of the object.
      */
      public OrderServlet() {
      super();
      } /**
      * Destruction of the servlet. <br>
      */
      public void destroy() {
      System.out.println("我是init()方法!用来进行初始化工作");
      } /**
      * The doGet method of the servlet. <br>
      *
      * This method is called when a form has its tag value method equals to get.
      *
      * @param request
      * the request send by the client to the server
      * @param response
      * the response send by the server to the client
      * @throws ServletException
      * if an error occurred
      * @throws IOException
      * if an error occurred
      */
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException { response.setContentType("text/html;charset=utf-8");
      PrintWriter out = response.getWriter();
      out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
      out.println("<HTML>");
      out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
      out.println(" <BODY>");
      out.print(" This is ");
      out.print(this.getClass());
      out.println(", using the GET method");
      out.println(" </BODY>");
      out.println("</HTML>");
      out.flush();
      out.close();
      } /**
      * The doPost method of the servlet. <br>
      *
      * This method is called when a form has its tag value method equals to
      * post.
      *
      * @param request
      * the request send by the client to the server
      * @param response
      * the response send by the server to the client
      * @throws ServletException
      * if an error occurred
      * @throws IOException
      * if an error occurred
      */
      public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
      // 设置响应给用户的内容类型和字符集
      response.setContentType("text/html;charset=utf-8");
      PrintWriter out = response.getWriter(); // 使用表单元素的name属性获取用户输入的表单数据
      String userName = request.getParameter("username");
      String password = request.getParameter("pwd"); User user = new User();
      user.setUserName(userName);
      user.setPassword(password); //request.setAttribute("ukey", user); if (userName.equals("admin") && password.equals("123456")) {
      // 请求分发器实现页面转发
      request.getRequestDispatcher("success.jsp").forward(request,response);
      } else {
      // 请求重定向实现页面转向
      response.sendRedirect("failure.jsp"); // 使用相对路径
      }
      out.flush();
      out.close();
      } /**
      * Initialization of the servlet. <br>
      *
      * @throws ServletException
      * if an error occurs
      */
      public void init() throws ServletException {
      System.out.println("我是destroy()方法!用来进行销毁实例的工作");
      // Put your code here
      } } web.xml文件
       <servlet>
          <servlet-name>OrderServlet</servlet-name>
          <servlet-class>mybao.OrderServlet</servlet-class>
        </servlet>   <servlet-mapping>
          <servlet-name>OrderServlet</servlet-name>
          <url-pattern>/OrderServlet</url-pattern>
        </servlet-mapping>    
        <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>

第一次接触servlet的知识的更多相关文章

  1. [译]与TensorFlow的第一次接触(三)之聚类

    转自 [译]与TensorFlow的第一次接触(三)之聚类 2016.08.09 16:58* 字数 4316 阅读 7916评论 5喜欢 18 前一章节中介绍的线性回归是一种监督学习算法,我们使用数 ...

  2. 第一次接触FPGA至今,总结的宝贵经验

    从大学时代第一次接触FPGA至今已有10多年的时间,至今记得当初第一次在EDA实验平台上完成数字秒表.抢答器.密码锁等实验时那个兴奋劲.当时由于没有接触到HDL硬件描述语言,设计都是在MAX+plus ...

  3. 孤荷凌寒自学python第五十天第一次接触NoSql数据库_Firebase

    孤荷凌寒自学python第五十天第一次接触NoSql数据库_Firebase (完整学习过程屏幕记录视频地址在文末) 之前对关系型数据库的学习告一段落,虽然能力所限没有能够完全完成理想中的所有数据库操 ...

  4. 第一次接触数据库(SQLite)

    第一次接触,学了创建列表 + 行的删除 + 内容的更改 + 删除列表 第一次接触要知道一些基本知识 NULL(SQL) = Nnoe(python)  #空值 INTEGER = int  #整数 R ...

  5. 第一次接触终极事务处理——Hekaton

    在这篇文章里,我想给出如何与终极事务处理(Extreme Transaction Processing (XTP) )的第一次接触,即大家熟知的Hakaton.如果你想对XTP有个很好的概况认识,我推 ...

  6. Hybird App(一)----第一次接触

    App你知道多少 一 什么是Native App 长处 缺点 二 什么是Web App 长处 缺点 三 什么是Hybrid App 长处 缺点 四 Web AppHybrid AppNative Ap ...

  7. 第一次接触C++------感触

    2018/09/24 上大学第一次接触C++,感觉还挺有趣的. C语言是计算机的一门语言,顾名思义,语言嘛,有它自己独特的语法. 第一次用C++敲代码,觉得还挺不错的,可以从中找到乐趣.咏梅老师布置的 ...

  8. 百度地图API的第一次接触

    因为项目的需求,第一次接触了百度API. 第一步:引用百度地图API的脚本 如果在局域网环境中,要把地图文件和js文件都要下载下来 <script type="text/javascr ...

  9. Servlet基本知识

    Servlet基本知识 1.IDEA创建第一个Servlet程序xing 这里说明如何使用 IDEA Ultimate 2020.1.3版本来新建第一个web程序.参考 MoonChasing 1.1 ...

随机推荐

  1. Servlet生命周期引起的问题

    A:Servlet的定义与作用. B:Serlvet的体系结构 Servlet | | GenericServlet | | HttpServlet | | 用户自定义的Servlet. HttpSe ...

  2. IE 6 ~ 9 CSS Hack 写法总结

    IE 6 ~ 9 CSS Hack 写法总结 24th 四, 14 lip2up [code lang="css"]_color: red;    /* ie6 */*color: ...

  3. 转一个PDevMode格式属性说明...

    找不到原始来源了... //PDevMode = _devicemodeW; // _devicemodeW = record // dmDeviceName: array[0..CCHDEVICEN ...

  4. 游记——noip2016

    2016.11.18 (day 0) 呆在家. 悠闲地呆在家.. 明后天可能出现的错误: 1)没打freopen.打了ctime: 2)对拍程序忘记怎么写了...忘记随机化种子怎么写了: 3)不知道厕 ...

  5. Java读写资源文件类Properties

    Java中读写资源文件最重要的类是Properties 1) 资源文件要求如下: 1.properties文件是一个文本文件 2.properties文件的语法有两种,一种是注释,一种属性配置.  注 ...

  6. Hibernate操作指南-搭建一个简单的示例(基于Java Persistence API JPA)

  7. kdiff3的主窗口说明 Base Local Remote 分别代表什么分支

  8. rplidar & hector slam without odometry

    接上一篇:1.rplidar测试 方式一:测试使用rplidar A2跑一下手持的hector slam,参考文章:用hector mapping构建地图 但是roslaunch exbotxi_br ...

  9. JQ 数字验证

    $.fn.extend({ checknum: function (min, max, accurate) { if ($(this).val() != "") { $(this) ...

  10. final review 报告

    项目名:约跑 组名:nice! 组长:李权 组员:刘芳芳于淼韩媛媛 宫丽君 final Review会议 时间:2016.12.2  代码git的地址:https://git.coding.net/m ...