目录
ServletContext对象
web容器在启动的时候,他会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
应用:
1. 共享数据
我在这个servlet中保存数据,可以在另外一个servlet中拿到
public class servlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String userName="张三";//数据
servletContext.setAttribute("userName",userName);//将一个数据保存在ServletContext中,名字为userName,值为userName
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
public class getServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
//响应
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String userName = (String)servletContext.getAttribute("userName");
response.getWriter().write("名字:"+userName);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>com.chang.servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getServlet</servlet-name>
<servlet-class>com.chang.getServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getServlet</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
2.获取初始化参数
public class servletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String url = servletContext.getInitParameter("url");
response.getWriter().write(url);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
<!--配置一些web的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc::mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>url</servlet-name>
<servlet-class>com.chang.servletDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>url</servlet-name>
<url-pattern>/url</url-pattern>
</servlet-mapping>
3.请求转发
请求转发和重定向的区别
请求转发:
public class servletDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
/* RequestDispatcher path = servletContext.getRequestDispatcher("/url");//转发的请求路径
path.forward(request,response);//forward实现转发*/
servletContext.getRequestDispatcher("/url").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
public class servletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String url = servletContext.getInitParameter("url");
response.getWriter().write(url);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
<!--配置一些web的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc::mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>url</servlet-name>
<servlet-class>com.chang.servletDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>url</servlet-name>
<url-pattern>/url</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servletDemo2</servlet-name>
<servlet-class>com.chang.servletDemo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo2</servlet-name>
<url-pattern>/ss</url-pattern>
</servlet-mapping>
4.读取资源文件
4.1认识Properties
Properties
- 方式1:在resources目录下新建Properties
- 方式2:在java目录下新建Properties
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath;
我们正常都用方式1
方式1:
方式2:
问题:配置文件无法被导出
问题:方式2遇到配置文件无法被导出的问题
问题原因:maven由于他的约定大于配置,我们之后可能遇到我们写的配置文件无法被导出或者生效的问题
解决:在maven中添加build,更新maven
<!-- 在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
解决后:
4.2读取资源文件实现:
思路:需要一个文件流
打成war包:完整路径
/WEB-INF/classes/com/chang/db.properties
db.properties
username=root
password=123456
servlet
public class servletDemo4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/chang/db.properties");
Properties prop = new Properties();
prop.load(is);
String name = prop.getProperty("username");
String pwd = prop.getProperty("password");
response.getWriter().write(name+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
<servlet>
<servlet-name>servletDemo4</servlet-name>
<servlet-class>com.chang.servletDemo4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDemo4</servlet-name>
<url-pattern>/ss4</url-pattern>
</servlet-mapping>