Web容器在启动的时候,它会为每个Web程序都创建一个对应的ServletContext对象,它代表了当前的Web应用。
二、作用共享数据:在这个Servlet中保存的数据,可以在另外一个Servlet中拿到。
1.步骤一:编写Store类实现数据的存放
package com.massimo.charpter11;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Get extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = this.getServletContext();
//取出数据
String userName = (String)context.getAttribute("userName");
System.out.println(userName);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.步骤二:编写Get类取出Store类存放的数据
package com.massimo.charpter11;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Get extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = this.getServletContext();
//取出数据
String userName = (String)context.getAttribute("userName");
System.out.println(userName);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.步骤三:在web.xml中注册Servlet
4.步骤四:配置Tomcat,运行程序
1.步骤一:在web.xml中配置初始化参数
url localhost:8080/massimo
2.步骤二:编写GetInitParameter类获取初始化参数
package com.massimo.charpter11;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class GetInitParameter extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = this.getServletContext();
//获取初始化参数
String url = context.getInitParameter("url");
PrintWriter writer = resp.getWriter();
writer.println(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.步骤三:注册Servlet,运行程序
1.步骤一:编写RequestForwarding类,实现转发
package com.massimo.charpter11;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RequestForwarding extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("进入了RequestForwarding");
ServletContext context = this.getServletContext();
// /page是转发的路径,然后调用forward实现请求转发
context.getRequestDispatcher("/page").forward(req , resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.步骤二:编写Page类,作为转发的目的地
package com.massimo.charpter11;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class Page extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.println("转发成功!");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.步骤三:注册Servlet
RequestForwarding com.massimo.charpter11.RequestForwarding RequestForwarding /forward Page com.massimo.charpter11.Page Page /page
4.步骤四:运行程序
1.步骤一:建立properties文件,要求不能超出main目录,应为代码就在main目录下
2.步骤二:创建GetPropertiesFile类读取资源文件
package com.massimo.charpter11;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class GetPropertiesFile extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取ServletContext对象
ServletContext context = this.getServletContext();
//通过context获取输入流对象
InputStream in = context.getResourceAsStream("/WEB-INF/classes/bb.properties");
//创建Properties对象
Properties prop = new Properties();
//通过prop对象加载输入流
prop.load(in);
//获取Properties文件数据
String userName = prop.getProperty("userName");
String password = prop.getProperty("password");
resp.getWriter().println(userName + ":" + password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注意:getResourceAsStream()的参数是文件的相对路径,具体路径参照下图。
可能出现的问题: 项目src下的配置文件没有同步至target。
解决方法:在文件都标明的情况下,在pom.xml中添加如下标签。
src/main/java ***.properties ***.yml ***.xml ***.jar
3.步骤三:注册Servlet,运行程序



