天天看点

JavaWeb-04-ServletContext对象JavaWeb-04-ServletContext对象

JavaWeb-04-ServletContext对象

1.概述

web服务器在启动的时候,会为每个web程序都创建一个对应的ServletContext对象,

它代表了当前的web应用

2.共享数据

在一个Servlet中保存的数据在另一个Servlet中可以拿到

JavaWeb-04-ServletContext对象JavaWeb-04-ServletContext对象

HelloServlet.class

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.getInitParameter();  初始化参数
//        this.getServletConfig();  Servlet配置
//        this.getServletContext(); Servlet上下文
        ServletContext ctx = this.getServletContext();

        String username = "void";//数据
        ctx.setAttribute("username",username);//将一个数据保存在ServletContext中(键值对)


        System.out.println("Hello");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

           

GetServlet.class

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取出HelloServlet的数据
        ServletContext ctx = this.getServletContext();
        String username = (String) ctx.getAttribute("username");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字:"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }
}

           

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>com.cmy.servlet.HelloServlet</servlet-class>
<!--        <init-param>-->
<!--            <param-name></param-name>-->
<!--            <param-value></param-value>-->
<!--        </init-param>-->
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>getctx</servlet-name>
        <servlet-class>com.cmy.servlet.GetServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>getctx</servlet-name>
        <url-pattern>/getctx</url-pattern>
    </servlet-mapping>
</web-app>
           

3.获取初始化参数

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetServletInitP extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext ctx = getServletContext();
        String url = ctx.getInitParameter("url");
        resp.getWriter().println(url+"OK");
        System.out.println("hello");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
           
<servlet>
     <servlet-name>getP</servlet-name>
     <servlet-class>com.cmy.servlet.GetServletInitP</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>getP</servlet-name>
    <url-pattern>/getP</url-pattern>
</servlet-mapping>
           

4.请求转发(getRequestDispatcher)

和重定向的区别

JavaWeb-04-ServletContext对象JavaWeb-04-ServletContext对象

示例

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ServletReqDispatcher extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext ctx = getServletContext();
//        RequestDispatcher requestDispatcher = ctx.getRequestDispatcher("/getP");//转发的请求路径
//        requestDispatcher.forward(req,resp);//调用forward方法实现请求转发
        ctx.getRequestDispatcher("/getP").forward(req,resp);//简介写法
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
           
<servlet>
    <servlet-name>grdq</servlet-name>
    <servlet-class>com.cmy.servlet.ServletReqDispatcher</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>grdq</servlet-name>
    <url-pattern>/grdp</url-pattern>
</servlet-mapping>
           

5.读取资源文件

Properties

  • 在Java目录下新建properties(不合约定,需要特殊的设置,见前面文章)
  • 在resource目录下新建properties

发现:都被打包到了同一个路径下:classes,称该路径为classpath:

读取properties文件

db.properties

username=root
password=123456
           
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ServletGetRes extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext ctx = this.getServletContext();
        InputStream is = ctx.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(is);
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");
        resp.getWriter().print(username+":"+password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
           

InputStream is = ctx.getResourceAsStream("/WEB-INF/classes/db.properties");

路径的来源

JavaWeb-04-ServletContext对象JavaWeb-04-ServletContext对象