zoukankan      html  css  js  c++  java
  • Servlet学习笔记

    Servlet学习笔记
    1.0 Servlet 概述
        1.1 Servlet是运行在服务器端的Java小程序,通过HTTP协议用于接受客户端请求,并发出响应。
        1.2 Servlet中的方法
        public void servlet(ServletRequest req,ServletRequest res)
            throws ServletExcepation,java.io.IOException
        ServletRequest req:代表着请求对象,该对象有HTTP协议的请求部分的所有内容。它的实现类由服务器提供,封装数据也是服务器来做。
        ServletRequest res:代表着响应对象,该对象中由我们写数据(HTTP协议的响应部分)进去。它的实现类也是由服务器提供的。
        service:            由服务器调用,每次请求都会调用一次。服务器采用的是多线程机制。
    2.0 Servlet 的编码步骤
        2.1 编写一个类实现javax.servlet接口,或者继承javax.servlet.GenericServlet.
        2.2 编译
        set classpath=%classpath%;E:IDE	omcat-8.0.26libservlet-api.jar
        javac -d . HelloServlet.java
        2.3 修改web.xml
        2.4 部署,把应用部署到Tomcat上,访问地址:http://localhost:8080/DemoServlet/hello
    
    3.0 Servlet 的执行过程
    4.0 Servlet 的编写方式
        1、javax.servlet.GenericServlet:通用的Servlet实现,抽象类
        2、javax.servlet.http.HttpServlet:与HTTP协议有关的,抽象类。
        3、Servlet的核心类
    5.0 Servlet 的生命周期
    6.0 Servlet 的线程安全
        尽量使用局部变量,避免使用实例变量。
    
    7.0 Servlet 的一些细节
        7.1 一个Servlet可以映射到多个地址上。
        7.2 可以使用地址通配符*
            7.2.1(优先级高) *.do 必须以*号开头        如:*.do
            7.2.2(优先级高) 以/开头,必须以*结尾     如:/action/*
            7.2.3 默认的Servlet,映射路径是<url-pattern>/</url-pattern>。不需要配,因为默认的Servlet负责处理用户请求找不到的处理工作。一切都是Servlet。
            7.2.4 应用在启动时就完成Servlet的实例化和初始化。2为启动的顺序。
                web.xml
                <load-on-startup>2</load-on-startup>
    8.0 ServletConfig:Servlet的参数配置
        8.1 ServletConfig:代表看针对当前Servlet的参数配置。
            在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
        8.2 如何得到ServletConfig对象的应用:在初始化Servlet,由容器产生,并传递给你
    
    9.0 ServletContext 
        9.1 ServletContext代表着当前应用。每个应用只有一个ServletContext对象的实例,由容器提供。
        9.2 如何获取ServletContext的实例:ServletConfig.getServletContext();
        9.3 ServletContext的生命周期:诞生,应用被加载时就由容器被创建好。活着,应用不挂就一直活着。死亡,应用挂了就挂了。
        9.4 域(存活范围)对象:ServletContext称为应用范围域对象。
                ServletContext{
                    private Map<String,Object>map = new HashMap<String,Object>();
                    public void setAttribute(String key,Object value){
                        map.put(key,value);
                    }
                    public void removeAttribute(String key){
                        map.remove(key);
                    }
                    public void getAttribute(String key){
                        return get(key);
                    }
                }
            注:当前应用的所有Servlet共享同一个ServletContext。
        9.5 配置应用级的参数web.xml
            用ServletContext取。
    10.0 servlet转发、实现中文文件的下载、读取
        10.1 servlet转发
        Demo1.java
            public void doGet(HttpServletRequest request,HttpServletResponse response)
                    throws ServletExcepation, IOException{
                    ServletContext sc = getServletContext();
                    RequestDispatcher rd = sc.getRequestDispatcher("/servlet/Demo2");
                    rd.forward(request,response); //转发
    
                    }
            }
        Demo2.java
            public void doGet(HttpServletRequest request,HttpServletResponse response)
                    throws ServletExcepation, IOException{
                            response.getWriter().writer("i am secent servlet")
                    }
        10.2 Servlet实现中文文件的下载
        DemoIo.java
            public void doGet(HttpServletRequest request,HttpServletResponse response)
                    throws ServletExcepation, IOException{
                            //文件在哪?以不变以万变
                            ServletContext sc = getServletContext();
                            String realPath = sc.getRealPath("/WEB-INF/classes/27.jpg");//文件存放的真是路径
                            //System.out.println(reaPath);
                            //构建文件的输入流
                            InputStream in = new FileInputStream(realPath);
    
                            //要获取 的文件名
    
                            String filename = realPath.substring(realPath.lastIndexOf(file.separator)+1);
    
                            //告知客户端以下载的方式打开:Content-Disposition=attachment;filename=27.jpg
    
    
                            response.setHeader("Content-Type","application/octet-stream");
                            response.setHeader("Content-Disposition","attachment;filename"+URLEncoder.encode(filename,"UTF-8"));//中文属于不安全字符,需要使用url编码
    
                            //用response的字节流进行输出
                            OutputStream out = response.getOutputStream();
                            int len = -1;
                            byte b[] = new byte[1024];
                            while((len = in.read(b))!=-1){
                                out.write(b,0,len);
                            }
                            in.close();
                            out.close();
                    }
                }
        10.3 读取配置文件的各种方式
            10.3.1 利用ServletContext读取: a b c ,可以读取任何位置上的资源,使用限制只能是在web应用中用。
                private void test10(HttpServletRequest request,HttpServletResponse response)
                    throws ServletExcepation,IOException{
                    String path = getServletContext().getRealPath("/a.properties");
                    //String path = getServletContext().getRealPath("/WEB-INF/classes/b.properties");
                    //String path = getServletContext().getRealPath("/WEB-INF/classes/com/demo/resource/c.properties");
    
    
                    InputStream in = new FileInputStream(path);
                    Properties props = new Properties();
                    props.load(in);
                    System.out.println(props.getProperty("hello"));
                    }
            10.3.2 利用ResourceBundle读取: b c,不能读取properties的文件。只要是java程序便可。
                private void test20(HttpServletRequest request,HttpServletResponse response)
                    throws ServletExcepation,IOException{
                    ResourceBundle rb = ResourceBundle.getBundle("b");
                    ResourceBundle rb = ResourceBundle.getBundle("com.demo.resource.c");
                    System.out.pringtln(rb.getString("hello"));
                    }
            10.3.3 类加载器读取:只能读取classes或者类路径中的任意资源(更专业),但是不适合读取特别大的资源。建议读b c  ,a也可以但是文件过大不推荐。不要让tomcat等服务器在有空格的目录中。
    
                    private void test20(HttpServletRequest request,HttpServletResponse response)
                        throws ServletExcepation,IOException{
                            ClassLoader c1 = ServletContextDemo7.class.getClassLoader();//得到类加载器
                            InputStream in = cl.getResourceAsStream("b.properties");
                            Properties props = new Properties();
                            props.load(in);
                            System.out.println(props.getProperty("hello"));
                    }
    
    
    
    
                private void test30(HttpServletRequest request,HttpServletResponse response)
                    throws ServletExcepation,IOException{
                        ClassLoader c1 = ServletContextDemo7.class.getClassLoader();//得到类加载器
                        URL url = cl.getResource("com/demo/resource/c.properties")
                        String path = url.getPath();
                        InputStream in = new FileInputStream(path);
                        Properties props = new Properties();
                        props.load(in);
                    System.out.println(props.getProperty("hello"));
                    }
    
    
    
    
    
    
  • 相关阅读:
    让xamarin的Entry绑定时,支持Nullable类型
    xamarin.forms 绑定页面里指定元素的某个属性值
    俄文环境下,字符串转数字失败
    使用devstack安装openstack
    在linux中访问macos 下的分区。
    git 多用户多仓库配置
    Macos上 ProxyChains 的坑, 需要关闭 sip
    MacOS 的预览 Preview 打开pdf 容易卡死 解决方案
    一客户端使用多个密钥对登录多个主机的解决方案.
    MacOS 10.13.6 下装xcode 流程
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6468022.html
Copyright © 2011-2022 走看看