配色: 字号:
Servlet类详解
2016-12-05 | 阅:  转:  |  分享 
  
Servlet类详解



关联:

从我可以拿到你

想要拿到servletConfig对象只要通过Servlet的getServletConfig()就可以拿到了

在ServletConfig中提供了getServeltContext()方法,返回的是一个ServeltContext对象,也是通过方法拿到了ServeltContext对象,所以ServletConfig和ServeltContext也是关联的关系

依赖:依赖的那个为被依赖的参数



ServletConfig

1、作用:

就是拿取servlet的相关配置.也就是拿取web.xml里面的配置信息(这个配置信息都是一样的,所以无论哪个方法得到都是一样)

创建的时机:当在创建Servlet对象的时候,服务器已经帮我们创建了这个对象,并作为参数传递进来了

生命:作为init方法的形参,所以离开了该方法,就死亡了

延长它的生命:把它作为一个全局变量



2、获取ServeltConfig对象的方式

(1).采用带参的init方法

(2).采用servlet实例拿取(不要写带参的init方法)(推荐)

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;



importjavax.servlet.ServletConfig;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

//演示如何获取servletConfig对象

/

获取的方式有两种:

1.采用带参的init方法

2.采用servlet实例拿取(不要写带参的init方法)

@authorAdministrator



/

publicclassServletConfig1extendsHttpServlet{



ServletConfigconfig;



@Override

publicvoidinit(ServletConfigconfig)throwsServletException{

super.init(config);

this.config=config;

}



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

ServletConfigsc=this.getServletConfig();

//System.out.println(config==sc);

System.out.println(sc);

System.out.println(config==sc);

}





publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}

在没有写带参的init方法的时候,默认调用父类的带参的init(),而在父类的init()中,已经帮我门把config给实例化了

如果我们自己去写带参的init方法的话,就不会去调用父类的带参的init方法了,就不会实例化,就会是null





3、拿取servlet的相关配置信息

在xml中使用配置信息,通过servletConfig对象获取这些配置信息

xml:(注意:一个只能配置一个键值对)

[html]viewplaincopy在CODE上查看代码片派生到我的代码片



ServletConfig2

com.example.servletconfig.ServletConfig2



name

张无忌





age

20









[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;

importjava.util.Enumeration;



importjavax.servlet.ServletConfig;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

//演示servletConfig对象的应用

publicclassServletConfig2extendsHttpServlet{



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{



//拿到servletConfi对象

ServletConfigsc=this.getServletConfig();



//拿取配置的单个信息

//Stringname=sc.getInitParameter("name");

//System.out.println(name);



//拿取配置的多个信息

Enumerationenu=sc.getInitParameterNames();

while(enu.hasMoreElements()){

Stringname=enu.nextElement();

System.out.println(name+":"+sc.getInitParameter(name));

}



}



publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}





servletContext

servlet上下文,一个全局对象,即工程对象,代表了一个应用

(就是建的项目,一个工程就代表一个servletContext,每个工程的servletContext对象是不一样的)

(1).生命周期很长:

产生:当服务器启动,加载这个应用的时候,就创建了servletContext这个对象;

灭亡:当服务器关闭时或停止应用时

(2).每个web应用都有一个唯一的servletContext对象.

(3).在每个应用加载的时候,服务器就会创建servletContext对象。

(4).ServletContext对象是一个域对象(领域)



1、获取sevletContext对象的方式:

1.采用servletConfig对象获取

2.采用servlet实例对象获取(推荐)

3.采用request对象获取

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;



importjavax.servlet.ServletConfig;

importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

//演示获取servletContext对象

/

有三种方式获取servletContext对象

1.采用servletConfig对象获取

2.采用servlet实例对象获取

3.采用request对象获取

@authorAdministrator



/

publicclassServletContext1extendsHttpServlet{



ServletContextsc;



@Override

publicvoidinit(ServletConfigconfig)throwsServletException{

super.init(config);

sc=config.getServletContext();

}



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//第二种方式

ServletContextsc1=this.getServletContext();

System.out.println(sc);

System.out.println(sc1==sc);

//第三种方式

ServletContextsc2=request.getSession().getServletContext();

System.out.println(sc2==sc);

}



publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}



2.常用方法

存储一个键值对;set:存进去;get:取出来





3.应用

1.作为域对象来使用:传递数据

域对象:在底层有一个map集合



