web容器在启动的时候,它会为每个web程序都创建一个对应的servletContext对象,它代表了当前的web应用。
1、共享数据我在这个servlet中保存的数据,可以再另外一个servlet中拿到
1.新建一个module
2.HelloServlet
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//this.getInitParameter() 初始化参数
//this.getServletConfig() Servlet配置
//this.getServletContext() Servlet上下文
ServletContext context = this.getServletContext();
String username = "高壮壮";
context.setAttribute("username",username); //将一个数据保存在ServletContext中,名字为username,值为高壮壮
System.out.println("Hello");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
3.GetServlet
public class GetServlet extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
PrintWriter writer = resp.getWriter();
writer.print("名字:"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
4.web.xml
hello
com.kuang.servlet.HelloServlet
hello
/hello
getContext
com.kuang.servlet.GetServlet
getContext
/getContext
5.测试
5.1 先调用localhost:8080/s2/getContext
5.2 再调用localhost:8080/s2/hello
5.3 重启,再调用localhost:8080/s2/hello
5.4 再调用localhost:8080/s2/getContext
2、获取初始化参数
url
jdbc:mysql://localhost:3306/mybatis
gp
com.kuang.servlet.ServletDemo03
gp
/gp
public class ServletDemo03 extends HelloServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
PrintWriter writer = resp.getWriter();
writer.print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3、请求转发
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入");
// RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp"); //请求的转发路径
// requestDispatcher.forward(req,resp); //调用forward实现请求转发
context.getRequestDispatcher("/gp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
dis
com.kuang.servlet.ServletDemo04
dis
/dis
4、读取资源文件
Properties
-
在java目录下新建properties
-
在resources目录下新建properties
发现:都被打包到同一个路径下:classes 我们俗称这个路径为classpath
思路:需要一个文件流
username=root
password=123456
sd5
com.kuang.servlet.ServletDemo05
sd5
/sd5
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream inputStream = req.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(inputStream);
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 {
doGet(req, resp);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)