1、ServletContext
web容器在启动的时候,它会为每个web程序都创立一个对应的ServletContext对象,它代表了以后web利用;
1、 共享数据
我在这个Servlet中保留的数据,能够在另一个servlet中拿到
例子:给Servlet搁置数据
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
//this.getInitParameter();//初始化参数
//this.getServletConfig();//Servlet配置
//this.getServletContext();//Servlet上下文
ServletContext context = this.getServletContext();
String username = "rygar";
context.setAttribute("username",username);
}
@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 {
ServletContext context = this.getServletContext();
Object username = context.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 {
super.doPost(req, resp);
}
}
web.xml配置
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.jialidun.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<servlet-class>hello</servlet-class>
</servlet-mapping>
<servlet>
<servlet-name>getc</servlet-name>
<servlet-class>com.jialidun.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getc</servlet-name>
<servlet-class>getc</servlet-class>
</servlet-mapping>
2、获取初始化参数
<!--配置一些web利用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
<context-param>
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
3、申请转发
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的申请门路
requestDispatcher.forward(req,resp);//调用forword实现申请转发
}
4、读取资源文件
Properties
- 在java目录下新建properties
-
在resources目录下新建properties
发现:都被打包到了同一个门路下:classes,咱们俗称这个门路为classpath
思路:须要一个文件流username=root123 password=123456
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");// /代表以后web我的项目 Properties prop = new Properties(); prop.load(is); String username = prop.getProperty("username"); String password = prop.getProperty("password"); resp.getWriter().print(username+":"+password); }
拜访测试后果
发表回复