存:

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;



importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;



//演示servletContext对象作为域对象使用

publicclassServletContext2extendsHttpServlet{



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{



//获取全局对象

ServletContextsc=this.getServletContext();



//存储数据

sc.setAttribute("name","张三丰");

System.out.println("数据存储完毕");

}



publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}

取:

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;



importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

//从servletContext对象中拿取数据

publicclassServletContext3extendsHttpServlet{



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{



//拿取全局对象

ServletContextsc=this.getServletContext();



//从sc中拿取数据

Stringname=(String)sc.getAttribute("name");



System.out.println(name);



}



publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}



2.获取全局配置参数

配置全局参数:在外配置,位置任意

使用标签

[html]viewplaincopy在CODE上查看代码片派生到我的代码片



name

东西方不败





sex

人妖





[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;

importjava.util.Enumeration;



importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

//获取全局配置参数

publicclassServletContext4extendsHttpServlet{



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

//拿到全局对象

ServletContextsc=this.getServletContext();

//获取单个配置参数(获取姓名)

//Stringname=sc.getInitParameter("name");

//System.out.println(name);



//拿取多个配置参数的值

Enumerationenu=sc.getInitParameterNames();

while(enu.hasMoreElements()){

Stringname=enu.nextElement();

System.out.println(name+":"+sc.getInitParameter(name));

}



}



publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}



3.请求转发

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;



importjavax.servlet.RequestDispatcher;

importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;



//演示全局对象的请求转发

publicclassServletContext5extendsHttpServlet{



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{



//拿到全局对象

ServletContextsc=this.getServletContext();



request.setAttribute("name","乔峰");



//拿到请求转发器

RequestDispatcherrd=sc.getRequestDispatcher("/servlet/ServletContext6");

//转发过去

rd.forward(request,response);



}



publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}

转发到的页面:

[java]viewplaincopy在CODE上查看代码片派生到我的代码片

importjava.io.IOException;



importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;



publicclassServletContext6extendsHttpServlet{



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{



System.out.println("你终于过来了");

Stringname=(String)request.getAttribute("name");

System.out.println("转发过来的数据:"+name);

}



publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

doGet(request,response);

}



}



4.获取资源文件

获取资源文件有三种方式:

1.采用ServletContext对象获取

优点:任意文件,任意路径

缺点:必须有web环境

2.采用ResourceBundle类来获取

优点:简单方便

缺点:

1.只能拿取properties文件

2.只能拿取非web环境下的资源

3.采用类加载器获取

优点:任意文件,任意路径

缺点:编写稍显麻烦



ResourceBundle类:

该类(抽象类)专门用来加载资源,还可以处理一些国际化的东西







类加载器:

一个java文件,编写好之后是源码,后缀名是.java,要将这个源码首先采用编译命令javac把其编译为.class文件,该.class文件位于硬盘上,在运行时,需要把.class文件加载到虚拟机里运行,就用类加载器来加载,类加载器的主要目的就是将字节码文件加载到内存里,然后运行字节码文件



获取类加载器的方式

1.通过类名:

ServletContext7.class.getClassLoader()

2.通过对象:

this.getClass().getClassLoader()

3.Class.forName()

Class.forName("ServletContext7").getClassLoader()



[java]viewplaincopy在CODE上查看代码片派生到我的代码片

packagecom.heima.four;



importjava.io.FileReader;

importjava.io.IOException;

importjava.io.InputStream;

importjava.net.URL;

importjava.util.Properties;

importjava.util.ResourceBundle;



importjavax.servlet.ServletContext;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;



//演示获取资源文件

/

获取资源文件有三种方式:

1.采用ServletContext对象获取

2.采用ResourceBundle类来获取

3.采用类加载器获取



第一种方式:优点:任意文件,任意路径

缺点:必须有web环境

第二种方式:优点:简单方便

缺点:1.只能拿取properties文件2.只能拿取非web环境下的资源

第三种方式:优点:任意文件,任意路径

缺点:编写稍显麻烦





@authorAdministrator



/

