zoukankan      html  css  js  c++  java
  • java学习笔记—Servlet技术(11)

    如果大家要开发一个动态的网站,那么就必须要学习一种动态的网页开发技术。那么在SUN提供的JavaEE中主要包含两种开发动态网页的技术:Servlet和JSP技术。

    Servlet技术简介

    Servlet技术是SUN提供的一种开发动态网页的核心组件之一。可以方便的开发动态网页。主要用得语言是java,开发者只需要实现相应的接口或者继承相应的类,那么你的java文件就是一个动态的网页。当然好需要一些额外的配置即可。

    一个Servlet其实就是一个运行在web server上得一个java程序。

    Servlet结构体系

    为了方便开发者进行基本的动态网页开发,那么SUN提供了一整套接口和类帮助开发者进行高效的开发。

    1  Servlet接口

    2  GenericServlet类
    GenericServlet实现了Servlet接口的抽象类。

    3  HttpServlet类

    HttpServlet继承了GenericServlet的抽象类。

    Servlet体验

    1.    建立动态网站的目录结构
    2.    编写一个动态网页如下HelloServlet.java
    package cn.itcast.servlets;
    import javax.servlet.*;
    import java.io.*;
    public class HelloServlet extends GenericServlet
    {
        public void service(ServletRequest req,ServletResponse res)
           throws ServletException,IOException{
           // 创建一个需要输出的数据
           String data = "hello servlet!";
           // 将以上的数据发送给浏览器进行显示
           res.getOutputStream().write(data.getBytes());
        }
    }
    3.    编译以上的HelloServlet.java
    将JavaEE需要的jar包引入到classpath环境变量
    set classpath=%tomcat_home%libservlet-api.jar
    编译
    D:	est>javac -d . HelloServlet.java
    4.    将编译好的包连同class文件剪切到网站的WEB-INFclasses目录
    5.    将class文件映射为浏览器需要的URL路径
    修改web.xml文件如下
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app 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_2_5.xsd"
        version="2.5">
         <servlet>
            <servlet-name>helloservlet</servlet-name>
            <servlet-class>cn.itcast.servlets.HelloServlet</servlet-class>
         </servlet>
         <servlet-mapping>
            <servlet-name>helloservlet</servlet-name>
            <url-pattern>/helloservlet</url-pattern>
        </servlet-mapping>
    </web-app>
    6.    将编写好的网站部署到tomcat的webapps目录
    http://localhost:8080/test/helloservlet 

    运行结果:hello servlet

    总结:

    1. 编译servlet比较繁琐。
    2. 配置servlet比较繁琐。

    2  IDE体验

    1. 自动编译
    2. 自动配置
    3. 集成发布

    Servlet、GenericServlet和HttpServlet

    1  Servlet接口

    Servlet接口主要用于定义初始化servlet、处理用户请求、从web server中移除servlet等生命周期方法。如果开发者需要实现servlet接口,那么推荐继承GenericServlet或HttpServlet。

    1. The servlet is constructed, then initialized with the init method.       构造函数、init()
    2. Any calls from clients to the service method are handled.          service()
    3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.            

    举例1:体验生命周期方法。

    public class Demo2 extends GenericServlet {
        // 创建对象
        public Demo2(){
            System.out.println("构造 函数");
        }
        // 初始化
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            System.out.println("init方法");
        }
        // 处理用户的请求
        public void service(ServletRequest arg0, ServletResponse arg1)
                throws ServletException, IOException {
            System.out.println("service方法");
        }
        // 移除方法
        public void destroy() {
            super.destroy();
            System.out.println("destroy方法");
        }
    }

    总结:

    1.    servlet 执行的流程是:创建对象初始化处理用户请求销毁
    2.    创建对象初始化只是执行一次
    3.    销毁在服务器关闭的时候进行执行
    4.    只有用户第一次访问servlet时候如果web server中没有该servlet的对象那么才创建。(懒装载)
    5.    servlet定义的生命周期方法全部由web server自己调用(回调)
    6.    servlet全部是单例
    7.    一般在实际开发中主要使用的service方法。

    2 GenericServlet抽象类

    该类是一个通用的servlet类,实现Servlet和ServletConfig接口。如果要实现HTTTP协议,那么请继承HttpServlet类。

    该类使得定义servlet变得简单,提供了一些日志、ServletConfig、以及版本的方法。内部声明了一个ServletContext接口类。

    该类默认的对Servlet接口的方法进行空实现。但是对于init方法它获取了传递进来的ServletConfig类赋值给了自己定义的ServletConfig成员变量。随后调用了自己的inti()方法。

    定义了唯一的一个抽象方法service().

    3  HttpServlet抽象类

    如果一个网站需要实现HTTP协议的Servlet,那么必须是HttpServlet的子类。那是作为HttpServlet的子类必须重写以下方法中的至少一个:

    doGet, if the servlet supports HTTP GET requests      处理用户的GET请求
    doPost, for HTTP POST requests                      处理用户的POST请求
    doPut, for HTTP PUT requests                      处理用户的PUT请求
    doDelete, for HTTP DELETE requests              处理用户的DELETE请求
    init and destroy, to manage resources that are held for the life of the servlet  生命周期方法
    getServletInfo, which the servlet uses to provide information about itself     获取servlet信息

    我们应该主要的重写doGet和doPost方法。

    举例:使用HttpServlet实现处理用户的请求。

    public class Demo3 extends HttpServlet {
        public Demo3(){
            System.out.println("创建对象");
        }
        public void init(ServletConfig config) throws ServletException {
            // TODO Auto-generated method stub
            super.init(config);
            System.out.println("初始化");
        }
        // 处理用户的get请求,地址栏直接回车、a、form默认
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            System.out.println("处理用户的get请求");
        }
        // form表单中的method修改为post
        public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            System.out.println("处理用户的POST请求");
        }
    }

    思考:HttpServlet继承自GenericServlet实现了Servlet接口,但是自己使用doGet和doPost方法处理用户的请求,那么还需要原来定义的service()?

    Tomcat处理用户请求的时候一定执行的Servlet接口中定义的service()

    但是用请求的Servlet如果直接继承了HttpServlet那么还是执行Servlet接口的service()方法

    该方法中默认调用HttpServlet中自定义的实现了Http协议的service(),该方法中又将请求转发给了相应的doGet或doPost()导致最终处理用户请求的方法是doGet或doPost()。

    但是如果开发者手工的重写了Servlet接口的service方法,那么默认不能进行转发。

    举例1: 阅读以下程序的运行结果。

    public class Demo3 extends HttpServlet {
        public Demo3(){
            System.out.println("创建对象");
        }
        public void init(ServletConfig config) throws ServletException {
            // TODO Auto-generated method stub
            super.init(config);
            System.out.println("初始化");
        }
        // 处理用户的get请求,地址栏直接回车、a、form默认
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            System.out.println("处理用户的get请求");
        }
        // form表单中的method修改为post
        public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            System.out.println("处理用户的POST请求");
        }
        @Override
        public void service(ServletRequest req, ServletResponse res)
                throws ServletException, IOException {
            super.service(req, res);
            System.out.println("service方法");
        }
        @Override
        public void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
            super.service(req, resp);
            System.out.println("httpservlet service");
        }
    }

    运行结果如下:

    创建对象

    初始化

    处理用户的POST请求

    httpservlet service

    service方法

  • 相关阅读:
    jQuery序列化
    jQuery的ajax与django传参
    Django中的cookie与session操作
    Django文件上传
    Django表单的简单应用
    django加载模板文件
    django-admin.py创建项目失败解决方法
    django笔记
    unity创建xml与加载xml
    JavaScript相关
  • 原文地址:https://www.cnblogs.com/zhenghongxin/p/4355748.html
Copyright © 2011-2022 走看看