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. Netty In Action

    1 introduction 1.2 Asynchronous by design two most common ways to work with or implement an asynchro ...

  2. SQL INSERT INTO 语句

    SQL Order By SQL update INSERT INTO 语句 INSERT INTO 语句用于向表格中插入新的行. 语法 INSERT INTO 表名称 VALUES (值1, 值2, ...

  3. iOS用户信息单例的创建

    UserInfo.h + (UserInfo *) sharedInstance; UserInfo.m #import "UserInfo.h" static UserInfo ...

  4. nginx配置之取消index.php同时不影响js,css功能

    server { listen 8084; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; ...

  5. JSP数据交互

    JSP数据交互   一.jsp中java小脚本 1.<% java代码段%> 2.<% =java表达式%>不能有分号 3.<%!成员变量和函数声明%>二.注释 1 ...

  6. 如何安装并使用hibernate tools

    参考资料:http://radiumwong.iteye.com/blog/358585 http://linjia880714.iteye.com/blog/859334 Hibernate Too ...

  7. jquery 给指定li添加制定的css样式

    $("ul li").eq(1).css({"color":"red"}); //第二个li $("ul li").eq ...

  8. 转:HAR(HTTP Archive)规范

    HAR(HTTP Archive),是一个用来储存HTTP请求/响应信息的通用文件格式,基于JSON.这个格式的出现可以使HTTP监测工具以一种通用的格式导出所收集的数据,这些数据可以被其他支持HAR ...

  9. Linux 利用进程打开的文件描述符(/proc)恢复被误删文件

    Linux 利用进程打开的文件描述符(/proc)恢复被误删文件 在 windows 上删除文件时,如果文件还在使用中,会提示一个错误:但是在 linux 上删除文件时,无论文件是否在使用中,甚至是还 ...

  10. CSS4

    1.处理溢出(overflow) overflow的取值可以是visible.hidden,scroll,auto,其中visible是默认值.visible表示不裁剪内容,也不添加滚动条,强制显示元 ...