web容器启动的时候,他会为每一个web容器都创建一个对应的ServletContext,它待变了当前的Web应用。
-
共享数据
我在这个Servlet中保存的数据,可以在另外一个中读取到。
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// this.getInitParameter() 初始化参数
// this.getServletConfig() servlet的配置
// this.getServletContext() servlet的上下文
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
String username = "西风";
ServletContext context = this.getServletContext();
context.setAttribute("username",username); //将一个数据保存在ServletContext中,名字为username,只为username
System.out.println("hello");
}
}
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 context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.getWriter().print("名字"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>org.westos.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getc</servlet-name>
<servlet-class>org.westos.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getc</servlet-name>
<url-pattern>/getc</url-pattern>
</servlet-mapping>
参数获取getInitParameter
配置JDBC数据库
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet-mapping>
<servlet-name>geturl</servlet-name>
<url-pattern>/geturl</url-pattern>
</servlet-mapping>
获取web的配置信息
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
ServletContext conext = this.getServletContext();
String url = conext.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
请求转发
代码示例
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了servletDemo04");
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/geturl");//转发请求路径
requestDispatcher.forward(req,resp); //调用forword实现请求转发
}
}
读取文件
properties
- 在java目录下新建
- 在Resources目录下新建properties
发现都被打包到同一个路径下:这个路劲下叫做Classes 我们俗称这个路径为classpath:
思路:需要一个文件流
访问测试结果
username=root
password=Asimov
#classpath;
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
InputStream rs = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(rs);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
resp.getWriter().print(username+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
}
<servlet>
<servlet-name>sd5</servlet-name>
<servlet-class>org.westos.ServletDemo05</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sd5</servlet-name>
<url-pattern>/sd5</url-pattern>
</servlet-mapping>