ServletContext
提示:以下是本篇文章正文内容,下面案例可供参考
一、共享数据
在一个Servlet中保存的数据,可以在另一个Servlet中拿到
1.设置数据
代码如下(示例):
public class contextSet extends HttpServlet {
// 重写一下doGet和doSet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
ServletContext context = this.getServletContext();//创建ServletContext对象
String name = "lili";
context.setAttribute("username",name);//设置username的值为name的值
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.读取数据
代码如下(示例):
public class contextGet extends HttpServlet {
//读取contextSet类中保存的username
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String getName = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字:"+getName);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
说明
ServletContext,一些人以为一个servlet对应一个ServletContext。其实不是,一个web应用对应一个ServletContext,所以ServletContext的作用范围是整个应用,有人就是两个类中创建的ServletContext对象指向同一个。
配置信息
<servlet>
<servlet-name>Get</servlet-name>
<servlet-class>servletContext.contextGet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Get</servlet-name>
<url-pattern>/getc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Set</servlet-name>
<servlet-class>servletContext.contextSet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Set</servlet-name>
<url-pattern>/setc</url-pattern>
</servlet-mapping>
先访问contextSet在访问contextGet即可的到contextSet中设置的值
二、其他应用
1.获取初始化参数
<!--配置一些web的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
public class GetParam extends HttpServlet {
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 {
doGet(req, resp);
}
}
2.请求转发路径
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/contextGet");//转发的请求路径
requestDispatcher.forward(req,resp);//调用forward实现请求转发
3.读取资源文件
配置文件
user = "root"
pwd = "123456"
public class getprop extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
InputStream inputStream = context.getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(inputStream);
String name = properties.getProperty("user");
String password = properties.getProperty("pwd");
resp.getWriter().print(name+" "+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
读取资源文件时需要一个文件流;
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。