javaweb开发(四):servlet应用上下文与jsp
- 4大作用域对象
PageContext: 当我们访问不同的servlet时,可以获取该对象,当访问其他的servlet,即切换不同的页面时,就会销毁该对象
ServletRequest:当我们发送不同的请求时,可以获取该对象,访问其他请求时,则该对象销毁
HttpSession: 当我们打开某个网站时,只要该会话没有过期,则可以随时从该对象中获取数据
ServletContext: 当tomcat服务器启动后该对象就一直存在,且所有的servlet都可以从该作用域中获取值
-
构建一个web项目,导入依赖
-
之后查看导入的jar包
-
web.xml中添加如下
<context-param>
<param-name>url</param-name>
<param-value>xdclass.net</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>jack</param-value>
</context-param>
- 编写UserServlet: 先获取web.xml中数据,再向servletContext中添加数据
@WebServlet(name = "userServlet",urlPatterns = {"/user"})
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("<div> doGet </div>");
// 获取servletContext中的数据
ServletContext servletContext = req.getServletContext();
String url = servletContext.getInitParameter("url");
System.out.println(url);
// 向servletContext中添加数据
servletContext.setAttribute("appInfo","xdclass.net info");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("<div> doPost </div>");
}
}
- 编写TestServlet: 获取在UserServlet中向servletContext中添加的数据
@WebServlet(name = "testServlet",urlPatterns = {"/test"})
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = resp.getWriter();
printWriter.write("<div> TestServlet doGet </div>");
// 获取servletContext中的数据
ServletContext servletContext = req.getServletContext();
String appInfo = (String)servletContext.getAttribute("appInfo");
System.out.println("TestServlet appInfo="+appInfo);
}
}
- 测试先访问testServlet: servletContext中没有数据
[2022-04-12 09:27:28,239] Artifact web02:war exploded: Artifact is deployed successfully
[2022-04-12 09:27:28,239] Artifact web02:war exploded: Deploy took 502 milliseconds
TestServlet appInfo=null
- 再访问userServlet: 获取到web.xml中的数据
12-Apr-2022 21:27:37.215 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory 鎶妛eb 搴旂敤绋嬪簭閮ㄧ讲鍒扮洰褰� [C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]
12-Apr-2022 21:27:37.271 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Web搴旂敤绋嬪簭鐩綍[C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]鐨勯儴缃插凡鍦╗56]姣鍐呭畬鎴�
xdclass.net
- 再访问testServlet: 获取到在userServlet中向servletContext中添加的数据
12-Apr-2022 21:27:37.215 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory 鎶妛eb 搴旂敤绋嬪簭閮ㄧ讲鍒扮洰褰� [C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]
12-Apr-2022 21:27:37.271 淇℃伅 [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Web搴旂敤绋嬪簭鐩綍[C:\Program Files\Apache Software Foundation\Tomcat 10.0\webapps\manager]鐨勯儴缃插凡鍦╗56]姣鍐呭畬鎴�
xdclass.net
TestServlet appInfo=xdclass.net info
JSP本身就是⼀种Servlet, JSP在第⼀次被访问的时候会被编译为HttpJspPage类,是HttpServlet的⼀个⼦类