javaWeb中servlet开发(3)——Servlet生命周期

生命周期:是一个程序的存在周期,servlet由于是受容器的管理,所以容器来决定其生命周期

1、servlet生命周期

 

2、servlet生命周期对应的方法

 3、servlet生命周期代码

public class LifeCycleServlet extends HttpServlet{
    public void init() throws ServletException{
        System.out.println("** 1、Servlet初始化 --> init()") ;
    }
    public void doGet(HttpServletRequest req,HttpServletResponse resp)
              throws ServletException,IOException{
        System.out.println("** 2、Servlet服务 --> doGet()、doPost()") ;
    }
    public void doPost(HttpServletRequest req,HttpServletResponse resp)
              throws ServletException,IOException{
        this.doGet(req,resp) ;
    }
    public void destroy(){
        System.out.println("** 3、Servlet销毁 --> destory()") ;
        try{
            Thread.sleep(3000) ;
        }catch(Exception e){}
    }/*
    public void service(ServletRequest req,
                             ServletResponse res)
                      throws ServletException,
                             IOException{
        System.out.println("************ 服务 **************") ;
    }
    */
}

一个基本生命周期编译后,就可以在web-INF/web.xml中进行映射配置。下面直接在根目录下配置

<servlet>
        <servlet-name>life</servlet-name>
        <servlet-class>com.alice.servlet.LifeCycleServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>life</servlet-name>
        <url-pattern>/LifeServlet</url-pattern>

访问路径为:

 4、取得初始化配置信息

public class InitParamServlet extends HttpServlet {
    private String initParam = null ;    // 用于保存初始化参数
    public void init() throws ServletException{
        System.out.println("*****************") ;
    }

    public void init(ServletConfig config) throws ServletException{
        System.out.println("#######################") ;
        this.initParam = config.getInitParameter("ref") ;    // 接收的初始化参数名称暂时为ref
    }

    public void doGet(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     IOException{
        System.out.println("** 初始化参数:" + this.initParam) ;
    }
    public void doPost(HttpServletRequest req,
                     HttpServletResponse resp)
              throws ServletException,
                     IOException{
        this.doGet(req,resp) ;
    
    }



}

之后,要做web.xml中配置

<servlet>
        <servlet-name>initparam</servlet-name>
        <servlet-class>com.alice.servlet.InitParamServlet</servlet-class>
        <init-param>
            <param-name>ref</param-name>
            <param-value>www.baidu.cn</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>initparam</servlet-name>
        <url-pattern>/InitParamServlet</url-pattern>
    </servlet-mapping>

 

如果一个servlet中覆写了两个init()方法,只有在web.xml中配置了参数的初始化方法才可以起作用。

一般来说,取得初始化参数在一些系统架构中经常使用的。

5、取得其他内置对象

5.1 取session对象

servlet本身提供的只有request和response对象,要想取得session对象,则只能依靠request对象,因为session本身属于http协议范畴,而且在每次发生请求时,服务器都会自动为客户端设置一个cookie,session中要使用cookie机制,cookie又只能通过request取得,那么自然地session也只能通过request取得。

public class HttpSessionDemoServlet extends HttpServlet {
	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		HttpSession ses = req.getSession() ;
		System.out.println("SESSION ID --> " + ses.getId()) ;
		ses.setAttribute("username","李李") ;	 // 设置session属性
		System.out.println("username属性内容:" + ses.getAttribute("username")) ;
	}
	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		this.doGet(req,resp) ;
	}
}

  

之后进行web.xml进行配置

<servlet>
		<servlet-name>sessiondemo</servlet-name>
		<servlet-class>
			com.alice.servlet.HttpSessionDemoServlet
		</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>sessiondemo</servlet-name>
		<url-pattern>/HttpSessionDemoServlet</url-pattern>
	</servlet-mapping>

 5.2 取application对象

public class ServletContextDemoServlet extends HttpServlet {
	public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		ServletContext app = super.getServletContext() ;
		System.out.println("真实路径:" + app.getRealPath("/")) ;
	}
	public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		this.doGet(req,resp) ;
	}
}

  

<servlet>
        <servlet-name>applicationdemo</servlet-name>
        <servlet-class>
            com.alice.servlet.ServletContextDemoServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>applicationdemo</servlet-name>
        <url-pattern>/ServletContextDemoServlet</url-pattern>
    </servlet-mapping>

 

posted on 2016-11-25 22:27  小企鹅ITgo  阅读(1194)  评论(0编辑  收藏  举报