当前位置:网站首页>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 use
- The threshold value of fusing proportion cannot be changed with sentinel, and setting the slow call proportion has no effect
- [chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
- 创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
- `Usage of ${}`
- P1007 独木桥
- [LeetCode] 多数元素【169】
- [Solved] Splunk: Cannot get username when all users are selected“
- go 多线程数据搜索
猜你喜欢

设置单击右键可以选择用VS Code打开文件

Construction of Hisilicon 3559 universal platform: rotation operation on the captured YUV image

世界环境日 | 周大福用心服务推动减碳环保

China Academy of information technology, Tsinghua University, Tencent security, cloud native security, industry university research and use strong alliance!

Xiaopeng P7 had an accident and the airbag did not pop up. Is this normal?

首批 | 腾讯云完成国内首个云原生安全成熟度评估

最小生成树 Minimum Spanning Tree

Data analysis learning records -- complete a simple one-way ANOVA with Excel

Jatpack------LiveData
![[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements](/img/b1/1fae909fb6a9231096a93d741d6426.png)
[chestnut sugar GIS] ArcMap - how to batch modify the font, color, size, etc. of annotation elements
随机推荐
从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
Construction of Hisilicon 3559 universal platform: rotation operation on the captured YUV image
Performance optimization - rigorous mode
1px pixel compatibility of mobile terminal, 1px border
设置单击右键可以选择用VS Code打开文件
【板栗糖GIS】arcscene—如何做出有高度的高程图
Jerry's prototype will trigger shutdown after multiple touches [chapter]
泛型与反射,看这篇就够了
[leetcode] reverse string [344]
Generics and reflection, this is enough
Uniapp wechat login returns user name and Avatar
Niuke network: maximum submatrix
boot actuator - prometheus使用
编辑卡顿
Antd component upload uploads xlsx files and reads the contents of the files
Webrtc audio and video capture and playback examples and mediastream media stream analysis
Mask R-CNN
easyclick,ec权朗网络验证源码
Splunk audit 的设定
Qt QScrollArea