当前位置:网站首页>Servlet、ServletConfig、ServletContext

Servlet、ServletConfig、ServletContext

2022-06-11 07:45:00 Nagisa siku

1、Servlet technology

1.1 Servlet summary :
What is? servlet: It's running on web One on the server side java Applet , Simply speaking , It's a java class .
1.2 Servlet To get started
1.Servlet Interface and implementation class of :
Servlet It's an interface , stay javax.servlet Under the bag .
Five methods :

  • destroy(): Execute this method , Destroyed servlet object
  • init(ServletConfig config): stay web Server creation servlet After object , Execute this initialization method .
  • service(ServletRequest req, ServletResponse res) : The service method , Used to receive requests sent by clients , And response data to the client
  • getServletConfig(): Not now
  • getServletInfo():servlet Information about , For example, author 、 Version and copyright , This method is not used in development .

GenericServlet, It's a class , Realized Servlet Interface
HttpServlet, It's a class , Realized Servlet Interface , At the same time inherited GenerciServlet
establish servlet Three ways
Direct realization Servlet Interface .
Inherit GenericServlet class
Inherit HttpServlet class .
Generally in development , establish servlet, Usually to inherit this HttpServlet.
such as :class MyServlet extends HttpServlet{ Rewrite the method we need }

1.3.Servlet Life cycle of :
Life cycle : From birth to the end, people in nature go to the Western Paradise , This cycle process is the human life cycle , Actually servlet The life cycle of is the same as that of human beings , from servlet Object created , After use servlet object , To the end servlet Object destroyed , The whole process is servlet Life cycle of . Three life cycle related approaches :

 Initialization phase :
init(ServletConfig config)  Initialization method : Visit this for the first time Servlet,servlet The object is created , Execute initialization method , Do it once .

 Operation phase :
service(ServletRequest req, ServletResponse res)  The service method : Each visit servlet when , All service methods will be executed , Execute many times .

 Destruction phase :
destroy() Destruction method : When the server shuts down normally . Will execute destruction method , Do it once 

2、ServletConfig Interface

What is? ServletConfig: It is servlet Configuration objects for , The function is to obtain and servlet Is the initialization parameter of .
Its subclass is GenericServlet and HttpServlet.
It is materialized through web Server implemented .
obtain servletConfig object :getServletConfig()
ServletConfig Common methods :

1. obtain servlet The value of the initialization parameter for :getInitParameter(String name): according to encoding obtain utf-8
2. obtain servlet Name of initialization parameter :getInitParameterNames() : obtain encoding and username Names such as 
3. obtain servletContext object :getServletContext()
4. obtain servlet The name of :getServletName(): Corresponding web.xml Inside TestServlet02

3、ServletContext Interface

ServletContext Object is in web The server is created when it starts , yes web Server created servletContext object ,
every last web There is only one project ServletContext object .
ServletContext The role of objects : obtain web Initialization parameters for the application , stay web Applications can share data , obtain web Resource files under the project .
obtain ServletContext object :
1. adopt ServletConfig object : config.getServletContext()
2. Directly obtained : getServletContext();
2. Implement multiple servlet Sharing data between objects : Because a web There is only one project ServletContext object . ServletContext Object is a domain object : Domain objects can store values and values in a certain range . Methods related to domain objects :
Store value :void setAttribute(String key,Object obj);
Value :Object obj = getAttribute(key);
Delete value :void removeAttribute(key);

原网站

版权声明
本文为[Nagisa siku]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020518498846.html