当前位置:网站首页>Analysis of common classes of Servlet
Analysis of common classes of Servlet
2022-07-25 07:17:00 【Hua Weiyun】
Catalog
By inheritance HttpServlet Realization Servlet Program
Use idea establish Servlet Program
servlet Method part source code
ServletConfig The three functions of classes
ServletContext The four functions of a class
1、 obtain web.xml Context parameters of the configuration in ,
Get the absolute path on the server hard disk after deployment
By inheritance HttpServlet Realization Servlet Program
In development , Inheritance is generally used HttpServlet Class to implement Servlet Program .
step :
1、 Write a class to inherit HttpServlet class
2、 Rewrite according to business needs doGet or doPost Method
3、 To web.xml Middle configuration Servlet Program
1、 Write a class ,Alt+insert Some methods needed in shortcut key rewriting
To web.xml Configure the access path in the file
The access address in the form hello Change for hello2
Run after submission :
Use idea establish Servlet Program
Select the package to implement →Servlet Program
Configuration information
Check with 3.0 The annotation configuration of
Only need web.xml Add a path to the ( Others have been generated automatically )
Servlet Inheritance system
doGet and dopost Source code
servlet Method part source code
ServletConfig class
- ServletConfig yes Servlet Program configuration information class .
- Servlet Procedure and ServletConfig All objects have Tomcat Responsible for creating , We are responsible for using .
- Servlet By default, the program is created during one access ,ServletConfig Is each Servlet When the program is created , Just create a corresponding ServletConfig object
ServletConfig The three functions of classes
1、 Can get Servlet Program alias (servlet-name Value )
2、 Get initialization parameters init-param
3、 obtain ServletContent object
Location init Method
1、 obtain servlet Alias
2、 Get initialization parameters init-param, Thanks for the others in front
Now? web.xml In the document with <init-param>
In the realization of Servlet Interface class init() Next
3、 obtain ServlertContent object
The above operation results :
every last ServletConfig Program independence , stay web.xml Information in each class in is not shared ,
rewrite init Methods should be added super.init(config), Have to access the parent class init Initialization method , Otherwise, the report will be wrong
The inheritance system can tell ServletConfig stay GenericServlet Class , In this category init Definition :
So if you rewrite init Method , Have to add super.init(config), Otherwise, the father of init Method will not execute ( The save operation in the parent class cannot be performed )
ServletContext class
What is? ServletContext?
1、ServletContent It's an interface , He said Servlet Context object
2、 One web engineering , only one ServletContext Object instances .
3、ServletContent Object is a domain object .
4、 stay web Create after the project starts , stay web Destroy after the completion of the project
What is domain object ?
Domain object , It can be like Map The same object that accesses data , It's called a domain object .
The domain here refers to the operating range of accessing data .
Comparison diagram :
| Save the data | Take the data | Delete data | |
| Map | put() | get() | remove() |
| Domain object | setAttribute() | getAttribute() | removeAttribute() |
ServletContext The four functions of a class
1、 obtain web.xml Context parameters of the configuration in context-param
2、 Get the current project path you , Format :/ Project path
3、 Get the absolute path on the server hard disk after deployment
4、 image Map And access data
Create a class with a shortcut ContextServlet, stay web.xml Configuration path ( The rest have been automatically generated )
1、 obtain web.xml Context parameters of the configuration in ,
First of all have to in web.xml Middle configuration context-param( Usually written in other servlet above )
stay ContextServlet Class doGet() Method in
After running, the result is :
It's easy to get wrong :
①: In the configuration web.xml In the address in the file / Don't forget the diagonal pole , Otherwise, the address will be invalid
②: Getting parameters in a class should be in doGet() Writing in method , Otherwise, the access address will not display the corresponding information after running
Get the current project path
Get the absolute path on the server hard disk after deployment
This diagonal bar / Indicates the path to the current project
map And access data
Create a Servlet class , stay web.xml Configure the path in the file
In class :
We can directly use getServletContext() Get objects , It's simpler , without getServletConfig().getServletContext();
We ctrl+b Look at the source :
The source code has done that step , So we can go back directly .
We continue to define a class called ContextServlet2
Deployment is omitted , After running
It's not hard to find out when ContextServlet1 There is data stored in ,ContextServlet2 The data can be found in . It's fine too context The data under is shared data
ContextServlet The object is web Create after the project starts , stay web Destroy after the completion of the project , And share
Restart and redeployment will lead to web Project destruction
ContextServlet1、2 My address is the same
边栏推荐
- Openatom xuprechain open source biweekly report | 2022.7.11-2022.7.22
- Dynamic memory management
- Common cross domain scenarios
- File operation-
- 华为无线设备配置WAPI-证书安全策略
- vulnhub CyberSploit: 1
- [computer explanation] NVIDIA released geforce RTX Super Series graphics cards, and the benefits of game players are coming!
- 【obs】视频包发送的dts_usec 计算
- 9大最佳工程施工项目管理系统
- 【程序员2公务员】关于体制调研的一些常见问题总结
猜你喜欢

How to use network installation to deploy multiple virtual servers in KVM environment

9 best engineering construction project management systems

What if Oracle 19C migration encounters large lob tables?

Luo min's backwater battle in qudian

【电脑讲解】NVIDIA发布GeForce RTX SUPER系列显卡,游戏玩家福利来了!

Day by day, month by month | Shenzhen potential technology released the extreme accelerated version of molecular docking engine uni docking

YOLOv7模型推理和训练自己的数据集

如何在KVM环境中使用网络安装部署多台虚拟服务器

QT actual combat case (53) -- using qdrag to realize the drag puzzle function

2022天工杯CTF---crypto1 wp
随机推荐
Can interface debugging still play like this?
Traffic is not the most important thing for the metauniverse. Whether it can really change the traditional way of life and production is the most important
Paddlepaddle 34 adjust the layer structure and forward process of the model (realize the addition, deletion, modification and forward modification of the layer)
【obs】视频包发送的dts_usec 计算
SAP queries open Po (open purchase order)
阿里云镜像地址&网易云镜像
微信小程序request请求携带cookie,验证是否已登录
【SemiDrive源码分析】【驱动BringUp】38 - NorFlash & eMMC分区配置
Alibaba cloud image address & Netease cloud image
解密NumPy求解梯度的一个关键难点
蔚来一面:多线程join和detach的区别?
【刷题笔记】搜索旋转排序数组
Expandablelistview nested GridView display incomplete problem
New functions of shixizhi are online. These new functions are online in June. Can you use them?
2022 Shenzhen cup
QT actual combat case (53) -- using qdrag to realize the drag puzzle function
Box horse "waist cut", blame Hou Yi for talking too much?
scrapy定时爬虫的思路
Paper reading: UNET 3+: a full-scale connected UNET for medical image segmentation
Leetcode skimming: dynamic programming 06 (integer splitting)














