Servlet life cycle

init()

    init方法是初始化参数服务的。    只要servlet在web.xml文件中配置了,    那么一定会执行init方法。    servlet实例只创建一次,所以init初始化函数只在第一次请求时调用一次。
    有时候希望在servlet首次载入时,执行复杂的初始化任务,但并不想每个请求都重复这些任务的时候,用init()方法他在servlet初次创建时被调用,之后处理每个用户的请求时,则不在调用这个方法。因此,他主要用于一次性的初始化,和applet的init方法相同。

    由于servlet初始化参数的使用非常依赖于部署描述文件(web.xml),该文件可存放servlet所需要的起始参数以及web应用程序的结构数据。容器启动的时候会根据web.xml中的配置去调用Servlet中的init方法。在用户请求的时候去执行doPost or doGet方法。当servlet容器读取web.xml文件内容后。可以将这些起始参数封装成一个对象并在调用init方法时传递个servlet,这个对象就是ServletConfig对象所以我们可以在Servlet内覆写init方法,并通过ServletConfig对象来取得某些初始参数。

   以init参数的名称为参数,调用ServletConfig的getInitParameter方法。返回值就是init参数的值。

   config.getInitParameter() vs  req.getParameter()

ServletRequest中的public String getParameter( String name )


它用来获取客户端(.html)通过get或post方法等传递过来的值,是从客户端传递过来的,一般指的是客户端提


交的表单组件的值(eg:  <form action="servlet/AddServlet" method=GET > )


ServletConfig中的public void String getInitParameter( String name )


它用来获取Servlet的配置文件(web.xml)的初始化参数的信息,也就是我们自己的web应用程序根目录下的WEB-


INF目录下的web.xml文件中的初始化参数信息。

init() 源码

 
时间: 2024-08-26 10:11:07

Servlet life cycle的相关文章

java:Servlet(Create,LifeCycle,交互式,Tomcat文件分析)

1.Servlet: Servlet(Server Applet)是Java Servlet的简称,是为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容. 狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者.Servlet运行于支持Java的应用服务器中.从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servle

What is Servlet Container

What is Servlet Container Normal Java application’s starting point is Main(public static void main(String args[])) method. When ever java application needs to start, main method of the application should be invoked. But in case of Servlet this is not

What is a Servlet Container?

原文转至:https://dzone.com/articles/what-servlet-container In this post, I write a little bit about the basic ideas of web server, Servlet container and its relation with JVM. I want to show that Servlet container is nothing more than a Java program. 1.

net.sf.json.JSONException: There is a cycle in the

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面: //得到所有部门     //返回json对象字符串     public String getAllDep(){         List list = deptDAO.findAll(

Understanding and Using Servlet Filters

Overview of How Filters Work This section provides an overview of the following topics: How the Servlet Container Invokes Filters Typical Filter Actions How the Servlet Container Invokes Filters Figure 5-1 shows, on the left, a scenario in which no f

JSONException: There is a cycle in the hierarchy解决

使用hibernate容易出现该问题,主要是由于pojo类属性存在级联关系.比如说员工和部门,在员工表里面有部门属性,而在部门表里面有个员工集合,这样就存在了嵌套引用的问题了,就会抛出这个异常. 解决方法很简单,在将每个对象转为json对象的时候用setExcludes函数将级联的属性去除掉就可以了,如下面: //得到所有部门 //返回json对象字符串 public String getAllDep(){ List list = deptDAO.findAll(); JsonConfig co

Servlet - Upload、Download、Async

Upload.Download.Async 标签 : Java与Web Upload-上传 随着3.0版本的发布,文件上传终于成为Servlet规范的一项内置特性,不再依赖于像Commons FileUpload之类组件,因此在服务端进行文件上传编程变得不费吹灰之力. 客户端 要上传文件, 必须利用multipart/form-data设置HTML表单的enctype属性,且method必须为POST: <form action="simple_file_upload_servlet.do

Struts2 Interceptor Life Cycle

Page 1 of 2 In the last tutorial we did successfully create a working interceptor, but there is more to it. To successfully develop bug free interceptors we need to know more. In this tutorial will try to understand a crucial part of interceptor. And

Spring Bean Life Cycle Methods – InitializingBean, DisposableBean, @PostConstruct, @PreDestroy and *Aware interfaces

Spring Beans are the most important part of any Spring application. Spring ApplicationContext is responsible to initialize the Spring Beans defined in spring bean configuration file. Spring Context is also responsible for injection dependencies in th