publicclassServletContext7extendsHttpServlet{



publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{



//test11();

//test12();

//test13();

//test22();

//test31();

//test32();

//test33();

//test34();



}



//采用ServletContext对象获取p1资源文件的内容

publicvoidtest11(){

//拿到全局对象

ServletContextsc=this.getServletContext();



//获取p1.properties文件的路径

Stringpath=sc.getRealPath("/WEB-INF/classes/p1.properties");

System.out.println(path);

//创建一个Properties对象

Propertiespro=newProperties();

//加载文件

try{

pro.load(newFileReader(path));

}catch(Exceptione){

e.printStackTrace();

}

//读取k的值

System.out.println(pro.get("k"));

}



//采用ServletContext对象获取p2资源文件的内容

publicvoidtest12(){

//拿到全局对象

ServletContextsc=this.getServletContext();



//获取p1.properties文件的路径

Stringpath=sc

.getRealPath("/WEB-INF/classes/com/heima/four/p2.properties");

System.out.println(path);

//创建一个Properties对象

Propertiespro=newProperties();

//加载文件

try{

pro.load(newFileReader(path));

}catch(Excepwww.sm136.comtione){

e.printStackTrace();

}

//读取k的值

System.out.println(pro.get("k"));

}



//采用ServletContext对象获取p3资源文件的内容

publicvoidtest13(){

//拿到全局对象

ServletContextsc=this.getServletContext();



//获取p1.properties文件的路径

Stringpath=sc.getRealPath("/p3.properties");

System.out.println(path);

//创建一个Properties对象

Propertiespro=newProperties();

//加载文件

try{

pro.load(newFileReader(path));

}catch(Exceptione){

e.printStackTrace();

}

//读取k的值

System.out.println(pro.get("k"));

}



//采用resourceBunble拿取资源文件:获取p1资源文件的内容默认路径是src,对用到web环境就是classes目录

publicvoidtest21(){

//拿取ResourceBundle对象(专门用来获取properties文件的信息,所以不用加后缀名)

ResourceBundlerb=ResourceBundle.getBundle("p1");//p1:路径

//拿取文件中的内容太

System.out.println(rb.getString("k"));

}



//采用resourceBunble拿取资源文件:获取p2资源文件的内容

publicvoidtest22(){

//拿取ResourceBundle对象(专门用来获取properties文件的信息)

ResourceBundlerb=ResourceBundle.getBundle("com.heima.four.p2");

//拿取文件中的内容太

System.out.println(rb.getString("k"));

}



//采用类加载器拿取资源文件:获取p1资源文件的内容:默认路径是src,对用到web环境就是classes目录

publicvoidtest31(){

//获取类加载器的方式

/

1.通过类名ServletContext7.class.getClassLoader()

2.通过对象

this.getClass().getClassLoader()

3.Class.forwww.shanxiwang.netName()

获取Class.forName("ServletContext7").getClassLoader()

/

InputStreamin=this.getClass().getClassLoader()

.getResourceAsStream("p1.properties");



//创建Properties对象

Propertiespro=newProperties();

try{

pro.load(in);

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}



//拿取文件的数据

System.out.println(pro.getProperty("k"));



}



//采用类加载器拿取资源文件:获取p2资源文件的内容:默认路径是src,对用到web环境就是classes目录

publicvoidtest32(){



InputStreamin=this.getClass().getClassLoader()

.getResourceAsStream("com/heima/four/p2.properties");



//创建Properties对象

Propertiespro=newProperties();

try{

pro.load(in);

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}



//拿取文件的数据

System.out.println(pro.getProperty("k"));



}



//采用类加载器拿取资源文件:获取p3资源文件的内容:默认路径是src,对用到web环境就是classes目录

publicvoidtest33(){



InputStreamin=this.getClass().getClassLoader()

.getResourceAsStream("../../p3.properties");



//创建Properties对象

Propertiespro=newProperties();

try{

pro.load(in);

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}



//拿取文件的数据

System.out.println(pro.getProperty("k"));



}



//采用类加载器拿取资源文件:获取p3资源文件的内容:默认路径是src,对用到web环境就是classes目录

publicvoidtest34(){

//获取类加载器的方式

/

1.通过类名ServletContext7.class.getClassLoader()2.通过对象

this.getClass().getClassLoader()3.Class.forName()

获取Class.forName("ServletContext7").getClassLoader()

/

URLurl=this.getClass().getClassLoader().getResource("p1.properties");



Stringpath=url.getPath();





//创建Properties对象

Propertiespro=newProperties();

try{

pro.load(newFileReader(path));

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}



//拿取文件的数据

System.out.println(pro.getProperty("k"));



}

}



献花(0)
+1
(本文系网络学习天...首藏)