ServletContext

ServletContext

字面意思是Servlet上下文的意思

但我们可以通过this.getServletContext获取这个接口

并且可以向这个接口存储一些值,像这样:

public class HelloServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        //this.getServletContext();  Servlet上下文
        ServletContext servletContext = this.getServletContext();
        String username = "王木木";//数据
        servletContext.setAttribute("username",username);//用一个键值对把数据存储到ServletContext()里面

    }

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

然后,在从另一个Servlet类中把他响应到:

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        ServletContext servletContext = this.getServletContext();
        String username = (String) servletContext.getAttribute("username");

        resp.getWriter().println(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:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://locathost:3306/mybatis</param-value>
    </context-param>
  <!--注册Servlet-->
      <servlet>
          <!--给类命名-->
        <servlet-name>hello</servlet-name>
          <!--类的路径-->
        <servlet-class>cn.lin.servlet.HelloServlet</servlet-class>
      </servlet>
    <!--Servlet访问路径-->
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <!--注册Servlet-->
    <servlet>
        <servlet-name>gethello</servlet-name>
        <servlet-class>cn.lin.servlet.GetServlet</servlet-class>
    </servlet>
  <!--Servlet访问路径-->
        <servlet-mapping>
            <servlet-name>gethello</servlet-name>
            <url-pattern>/gethello</url-pattern>
        </servlet-mapping>
</web-app>

然后就可以运行了,但是会出现这样一个情况:

需要我们先访问hello,在访问gethello:

 

 他的原理,我简单的画一张图:

 通过setAttribute赋值键值对

通过getAttribute取值/响应

上面的代码都有写。

context-param

web-app配置context-param初始值

public class ServletDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        String url = context.getInitParameter("url");
        resp.getWriter().print(url);
    }

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

通过getInitParameter获取命名:

    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://locathost:3306/mybatis</param-value>
    </context-param>
    <servlet>
        <servlet-name>gz</servlet-name>
        <servlet-class>cn.lin.servlet.ServletDemo1
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>gz</servlet-name>
        <url-pattern>/gz</url-pattern>
    </servlet-mapping>

 

forward实现请求转发

通过getReauestDispatcher获取转发的请求路径

public class ServletDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        System.out.println("进入了ServletDemo2"
        );
        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gz");//转发的请求路径
        requestDispatcher.forward(req,resp);//实现forward实现请求转发
    }

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

    <servlet>
        <servlet-name>sd2</servlet-name>
        <servlet-class>cn.lin.servlet.ServletDemo2
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sd2</servlet-name>
        <url-pattern>/sd2</url-pattern>
    </servlet-mapping>

这样就可用通过访问/sd2,从而访问到/gz

 

获取db.properties

public class ServletDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        InputStream resourceAsStream = servletContext.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String name = properties.getProperty("username");
        String pwd = properties.getProperty("password");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print(name+":"+pwd);
    }

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

 

  <servlet>
        <servlet-name>sd3</servlet-name>
        <servlet-class>cn.lin.servlet.ServletDemo3</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sd3</servlet-name>
        <url-pattern>/sd3</url-pattern>
    </servlet-mapping>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值