当前位置:网站首页>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);
}
}
边栏推荐
- go 多线程数据搜索
- [leetcode] reverse string [344]
- Niuke network: maximum submatrix
- P7072 [CSP-J2020] 直播获奖
- Uniapp wechat login returns user name and Avatar
- 【板栗糖GIS】arcmap—为什么使用自定义捕捉的时候,经典捕捉的勾要去掉呢?
- Array advanced improvement
- Xshell configuration xforward forwarding Firefox browser
- Baidu AI Cloud - create a face recognition application
- [LeetCode] 存在重复元素【217】
猜你喜欢
Gas station [problem analysis - > problem conversion - > greed]
设置单击右键可以选择用VS Code打开文件
性能优化----严苛模式
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
[LeetCode] 反转字符串中的单词 III【557】
[Solved] Splunk: Cannot get username when all users are selected“
[chestnut sugar GIS] ArcScene - how to make elevation map with height
成功改变splunk 默认URL root path
【喜欢的诗词】好了歌
Motivation du Protocole de chiffrement avancé AES
随机推荐
Jerry's fast touch does not respond [chapter]
[NPUCTF2020]ezlogin xPATH注入
Pytorch training CPU usage continues to grow (Bug)
电路设计者常用的学习网站
[leetcode] most elements [169]
[Luogu p1541] tortoise chess [DP]
剑指 Offer II 099. 最小路径之和-双百代码
Webrtc audio and video capture and playback examples and mediastream media stream analysis
Minimum spanning tree
Chow-Liu Tree
[leetcode] reverse string [344]
[Solved] Splunk: Cannot get username when all users are selected“
Learning Websites commonly used by circuit designers
Addition, deletion, modification and query of handwritten ORM (object relationship mapping)
JS syntax ES6, ES7, es8, es9, ES10, es11, ES12 new features (Abstract)
Go 4 modes Singleton
世界环境日 | 周大福用心服务推动减碳环保
Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
AES高级加密协议的动机阐述
Qt QSplitter拆分器