<!--动态程序Servlet的配置-->
<!--告诉Tomcat容器,由一个Servlet程序-->
<servlet>
<!--Servlet的内部名称-->
<servlet-name>Index</servlet-name>
<!--Servlet的类名,全限定名-->
<servlet-class>com.xunpu.IndexServlet</servlet-class>
</servlet>
<!--Tomcat将通过反射实例化一个Servlet对象。-->
<!--Servlet的映射关系,告诉Tomcat容器,当用户请求一个地址的时候,使用该Servlet处理。-->
<servlet-mapping>
<servlet-name>Index</servlet-name>
<url-pattern>/IndexServlet</url-pattern>
</servlet-mapping>
被final修饰的变量:1)立即初始化 2)构造方法中初始化 3)构造块中初始化
1.ServletContext对象(重要)
介绍:
ServletContext表示Servlet应用程序。每个web应用程序只有一个上下文,在将一个应用程序同时部署到多个容器的分布式环境中,每台Java虚拟机上的Web应用都会有一个ServletContext对象。
有了ServletContext,就可以共享从应用程序中的所有资料处访问到的信息,并且可以动态注册Web对象。共享信息可以将对象保存在Servlet对象的一个内部Map中,保存在ServletContext中的对象称为属性。
作用:
1)获取上下文路径
2)获取全局参数
3)可以请求转发(跳转页面)
4)作为域对象使用
5)获取真实路径
1.1 获取上下文路径
通过获取ServletContext对象,调用ServletContext对象的getContextPath来获取上下文路径.
首先在IDEA中修改路径如图:
package com.xunpu.e_context;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* ServletContext对象的作用1:获取上下文路径
*/
/**
* GenericServlet的源码:
* public ServletContext getServletContext() {
* ServletConfig sc = this.getServletConfig();
* if (sc == null) {
* throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
* } else {
* return sc.getServletContext();
* }
* }
*/
public class ContextDemo1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取ServletContext对象
//方式一:
// ServletConfig config=this.getServletConfig();
// ServletContext servletContext=config.getServletContext();
//方式二:
ServletContext context=this.getServletContext();
//getContextPath():获取上下文路径
String contextPath = context.getContextPath();
System.out.println("contextPath="+contextPath);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
访问contextDemo1,得到结果:
1.2 获取全局参数
一个项目(web application)有且只有一个ServletContext对象。主要使用ServletContext对象的getInitParameter("参数名")获取。
在web.xml中配置全局参数
<!--配置全局参数,全局参数包含在ServletContext对象中-->
<context-param>
<param-name>AAA</param-name>
<param-value>AAA_value</param-value>
</context-param>
<context-param>
<param-name>BBB</param-name>
<param-value>BBB_value</param-value>
</context-param>
使用ServlerContext对象获取全局数据:
package com.xunpu.e_context;
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 ContextDemo2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context=this.getServletContext();
//获取上下文的初始化参数
String value1=context.getInitParameter("AAA");
System.out.println("AAA="+value1);
//还可以采用getInitParameternames()获取--Enumeration迭代器获取
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
在浏览器中输入http://localhost:8080/contextDemo2,得到AAA的值:
1.3 获取真实路径(绝对路径)
两种方式:1)使用ServletContext对象的getRealPath("文件名")来获取。2)通过类对象的getClassLoader().getResource("文件名").getPath()。
package com.xunpu.e_context;
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 PathDemo extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取全局管理者对象
ServletContext servletContext = this.getServletContext();
//public String getRealPath(String path):获取真实路径
servletContext.getRealPath("aaa.txt");
//如果不使用servletContext,使用
String path = PathDemo.class.getClassLoader().getResource("aaa.txt").getPath();
System.out.println(path);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
1.4 作为域对象使用
什么是域对象?
共享数据,在不同jsp/servlet之间保存数据,获取数据。域对象仅在一个jsp页面中有效。在一次请求转发过程中有效。
域对象的分类:
从小到大依次是:
PageContext--->pafeScope: 只在一个页面中有效
HttpServletRequest对象:就是一个域对象requestScope。
HttpSession -->sessionScope
ServletContext -->applicationScope
重定向:两次请求的request对象不同,携带的信息不同。
重定向中的方法:xxxAttribute(),eg:request.setAttribute("name","lucy"); 可以通过request.getAttribute("name");获取值
重定向的整个过程,有两个request对象,因此不适合业务之间的数据传递。因此要使用ServletContext对象,可以请求转发。
域对象的简单介绍:
代码练习:主要运用servletContext对象的setAttribute(name,value),和getAttribute(name).
先在Servlet.pom文件中导入依赖:
<!--添加依赖:servlet jsp-->
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
package com.xunpu.e_context.scope;
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.util.ArrayList;
import java.util.List;
/**
* ServletContext作用:
* 作为域对象去使用,保存数据,获取数据(共享数据)
*/
public class ScopeDemo1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
response.setContentType("text/html;charset=utf-8");
//直接可以获取ServletContext对象
ServletContext servletContext = this.getServletContext();
//public void setAttribute(String name,Object object);//给域对象中添加数据,同时设置属性名称
servletContext.setAttribute("name","xunpu");
//创建一个List集合对象
List list=new ArrayList();
list.add("hello");
list.add("world");
list.add("java web");
//将list集合存储到域对象中
servletContext.setAttribute("list",list);
//提示
response.getWriter().write("保存数据成功了。。。");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
package com.xunpu.e_context.scope;
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.util.List;
/**
* 从servletContext对象中获取数据
*/
public class ScopeDemo2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置编码
response.setContentType("text/html;charset=utf-8");
//获取全局管理者对象
ServletContext servletContext = this.getServletContext();
//public Object getAttribute();从域中获取数据,通过域中存储的属性名称
String name = (String) servletContext.getAttribute("name");
System.out.println("name="+name);
List list= (List) servletContext.getAttribute("list");
System.out.println(list);
response.getWriter().write("获取到了数据。。。");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
结果:
1.5 请求转发
使用servletContext对象的getRequestDispatcher("/hello.html").forward(request,response)方法,跳转hello.html页面,结果地址栏不会发生变化。
package com.xunpu.e_context;
import javax.servlet.RequestDispatcher;
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 PathDemo extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//请求转发技术 public RequestDispatcher getRequestDispatcher(String path)
//获取全局管理者对象
ServletContext servletContext = this.getServletContext();
RequestDispatcher rd = servletContext.getRequestDispatcher("/hello.html");
//public void forward(ServletRequest request,ServletResponse response)
rd.forward(request,response);
//特点:整个过程,request对象只有一个。
//请求转发的简写方式
request.getRequestDispatcher("/hello.html").forward(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
在浏览器中输出localhost:8080/pathDemo,结果为: