ServletContext

/ java / 80浏览

ServletContext

引言:

web容器再启动的时候,它会为每一个web程序都创建一个对应的ServletContext(Servlet上下文)对象,它代表了当前整个web应用。

image-20220410233714743

1、共享数据

在一个Servlet中保存的数据,可以在另外一个Servelt中拿到。

上代码:

package top.lhhstudy.servlet;

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.PrintWriter;

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应回去的编码格式
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        ServletContext servletContext = this.getServletContext();
        String userName = (String) servletContext.getAttribute("userName");
        PrintWriter writer = resp.getWriter();
        writer.print("名字" + userName);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
package top.lhhstudy.servlet;
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 {
        ServletContext servletContext = this.getServletContext();

        String userName = "梁宏华";
        servletContext.setAttribute("userName",userName); //往servletContext对象中存userName

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
<?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>top.lhhstudy.servlet.HelloServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>

        <servlet>
            <servlet-name>getServlet</servlet-name>
            <servlet-class>top.lhhstudy.servlet.GetServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>getServlet</servlet-name>
            <url-pattern>/getServlet</url-pattern>
        </servlet-mapping>
</web-app>

2、获取初始化参数

<!--配置一些web应用初始化参数-->
        <context-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
        </context-param>
package top.lhhstudy.servlet;

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 GetInitParam extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String url = servletContext.getInitParameter("url");

        resp.getWriter().print(url);
    }
}

image-20220411100838731

image-20220411100748821

3、请求转发RequestDispatcher

package top.lhhstudy.servlet;

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 RequestDispatcherServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //请求转发到getServlet
        servletContext.getRequestDispatcher("/getServlet").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
<?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">


        <!--配置一些web应用初始化参数-->
        <context-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
        </context-param>

        <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>top.lhhstudy.servlet.HelloServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>

        <servlet>
            <servlet-name>getServlet</servlet-name>
            <servlet-class>top.lhhstudy.servlet.GetServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>getServlet</servlet-name>
            <url-pattern>/getServlet</url-pattern>
        </servlet-mapping>

        <servlet>
            <servlet-name>getInitParam</servlet-name>
            <servlet-class>top.lhhstudy.servlet.GetInitParam</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>getInitParam</servlet-name>
            <url-pattern>/getInitParam</url-pattern>
        </servlet-mapping>

        <servlet>
            <servlet-name>requestDispatcherServlet</servlet-name>
            <servlet-class>top.lhhstudy.servlet.RequestDispatcherServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>requestDispatcherServlet</servlet-name>
            <url-pattern>/requestDispatcherServlet</url-pattern>
        </servlet-mapping>

</web-app>

结果页面:

image-20220411184521709

结论:

4、读取资源文件

Properties

发现: 都被打包在同一个路劲下在:classes(类路径)

获得Properties文件

  }

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

}


  ```xml
  <servlet>
              <servlet-name>readPropertiesServlet</servlet-name>
              <servlet-class>top.lhhstudy.servlet.ReadPropertiesServlet</servlet-class>
          </servlet>

          <servlet-mapping>
              <servlet-name>readPropertiesServlet</servlet-name>
              <url-pattern>/readPropertiesServlet</url-pattern>
          </servlet-mapping>
  username=root
  password=root
站长统计代码