当前位置:网站首页>ServletContext learning diary 1
ServletContext learning diary 1
2022-07-02 23:02:00 【herb. dr】
Catalog
1.2 obtain ServletContext object
1.3.1 Get the real path of the project
1.3.2 Get the project context path
1.4 ServletContext characteristic
1.5 ServletContext Application scenarios
One 、ServletContext object
1.1 ServletContext summary
Global object , Also has scope , Corresponding to one Tomcat Medium Web application
When Web When the server starts , For every individual Web Application creation one Block shared storage area (ServletContext) .
ServletContext stay Web Create... When the server starts , Destroy on server shutdown .
1.2 obtain ServletContext object
GenericServlet Provides getServletContext() Method .( recommend ) this.getServletContext();
HttpServletRequest Provides getServletContext() Method .( recommend )
HttpSession Provides getServletContext() Method .

package com.ha.controller;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class ServletContextController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. adopt this.getServletContext();
ServletContext servletContext = this.getServletContext();
//2. adopt request Object acquisition
ServletContext servletContext1 = request.getServletContext();
//3. adopt session Object acquisition
HttpSession session = request.getSession();
ServletContext servletContext2 = session.getServletContext();
System.out.println(servletContext);
System.out.println(servletContext1);
System.out.println(servletContext2);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<servlet>
<servlet-name>ctx</servlet-name>
<servlet-class>com.ha.controller.ServletContextController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ctx</servlet-name>
<url-pattern>/ctx</url-pattern>
</servlet-mapping>
</web-app>

1.3 ServletContext effect
1.3.1 Get the real path of the project
Get the real path of the current project published on the server
String realpath=servletContext.getRealPath("/");
1.3.2 Get the project context path
Gets the current project context path ( Application name )
System.out.println(servletContext.getContextPath()); // Context path ( Application name )
System.out.println(request.getContextPath());


1.3.3 Global container
ServletContext Have scope , Data can be stored in a global container
Store the data : servletContext.setAttribute("name",value);
get data : servletContext.getAttribute("name");
Remove data : servletContext.removeAttribute("name");


1.4 ServletContext characteristic
Uniqueness : One application corresponds to one individual ServletContext.
Life cycle : As long as the container is not closed or the application is not uninstalled ,ServletContext Just one Direct existence .
1.5 ServletContext Application scenarios
ServletContext Count the number of visits to the current project
package com.qf.counter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "CounterController",value = "/counterController")
public class CounterController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. obtain ServletContext object
ServletContext servletContext = request.getServletContext();
//2. Get counter
Integer counter = (Integer) servletContext.getAttribute("counter");
if(counter == null){
counter = 1;
servletContext.setAttribute("counter",counter);
}else{
counter++;
servletContext.setAttribute("counter",counter);
}
System.out.println("counter:"+counter);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
边栏推荐
- 位的高阶运算
- boot actuator - prometheus使用
- 全面解析分享购商业模式逻辑?分享购是如何赋能企业
- PMP project integration management
- Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
- Pytorch training CPU usage continues to grow (Bug)
- [Solved] Splunk: Cannot get username when all users are selected“
- 编辑卡顿
- [NPUCTF2020]ezlogin xPATH注入
- 情感对话识别与生成简述
猜你喜欢

泛型与反射,看这篇就够了

分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少
![P7072 [CSP-J2020] 直播获奖](/img/bc/fcbc2b1b9595a3bd31d8577aba9b8b.png)
P7072 [CSP-J2020] 直播获奖
![[Solved] Splunk: Cannot get username when all users are selected“](/img/13/1e824c8005701e21fc5b4e73308d53.png)
[Solved] Splunk: Cannot get username when all users are selected“

Hanging mirror security won four global infosec awards on rsac2022

手写ORM(对象关系映射)增删改查

Array advanced improvement

Webrtc audio and video capture and playback examples and mediastream media stream analysis

MySQL reset password, forget password, reset root password, reset MySQL password

Mask R-CNN
随机推荐
Innovation strength is recognized again! Tencent security MSS was the pioneer of cloud native security guard in 2022
Splunk audit setting
P1007 single log bridge
海思3559万能平台搭建:在截获的YUV图像上旋转操作
antd组件upload上传xlsx文件,并读取文件内容
LeetCode 968. Monitor binary tree
Splunk audit 的设定
地平线2022年4月最新方案介绍
数据分析学习记录--用EXCEL完成简单的单因素方差分析
Lc173. Binary search tree iterator
mysql重置密码,忘记密码,重置root密码,重置mysql密码
【硬件】标准阻值的由来
用sentinel熔断比例阈值改不了,设置慢调用比例没效果
Qt QScrollArea
1px pixel compatibility of mobile terminal, 1px border
全面解析分享购商业模式逻辑?分享购是如何赋能企业
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
Learning records of data analysis (II) -- simple use of response surface method and design expert
E-commerce system microservice architecture
手写ORM(对象关系映射)增删改查