servlet 学习记录

Author : Janloong Do_O

servlet 生命周期

单实例 多线程

构造方法

只会调用一次,在第一次访问,即通过反射创建servlet对象的时候

init

只会调用一次

service

创建成功后多次调用

destory

servlet 停止的时候调用
servlet url配置

/

代表工程路径
只配/ 代表此servlet是一个默认的servlet 任何找不到的url 会匹配此

*

必须加后缀名,后缀名不能为*
代表匹配任意的url

serlvet 创建时机

启动时创建

启动顺序从2 开始  因为1在tomcat默认配置中已经占用
  <servlet>
        <servlet-name>Servlet</servlet-name>
        <servlet-class>com.janloong.webtest.Servlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
servletConfig 对象获取
  获取的方式有两种:
    1. 采用带参的init方法
    2. 采用servlet实例拿取(不要写带参的init方法)

    web.xml 配置
     <servlet>
        <servlet-name>spring3mvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>

    //拿到servletConfi对象
        ServletConfig sc = this.getServletConfig() ;

        //拿取配置的单个信息
        //String name = sc.getInitParameter("name") ;
         //System.out.println(name);

        //拿取配置的多个信息
        Enumeration<String> enu = sc.getInitParameterNames() ;
        while(enu.hasMoreElements()){
            String name = enu.nextElement() ;
            System.out.println(name + ":" + sc.getInitParameter(name));
        }
servletContext 对象获取
1. servletconfig.getServletContext

2. this.getServletContext

3. request.getSession().getServletContext()

一个域对象,底层有一个map集合,用来存储键值对

应用场景

全局对象中存取数据

获取全局配置参数

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath:spring-*.xml,
      classpath*:/spring/spring-*.xml
    </param-value>
  </context-param>

请求转发

请求转发时有一个请求包含功能

获取资源文件

 * 获取资源文件有三种方式:
 * 1.采用 ServletContext对象获取
 * 2.采用ResourceBundle类来获取
 * 3.采用类加载器获取
 *
 *             第一种方式:优点: 任意文件,任意路径
 *                         缺点: 必须有web环境
 *             第二种方式: 优点:简单方便
 *                          缺点: 1.只能拿取properties文件 2. 只能拿取非web环境下的资源
 *             第三种方式: 优点: 任意文件,任意路径
 *                         缺点: 编写稍显麻烦
 *
1. // 拿到全局对象
        ServletContext sc = this.getServletContext();

        // 获取p1.properties文件的路径
        String path = sc.getRealPath("/WEB-INF/classes/p1.properties");
        System.out.println(path);
        // 创建一个Properties对象
        Properties pro = new Properties();
        // 加载文件
        try {
            pro.load(new FileReader(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 读取k的值
        System.out.println(pro.get("k"));

2.// 拿取ResourceBundle对象(专门用来获取properties文件的信息)
        ResourceBundle rb = ResourceBundle.getBundle("p1");
        // 拿取文件中的内容太
        System.out.println(rb.getString("k"));

3. // 获取类加载器的方式
        /*
         * 1. 通过类名 ServletContext7.class.getClassLoader() 2. 通过对象
         * this.getClass().getClassLoader() 3. Class.forName()
         * 获取Class.forName("ServletContext7").getClassLoader()
         */
        InputStream in = this.getClass().getClassLoader()
                .getResourceAsStream("p1.properties");

        // 创建Properties对象
        Properties pro = new Properties();
        try {
            pro.load(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // 拿取文件的数据
        System.out.println(pro.getProperty("k"));
域对象

web的四个域对象

page(jsp有效) request(一次请求) session(一次会话) application(当前web应用)+

page域指的是pageContext
request域指的是request HttpServletRequest
session 域指的是 session HTTPSession
application 域指的是 application ServletContext

之所以他们是域对象,原因就是他们都内置了map集合,都有setAttribute getAttribute方法。+

他们都有自己固定的生命周期和作用域。这四个对象的生命周期(生命周期就是值对象的创建到销毁的期间)

中文乱码
//解决乱码问题
//第一种办法:让用户在浏览器中选择解码(不靠谱)
//第二种方式:通知浏览器采用某种编码进行解码
//response.setHeader("Content-Type", "text/html;charset=UTF-8") ;
//第三种方式:输出一个字符串
//response.getOutputStream().write("<meta http-equiv='content-type' content='text/html; charset=UTF-8'>".getBytes()) ;
//response.getOutputStream().write(s.getBytes("utf-8")) ;
//第四种方式 让服务器的编码用一种编码,通知浏览器的解码
//response.setCharacterEncoding("UTF-8") ;
//response.setHeader("Content-Type", "text/html;charset=UTF-8") ;
//
//response.getOutputStream().write(s.getBytes("utf-8")) ;

//第五种方法(推荐)
response.setContentType("text/html;charset=utf-8") ;
 //此句代码做了两件事情:
 1. 设定服务器将数据编码时用的码表
 2. 通知浏览器解码用的码表

response 在使用IO流的时候,默认的

字节流是以 GBK编码返回的
字符流是以 ISO-8859-1 编码返回的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值