当前位置:网站首页>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);
边栏推荐
- Nim product
- The maximum number of divisors of numbers in the int range is 1536
- [untitled] Weng_ C lesson 1
- Wc2020 course selection
- C wechat upload form data
- forEach 中 return 和 for 中 break
- C language volatile
- 【AtCoder2376】Black and White Tree(博弈)
- Classes and objects (Part 2)
- Summary of written test questions of shopee 2021 autumn recruitment
猜你喜欢

Flask页面的分页

【IoT】项目管理:如何打造更好的跨职能团队?

【AtCoder2306】Rearranging(拓扑)

Lesson 1 about Xiaobai's C language

QObject usage skills -- control function class

TiDB Cloud 上线 Google Cloud Marketplace,以全新一栈式实时 HTAP 数据库赋能全球开发者

Black Qunhui dsm7.0.1 physical machine installation tutorial

Import on CSDN MD file
![[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products](/img/85/5766d8269391820b5e142178530657.png)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products

Summary of classic interview questions
随机推荐
Three expressions of integers and their storage in memory
Black Qunhui dsm7.0.1 physical machine installation tutorial
Djikstra solves the shortest circuit with negative weight
Bidirectional linked list simple template (pointer version)
Arduino_ Esp32 development record
Classes and objects (medium)
134. 加油站
Long dialogue in June 2017
【HDU6357】Hills And Valleys(DP)
模线性方程组(中国剩余定理+通用解法)
C language Yanghui triangle code
C language to achieve a simple game - minesweeping
Sdl-3 YUV playback
Implementation of stack (C language)
Use of wordcloud
20200803 T3 my friends [divide and conquer NTT optimization recursion]
C language to achieve three piece chess (not artificial mental retardation ha ha ha)
Implementation of queue (C language)
【AtCoder1983】BBQ Hard (组合数+巧妙模型转化)
Rabin-Miller素数测试