HttpServletRequest:请求对象:获取请求信息
HttpServletResponse:响应对象:设置响应对象
ServletConfig对象:servlet配置对象
ServletContext对象:servlet的上下文对象
ServletConfig对象
作用:主要用于加载servlet的初始化参数,在一个web应用中可以存在多个ServletConfig对象(一个Servlet对应一个ServletConfig对象)
对象的创建和得到:
创建时机:在创建完servlet对象之后,在调用init方法之前创建
得到对象:直接从有参数的init方法中得到。
servlet的初始化参数配置
1 <servlet> 2 <servlet-name>Demo1</servlet-name> 3 <servlet-class>com.maodoer.test.Demo1</servlet-class> 4 <load-on-startup>1</load-on-startup> 5 <!-- 初始化参数,这些参数会在加载web应用的时候封装到ServletConfig对象中 --> 6 <init-param> 7 <param-name>path</param-name> 8 <param-value>c:/users/maodoer/desktop/a.txt</param-value> 9 </init-param> 10 </servlet>
1)tomcat服务器把<init-param>中的参数加载web应用的时候,封装到ServletConfig对象中
2)tomcat服务器调用init方法传入ServletConfig对象
servlet的初始化参数设置
注意:servlet的参数只能由当前的这个servlet获取
ServletConfig的api:
getInitParameter(String name);//根据参数名获取参数值
getInitParameterNames();//得到servlet初始化的所有参数
getServletContext();//得到servlet上下文对象
getServletName();//得到servlet的名称
1 public class Demo1 extends HttpServlet{ 2 /** 3 * 以下注释代码GenericServlet已经帮开发者写好了,所以不需要再写了。 4 */ 5 /*ServletConfig config=null; 6 @Override 7 public void init(ServletConfig config) throws ServletException { 8 this.config=config; 9 }*/ 10 @Override 11 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 12 /** 13 * 读取servlet的初始参数 14 */ 15 String path = this.getServletConfig().getInitParameter("path"); 16 File file=new File(path); 17 //读取内容 18 BufferedReader br=new BufferedReader(new FileReader(file)); 19 String str=null; 20 while((str=br.readLine())!=null){ 21 System.out.println(str); 22 } 23 //查询当前servlet的所有初始化参数 24 Enumeration<String> enums = this.getServletConfig().getInitParameterNames(); 25 while(enums.hasMoreElements()){ 26 String paramName = enums.nextElement(); 27 String paramValue = this.getServletConfig().getInitParameter(paramName); 28 System.out.println(paramName+"="+paramValue); 29 } 30 //得到servlet的名称 31 String servletName = this.getServletConfig().getServletName(); 32 System.out.println(servletName); 33 } 34 }
ServletContext对象:
ServletConfig对象,叫做Servlet的上下文对象。表示一个当前的web应用环境。一个web应用中只有一个ServletContext对象。
对象创建和得到:
创建时间:加载web应用时创建ServletContext对象。
得到:从ServletConfig的getServletContext方法得到。
sun公司设计:
1)创建ServletContext对象 ServletContext context=new ServletContext();
2)创建ServletConfig对象 ServletConfig config=new ServletConfig();
config.setServletContext(context);
Class ServletConfig{
ServletContext context;
public ServletContext getServletContext(){
return context;
}
}
public void init(ServletConfig config){
得到ServletConfig对象
从ServletConfig对象中得到ServletContext对象
ServletContext context=config.getServletContext();
}
ServletContext核心api:
getContextPath();得到当前web应用的路径
getInitParameter(String name);得到web应用的初始化参数
getInitParameterNames()
setAttribute(String name,Object object);和域对象有关的方法
getAttribute(String name);
removeAttribute(String name);
getRequestDispather(String path);转发(类似与重定向)
getRealPath(String path);得到web应用的资源文件
getResourceAsStream(String path);